views:

975

answers:

2

I have a gridview, the contents of which are provided by accessdatasource the data displayed is dependant on a dropdown list which has a postback event

  • The page loads
  • User selects an item from the dropdown
  • page reloads with new data in the gridview

So far so good

I have then added a textbox to the grid view (no I can't to use the standard "edit" link) The textbox is in a itemtemplate

this textbox contains editable data for the user to update and send back to the server with a click of the update button.

The problem is if I change the selection in the dropdown the gridview updates as it should but the textboxes retain the old value even though they are bound with <%# Bind("vr_total") %>

I can only assume that this is caused by the postback data and that the data is overwritten after the binding occurs. (otherwise the bind would overwrite the old unwanted data)

Can someone please explain how I can change this behaviour.

viewstate is set to false

I am new to .net and c#

DC

A further development..

If I replace the <asp:textbox with <input type="text" value="<%# Eval("vr_total") %>" ... > the grid works exactly as expected.

The problem appears to be the gridview populates its child controls when the data is bound but the textbox overwrites it with what was posted (via the postback event on the dropdown) even if its viewstate is disabled.

A: 

Databinding only overwirtes the data when it is called usally with a Page.DataBind, this is normally only done on the initial get of the page, in your code behind

override void OnLoad(){
    if(this.IsPostBack == false){
        this.DataBind();
    }
}

Or something similar, when your post back is ocurring you are not databinding. This is usally the correct behaviour.

If you wish to re-Databind just the grid when your dropdown changes you can add a serverside onChange event handler to your drop down and specificly databind your Gridview e.g.

void OnDropDownChange(EventArgs args){
    gridView.DataBind();
}

Probably a good idea for you to read some more about Databinding as it is very important and very different to a lot of other approaches. http://support.microsoft.com/kb/307860 would be a place to start.

David Waters
Thanks the link was helpful. In this case I do want to databind even if its a postback. Because the postback in question is changing the datasource. therefore the textboxes must change and not retain their controlstate.
DeveloperChris
+1  A: 

Chris, Controls have what is known as ControlState, which you can't disable. The problem you're encountering is that you're loading and binding in page load, then an action is being fired afterward.

The way we work around this problem is to load data in Page_Load and bind data in OnPreRender.

for example:

private void Page_Load(object sender, System.EventArgs e)
{
  grid.DataSource = whatever;
}

// your dropdown event occurs between these two events

protected override void OnPreRender(EventArgs e)
{
   grid.DataBind();
}

To better understand ASP.NET ViewState, check out http://msdn.microsoft.com/en-us/library/ms972976.aspx

Jim Schubert
OnPrerender is the trick I needed thanks! Interestingly the databind appears to be implicit with changing the SelectCommand for the datasource. In other words If I set the SelectCommand Property in OnPreRender I don't need to call the DataBind method. it runs it anyway. What I have done is initialised the SelectCommand to nothing to prevent it from running (assumed) and only set it in OnPreRender which then fills it with the new dataset.Much appreciated Jim DC
DeveloperChris