ASP.NET中的按钮第一次点击时未触发OnClick事件?

4

可能是重复问题:
ASP.NET中第一次点击按钮不触发OnClick事件?

<asp:Button ID="buttonSub" runat="server" CssClass="btn9" 
           OnClick="buttonSub_Click" OnClientClick="return confirmation();" Text="Next" />
       <asp:Button ID="ButtonCancel" runat="server" 
            CssClass="btn9" OnClick="ButtonCancel_Click"  CausesValidation="false"  Text="Cancel" />


try
    {

        //Resize the Photo Image 

        ResizePhoto();

        //Resize the Company Logo Image
        ResizeCompanyLogo();

        bool ErrorGuid = false;

        string LoginNameChecking = "<MortValidLoanOfficerLoginName><LoginName>" + textboxLoginName.Text.Trim() + "</LoginName></MortValidLoanOfficerLoginName>";

        dsLogincheck = objGeneric.GenericMethod(LoginNameChecking, "MortValidLoanOfficerLoginName", out ErrorGuid);

        if (Convert.ToInt32(dsLogincheck.Tables[0].Rows[0][0]) == 1)
        {

            Session["firstname"] = textboxFirstName.Text;

            Session["lastname"] = textboxLastName.Text;

            Session["loginpassword"] = textboxLoginPassword.Text.Trim();
            Session["reenterpassword"] = textboxReEnterPassword.Text.Trim();
            Session["emailaddress"] = textboxEmailAddress.Text.Trim();

            Session["loanofficertype"] = DropDownListLoanOfficerType.SelectedItem.Value.Trim();
            Session["companyname"] = textboxCompanyName.Text.Trim();
            Session["companyurl"] = textboxCompanyURL.Text.Trim();
            Session["registrationperiod"] = DropDownListRegistrationPeriod.SelectedItem.Value.Trim();
            Session["RegistrationAmount"] = Convert.ToString(100 * int.Parse(DropDownListRegistrationPeriod.SelectedItem.Value));
            Session["str"] = "yes";


            textboxLoginName.Text = "";
            ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "aa", "<script>alert('login name already exists Please enter another login name');</script>", false);
            Response.Redirect("~/LoanOfficerRegistration.aspx");
        }
        else
        {
            Server.Transfer("CreditCardInfo.aspx", true);

        }
    }
    catch (Exception ExeptionMessage)
    {


    }


protected void ButtonCancel_Click(object sender, EventArgs e)
{

    Session["LoginValidation"] = null;
    Response.Redirect("~/Logon.aspx");
}

这是一个注册表单,取消按钮在第一次点击时不起作用。当提交按钮被点击一次后,再点击取消按钮两次才能生效,否则将无法取消。

请提供解决此问题的方案。

protected void Page_Load(object sender, EventArgs e) {

    if (!IsPostBack)
    {
        GetLoanOfficerid();
        textboxFirstName.Focus();
        //Values Assigning to Registration Page Controls if Login Name Already Exists
        if (Session["str"] == "yes")
        {
            Page.RegisterStartupScript("aa", "<script>alert('login name already exists Please enter another login name');</script>");


            textboxFirstName.Text = (String)Session["firstname"]; ;
            textboxLastName.Text = (String)Session["lastname"];
            textboxLoginName.Text = "";
            textboxLoginName.Focus();
            textboxLoginPassword.Text = (String)Session["loginpassword"];

            textboxReEnterPassword.Text = (String)Session["reenterpassword"];
            textboxEmailAddress.Text = (String)Session["emailaddress"];


            DropDownListLoanOfficerType.Text = (String)Session["loanofficertype"];
            textboxCompanyName.Text = (String)Session["companyname"];
            textboxCompanyURL.Text = (String)Session["companyurl"];
            DropDownListRegistrationPeriod.Text = (String)Session["registrationperiod"];
            textboxRegistrationAmount.Text = (String)Session["RegistrationAmount"];
        }
        //Values Assigning to Registration Page Controls When Back Button is Click in CreditCardinfo Page
        if (Session["page"] == "yes")
        {
            textboxFirstName.Text = (String)Session["firstname"]; ;
            textboxLastName.Text = (String)Session["lastname"];
            textboxLoginName.Text = (String)Session["loginname"];
            //textboxLoginName.Focus();
            textboxLoginPassword.Text = (String)Session["loginpassword"];

            textboxReEnterPassword.Text = (String)Session["reenterpassword"];
            textboxEmailAddress.Text = (String)Session["emailaddress"];


            DropDownListLoanOfficerType.Text = (String)Session["loanofficertype"];
            textboxCompanyName.Text = (String)Session["companyname"];
            textboxCompanyURL.Text = (String)Session["companyurl"];
            DropDownListRegistrationPeriod.Text = (String)Session["registrationperiod"];
            textboxRegistrationAmount.Text = (String)Session["RegistrationAmount"];

        }

    }
}

2
同样的问题可能是重复的 http://stackoverflow.com/questions/5138129/button-onclick-not-firing-on-first-click-in-asp-net - Prince Ashitaka
2
不要发布第二个完全相同的问题,编辑您的原始问题并包含附加信息。 - Erik Funkenbusch
客户端的confirmation()函数是什么样子的? - DaveB
1个回答

0
确保在Page_Load函数中捕获IsPostBack。

if(this.IsPostBack) { }


如果(!IsPostBack)已经编写了这段代码但不起作用。 - rams
你能发一下你的PageLoad函数吗? - Robbie Tapping

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接