简体   繁体   English

单击时,asp.net c#命令按钮不运行命令代码

[英]asp.net c# Command button doesn't run command code when clicked

I have ac# asp.net applcation that has 3 pages. 我有3页的ac#asp.net应用程序。 page 1 is data entry. 第1页是数据输入。 Page 2 is a name search. 第2页是名称搜索。 Page 3 is a summary of account information. 第3页是帐户信息的摘要。

The first page contains 2 asp:textbox (es) and 2 buttons. 第一页包含2个asp:文本框(es)和2个按钮。 The first textbox allows a user to enter in an account number. 第一个文本框允许用户输入帐号。 The second textbox allows a user to enter in a name to "lookup an account number by name" search. 第二个文本框允许用户输入名称以“按名称查找帐号”搜索。 button 1 submits the account number in textbox number 1 to the account summary page via querystring. 按钮1通过查询字符串将文本框1中的帐号提交到帐户摘要页面。 button 2 submits the name to lookup to name search page via querystring. 按钮2通过查询字符串将名称提交到查找到名称搜索页面。

On the name search page, a list of account numbers and names is displayed with the account number being a href link back to the data entry. 在名称搜索页面上,显示帐号和名称的列表,该帐号是指向数据条目的href链接。 The account number is transferred back to the data entry page via the querystring in the link on the name search page. 帐号通过名称搜索页面上的链接中的查询字符串转移回数据输入页面。

My issue occurs as such. 我的问题是这样发生的。 When I return to the data entry page from either the name search page or the account summary page... I parse the querystring to pre-populate the value of textbox #1 with the account number. 从名称搜索页面或帐户摘要页面返回数据输入页面时,我解析查询字符串以使用帐号预填充文本框#1的值。

If the user then changes the pre-populated account number and clicks on button number 1. The data on the account summary page is for the pre-populated account number and not the new account number the user typed in. 如果用户随后更改了预先填充的帐号并单击按钮号1,则“帐户摘要”页面上的数据将针对预先填充的帐号,而不是用户键入的新帐号。

The code for the command button sets the value of textbox #1 and sticks it in the querystring as part of the click event; 命令按钮的代码设置了文本框#1的值,并将其作为click事件的一部分粘贴在querystring中。 however, the querystring retains the pre-populated value. 但是,查询字符串保留预填充的值。

            case "summary":

            sAPN = txtAPN.Text.ToString();  (<--- Textbox 1) this always comes out as the    prepopulated value.
            sfxbal = fxbal.CheckAPN(sAPN);  (<---- Validation function)
            if (sfxbal.Length == 14)        
            {
                sAPN = sfxbal;
                sfxbal = "";
            }

            if (sfxbal == "")
                Response.Redirect("CreateAccount_Summary.aspx?APN=" + sAPN);
            else
                Response.Redirect("GetAPN.aspx?APNError=" + sfxbal);
            break;

i've tried using Use Submit behavior to false. 我尝试使用使用提交行为为false。 I've tried using a javascript "onblur" and although I can display in an alert the value of txtAPN (textbox 1) with the new user entered value and not the pre-populated value, the data is always for the pre-populated value. 我尝试使用JavaScript“ onblur”,虽然我可以在警报中显示txtAPN(文本框1)的值,但输入的是新用户输入的值,而不是预先填充的值,但数据始终是预先填充的值。

No validation occurs, as well. 同样,也不会发生验证。

If I return nothing in the query string. 如果我在查询字符串中不返回任何内容。 The new user entered account number is valildated and data on the summary page is for the new account number. 将验证新用户输入的帐号,并且摘要页面上的数据将用于新帐号。

It absolutely acts like my command code is by-passed. 绝对就像我的命令代码被绕过一样。 Why would that occur? 为什么会这样?

I'm having the strong feeling that you set Text property every time the page is loaded. 我有一种强烈的感觉,那就是每次加载页面时都要设置Text属性。

You need to set the Text property once (only when the GET is made): 您只需设置一次Text属性(仅在执行GET时):

protected void Page_Load(object sender, EventArgs e) 
{
    if (!Page.IsPostBack)
    {
       txtAPN.Text = ....;
    }
}

Otherwise each time the form is submitted, the text box will get the value from query string (it overwrites the value given by the user). 否则,每次提交表单时,文本框都会从查询字符串中获取值(它将覆盖用户提供的值)。

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

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