ASP文本框没有更新,什么时候检查PostBack?(ASP Textbox not updating, when to check PostBack?)

编程入门 行业动态 更新时间:2024-10-28 02:31:04
ASP文本框没有更新,什么时候检查PostBack?(ASP Textbox not updating, when to check PostBack?)

问题

<asp:TextBox>未更新。

的背景

我有一个名为Add_Edit_Drugs.aspx的页面。 可以通过以下两种方式之一访问此页面:用户可以单击“ Add Drug按钮,该按钮将触发Response.Redirect("Add_Edit_Drugs.aspx")命令。 或者,他们可以点击超链接,将它们发送到同一页面,如下所示:

<asp:HyperLinkField DataNavigateUrlFields="Drug_INDEX" DataNavigateUrlFormatString="Add_Edit_Drugs.aspx?Drug_INDEX={0}" DataTextField="Drug_Name" HeaderText="Drug Name" />

The Background

I have a page called Add_Edit_Drugs.aspx. This page can be accessed in one of two ways: the user can click on the Add Drug button, which fires the Response.Redirect("Add_Edit_Drugs.aspx") command. OR, they can click on a hyperlink, which sends them to the same page like this:

<asp:HyperLinkField DataNavigateUrlFields="Drug_INDEX" DataNavigateUrlFormatString="Add_Edit_Drugs.aspx?Drug_INDEX={0}" DataTextField="Drug_Name" HeaderText="Drug Name" />

The Page_Load event checks for the presence of a query string, and if it's there, it populates the fields with the information in the database, otherwise it just leaves them blank:

protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString["Drug_INDEX"] != null) { String sIndex = Request.QueryString["Drug_INDEX"].ToString(); if (!Int32.TryParse(sIndex, out Drug_INDEX)) //Query String not a valid index { errorMessage.Text = "There was an error retrieving the record."; message.Text = "Please contact you IT administrator."; } else { Drug_IO dio = new Drug_IO(); SqlDataReader drugDataReader = dio.Get_Drug(Drug_INDEX); if (drugDataReader.Read()) { drugNameTxt.Text = drugDataReader["Drug_Name"].ToString(); descriptionTxt.Text = drugDataReader["DrugDescription"].ToString(); toxTxt.Text = drugDataReader["Toxicity"].ToString(); hlTxt.Text = drugDataReader["HalfLife"].ToString(); hlUnitsTxt.Text = drugDataReader["HalfLife_Units"].ToString(); bindingTxt.Text = drugDataReader["ProteinBinding"].ToString(); logPTxt.Text = drugDataReader["LogP"].ToString(); molecularWeightTxt.Text = drugDataReader["MolecularWeight"].ToString(); molecularFormTxt.Text = drugDataReader["MolecularFormula"].ToString(); chemicalForumlaTxt.Text = drugDataReader["ChemicalFormula"].ToString(); } } } }

Then I have my saveBtn_Click method:

protected void saveBtn_Click(object sender, EventArgs e) { double halfLife; if (!Double.TryParse(hlTxt.Text, out halfLife)) halfLife = Double.NaN; double logP; if (!Double.TryParse(logPTxt.Text, out logP)) logP = Double.NaN; double moleWeight; if (!Double.TryParse(molecularWeightTxt.Text, out moleWeight)) moleWeight = Double.NaN; if (-1 == Drug_INDEX) // New record to be inserted { Drug_Record dr = new Drug_Record( drugNameTxt.Text, descriptionTxt.Text, toxTxt.Text, halfLife, hlUnitsTxt.Text, bindingTxt.Text, logP, moleWeight, molecularFormTxt.Text, chemicalForumlaTxt.Text); if (dr.saveToDataBase()) { Response.Redirect("List_Drugs.aspx"); } else { message.Text = "An error occured saving the record."; errorMessage.Text = "Please contact your system admin."; } } else //record already exists in db, needs to be updated { Drug_Record dr = new Drug_Record( drugNameTxt.Text, descriptionTxt.Text, toxTxt.Text, halfLife, hlUnitsTxt.Text, bindingTxt.Text, logP, moleWeight, molecularFormTxt.Text, chemicalForumlaTxt.Text, Drug_INDEX); if (dr.updateRecord()) { Response.Redirect("List_Drugs.aspx"); } else { message.Text = "An error occur updating the record."; errorMessage.Text = "Please contact your system admin."; } } }

Now, as I'm sure you've guessed, in my else //record already exists block, when I get the (supposedly) new values from the user edit, what I actually get is the original information which was retrieved from the database. I know from what I've read that the problem lies in the issue of whether or not Page.IsPostBack. I checked in Page_Load. I checked in saveBtn_Click. I checked before creating a new record, I checked before updating a record. I combined all those things. I inverted them. No matter what I've tried, one of two things happens:

I get the old information. Nothing.

This specific problem is really a symptom of a larger problem - I (kinda) understand what a PostBack is, but don't really understand what causes it. In this instance, what causes a PostBack, and when do I need to check for it?

最满意答案

在你的页面加载中你应该检查它是否是使用Page.IsPostBack的回发,如果它不是回发,那么你应该初始加载页面。 如果是回发,则页面上的一个控件已启动服务器端事件,例如按钮单击。

在按钮单击中,您可以更新标签或文本框。 每次回发都会调用页面加载,因此请确保不要运行两次初始化代码。

当您只想在初始加载时运行它时,看起来您的页面加载正在为每个回发运行初始化。 试试这个修改:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request.QueryString["Drug_INDEX"] != null) { String sIndex = Request.QueryString["Drug_INDEX"].ToString(); if (!Int32.TryParse(sIndex, out Drug_INDEX)) //Query String not a valid index { errorMessage.Text = "There was an error retrieving the record."; message.Text = "Please contact you IT administrator."; } else { Drug_IO dio = new Drug_IO(); SqlDataReader drugDataReader = dio.Get_Drug(Drug_INDEX); if (drugDataReader.Read()) { drugNameTxt.Text = drugDataReader["Drug_Name"].ToString(); descriptionTxt.Text = drugDataReader["DrugDescription"].ToString(); toxTxt.Text = drugDataReader["Toxicity"].ToString(); hlTxt.Text = drugDataReader["HalfLife"].ToString(); hlUnitsTxt.Text = drugDataReader["HalfLife_Units"].ToString(); bindingTxt.Text = drugDataReader["ProteinBinding"].ToString(); logPTxt.Text = drugDataReader["LogP"].ToString(); molecularWeightTxt.Text = drugDataReader["MolecularWeight"].ToString(); molecularFormTxt.Text = drugDataReader["MolecularFormula"].ToString(); chemicalForumlaTxt.Text = drugDataReader["ChemicalFormula"].ToString(); } } } } }

现在发生的事情是,当发生PostBack时,它将不会运行初始化代码,它将继续执行您的按钮单击事件,您可以在其中更新文本框的值。 在这种情况下,PageLoad确实是唯一需要检查以查看它是否为回发的地方,因为按钮事件只能通过回发触发。

In your page load you should check to see if it is a postback using Page.IsPostBack if it is not a postback then you should do your initial loading of the page. If it is a postback then one of the controls on the page has initiated a server side event, such as a button click.

In your button click you can update the labels or textboxes. The page load will be called on every postback, so make sure you don't run initialization code twice.

It looks like your page load is running the initialization for every postback, when you only want to run it on the initial load. Try this modification:

protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { if (Request.QueryString["Drug_INDEX"] != null) { String sIndex = Request.QueryString["Drug_INDEX"].ToString(); if (!Int32.TryParse(sIndex, out Drug_INDEX)) //Query String not a valid index { errorMessage.Text = "There was an error retrieving the record."; message.Text = "Please contact you IT administrator."; } else { Drug_IO dio = new Drug_IO(); SqlDataReader drugDataReader = dio.Get_Drug(Drug_INDEX); if (drugDataReader.Read()) { drugNameTxt.Text = drugDataReader["Drug_Name"].ToString(); descriptionTxt.Text = drugDataReader["DrugDescription"].ToString(); toxTxt.Text = drugDataReader["Toxicity"].ToString(); hlTxt.Text = drugDataReader["HalfLife"].ToString(); hlUnitsTxt.Text = drugDataReader["HalfLife_Units"].ToString(); bindingTxt.Text = drugDataReader["ProteinBinding"].ToString(); logPTxt.Text = drugDataReader["LogP"].ToString(); molecularWeightTxt.Text = drugDataReader["MolecularWeight"].ToString(); molecularFormTxt.Text = drugDataReader["MolecularFormula"].ToString(); chemicalForumlaTxt.Text = drugDataReader["ChemicalFormula"].ToString(); } } } } }

Now what happens is that when a PostBack happens, it will not run the initialization code and it will continue on to your button click event where you can update the value of the your textbox. PageLoad is really the only place you need to check to see if it is a postback in this situation, since button events can only be triggered by a postback.

更多推荐

本文发布于:2023-07-19 02:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1171561.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:什么时候   文本框   ASP   PostBack   check

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!