简体   繁体   中英

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

I have ac# asp.net applcation that has 3 pages. page 1 is data entry. Page 2 is a name search. Page 3 is a summary of account information.

The first page contains 2 asp:textbox (es) and 2 buttons. 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. button 2 submits the name to lookup to name search page via querystring.

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. 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.

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.

The code for the command button sets the value of textbox #1 and sticks it in the querystring as part of the click event; 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. 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.

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.

You need to set the Text property once (only when the GET is made):

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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