Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net controls not updating after a postback

I'm writing code to read data from asp controls to update records in a database. I've been debugging the last day and I've tracked it back to something that I ought to have noticed before.

The code first populates the controls with the existing values from the database. When I click SAVE, it should read the current values from the controls and save with those. Unfortunately, what it's actually doing is using the values of the controls before a change was made to them. It's not seeing the change to the controls.

Here's a sample:

<asp:TextBox ID="OtherCourseName_5" runat="server"></asp:TextBox>

Here's the corresponding behind code in the btnSave_onClick() function:

int object_number=5;

string other_course_name_string
    = "OtherCourseName_" + object_number.ToString().Trim();

TextBox ocn = utilities
   .utils
   .FindControlRecursive(this.Master, other_course_name_string) as TextBox;

I'm using the FindControlRecursive() I found somewhere on the web. It works, I'm sure, but just in case, I tried to address the control directly as OtherCourseName_5.Text. Even if I just display the value in OtherCourseName_5.Text, it gives the original value.

I use this same page for both entering new data and for editing data. It works fine when I enter the data. That is, it correctly sees that the TextBox control has changed from empty to having data. It's only when I invoke the edit function on the page (by passing edit=true). I invoke it this way by adding the switch edit=true as a query string (the program correctly reads that switch, gets to the appropriate area of code, prints out all the correct values for everything - except the contents of the controls!).

The page is far too complicated to post the entire thing. I've tried to convey the essential details. It's entirely possible that I've made a simple coding error, but it's seeming more a possibility that I fundamentally misunderstand how pages are processed.

Is there anything known that can make it seem as though the value of a control has not been changed?

Note 1: I thought perhaps I had to go to another field after I entered the data, but I tried that and it's still a problem.

Note 2: I'm using both TextBox and DropDownList controls and have the same problem with both.

Note 3: These controls are on a panel and the page is using a SiteMaster. I haven't had any problem with that and don't think the problem is there, but I'm down to questioning the laws of the physics at this point.

like image 436
elbillaf Avatar asked Feb 15 '23 23:02

elbillaf


1 Answers

protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        //populate controls with data from database
    }
}

When you do a postback before the postback handler is evaluated the PageLoad event is raised

so if you don't avoid to rebind your control they will be loaded with the values from the database. And then the postback event will save them to db

Asp.net Page Lifecycle enter image description here
(source: microsoft.com)

like image 51
giammin Avatar answered Feb 24 '23 05:02

giammin