2014年7月13日 星期日

[RESOLVED] How to display GridView after separate OnClick event


I have a file upload procedure that writes XML data to SQL Server.  I would like to display the SQL data on the same page in GridView after it is in the database, without the user having to click another button to view it.  Is there a way to do this?



Hi,




indyitman


I have a file upload procedure that writes XML data to SQL Server.  I would like to display the SQL data on the same page in GridView after it is in the database, without the user having to click another button to view it.  Is there a way to do this?


From what I understand from your POST you want to refresh the GridView to contain the newly added data with the single button click without having to click any other button.


Create an Update method to update and Bind the newly added data if you are displaying the data to GridView dynamically.


If the GridView is binded using SqlDataSource just call the Bind( ) method.


GridView1.Bind();

Key is to call the Bind( ) after each update to the database it will do the job. Just after the code to write XML to the DataBase. Call the Bind( ) method on the GridView. If the data is updated in the DB Table it will reflect the changes to the GridView.


Hope it helps!


Best Regards!



Here is the method that writes to the database.   I already built the GridView (asp:SqlDataSource ID="Users") and query, and the GridView already updates, but only shows the previous update.


I need it to update after SubmitChanges is finished. 



        internal IEnumerable updateUsers(IEnumerable newUsers)
{

using (DataContext db = new DataContext())
{
foreach (var el in users)
{

db.Users.InsertOnSubmit(new User()
{
UserID = userId,
LastName = lastName,
FirstName = firstName,
ImportDate = importDate,
ImportedBy = importedBy,
});
}
db.SubmitChanges();
};
return users;
        {

Here is the aspx.cs method I think you were recommending:


        public void ShowUpdate(object sender, EventArgs e)
{
GridView1.DataBind();
}

I added an attribute to GridView1: OnDataBinding="ShowUpdate", but can't get the GridView1.DataBind( ) method to do the update.


What am I missing?



Putting GridView.DataBind() below db.SubmitChanges() isn't working?



Also, making the GridView data bind on the OnDataBinding event seems like a potential infinite loop scenario to me. I'd get rid of that.



Thanks - it is working now.  I had moved the updateUsers method to another class, and "GridView1" was then out of scope.  After moving it back to the Default.aspx.cs, all is good.



Sweet.


沒有留言:

張貼留言