开发者

.net c# postback overwrites bound textbox

开发者 https://www.devze.com 2022-12-09 04:29 出处:网络
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

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 ca开发者_如何学JAVAn'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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消