简体   繁体   English

Asp.net控制回发后不更新

[英]Asp.net controls not updating after a postback

I'm writing code to read data from asp controls to update records in a database. 我正在编写代码来从asp控件读取数据以更新数据库中的记录。 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. 当我单击SAVE时 ,它应该从控件中读取当前值并保存。 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: 这是btnSave_onClick()函数中相应的后面代码:

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. 我正在使用我在网络上找到的FindControlRecursive() It works, I'm sure, but just in case, I tried to address the control directly as OtherCourseName_5.Text . 我确定它有效,但为了以防万一,我试图直接将控件作为OtherCourseName_5.Text来解决。 Even if I just display the value in OtherCourseName_5.Text , it gives the original value. 即使我只是在OtherCourseName_5.Text显示该值,它OtherCourseName_5.Text给出原始值。

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. 也就是说,它正确地看到TextBox控件已从空变为拥有数据。 It's only when I invoke the edit function on the page (by passing edit=true ). 它只在我调用页面上的编辑功能时(通过传递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!). 我通过添加switch edit=true作为查询字符串来调用它(程序正确地读取该开关,到达适当的代码区域,打印出所有正确的值 - 除了控件的内容!)。

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. 注1 :我想也许在输入数据后我不得不去另一个领域,但我试过了,这仍然是一个问题。

Note 2 : I'm using both TextBox and DropDownList controls and have the same problem with both. 注意2 :我正在使用TextBoxDropDownList控件,并且两者都有相同的问题。

Note 3 : These controls are on a panel and the page is using a SiteMaster . 注意3 :这些控件位于面板上,页面使用的是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. 我没有遇到任何问题,也不认为问题存在,但我现在要质疑物理定律。

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 在评估回发处理程序之前执行回发时,将引发PageLoad事件

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 然后回发事件将它们保存到db

Asp.net Page Lifecycle Asp.net页面生命周期 在此输入图像描述
(source: microsoft.com ) (来源: microsoft.com

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM