簡體   English   中英

ASP.NET 按鈕根本沒有觸發事件

[英]ASP.NET button not firing event at all

幾個小時前,我發布了一個關於我的代碼不起作用的問題,在看到答案和解決方案並嘗試之后,我的代碼仍然不起作用!

所以我嘗試了調試技巧和提示,看看發生了什么,我注意到問題不在我的代碼中。 這是因為按鈕沒有觸發事件。

我已經嘗試了這些解決方案,但都沒有奏效:

還嘗試將按鈕上的CausesValidation添加到falsetrue
重新編碼按鈕事件並從源代碼和 html 標記中刪除舊事件,
試圖在 web 表單的頂部將EnableEventValidation添加到truefalse ..
修復 js 鏈接為<script></script>
沒有任何工作</3請幫助我

這是 HTML 標記:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="index.aspx.cs" Inherits="sevenCODE.index" %>

<form id="signup_form" runat="server">
        <asp:TextBox ID="email" TextMode="Email" required="true" runat="server"></asp:TextBox>
        <asp:Button ID="btnSubmit" runat="server" Text="Notify me" OnClick="btnSubmit_Click" />
</form>

這是背后的代碼:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    //This event is for submitting an email address to receive future e-mails.
    string cs = ConfigurationManager.ConnectionStrings["notifyCS"].ConnectionString;

    //also I tried to not use using()
    using (SqlConnection con = new SqlConnection(cs))
    {

        try
        {
            con.Open();

            //SQL statements
            //check if there is an email registered before
            string checkEmail = "SELECT User_Email FROM tbl_users WHERE User_Email = @User_Email";

            //check subscription if the above email is registered.
            string checkSubscription = "SELECT User_Status FROM tbl_users WHERE User_Email = @User_Email";

            //Insert email as a new one in the db
            string submitEmail = "INSERT INTO tbl_users (User_UID, User_Email, User_Status) VALUES (@User_UID, @User_Email, @User_Status)";

            //Update email info if the email is already registered.
            string updateEmail = "UPDATE tbl_users SET User_UID = @User_UID, User_Status = @User_Status WHERE User_Email = @User_Email";


            //check if the submitted email is in the db
            using (SqlCommand cEmailCMD = new SqlCommand(checkEmail, con))
            {
                cEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                SqlDataAdapter cEmailSDA = new SqlDataAdapter
                {
                    SelectCommand = cEmailCMD
                };
                DataSet cEmailDS = new DataSet();
                cEmailSDA.Fill(cEmailDS);

                //If there is no email registered
                if (cEmailDS.Tables[0].Rows.Count == 0)
                {
                    using (SqlCommand sEmailCMD = new SqlCommand(submitEmail, con))
                    {
                        //Generate UID to add so later we can play with it
                        string User_UID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();

                        //Insert data as new email
                        sEmailCMD.Parameters.AddWithValue("@User_UID", HttpUtility.HtmlEncode(User_UID));
                        sEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                        sEmailCMD.Parameters.AddWithValue("@User_Status", HttpUtility.HtmlEncode("subscribed"));
                        sEmailCMD.ExecuteNonQuery();
                        sEmailCMD.Dispose();
                        con.Close();
                        con.Dispose();
                        email.Text = null;
                        Response.Redirect("index.aspx");
                    }
                }
                //If there is an email registered
                else
                {
                    //first check subscription of the email
                    //if sub != true then we can resubscribe email again with different UID and change status
                    using (SqlCommand cSubCMD = new SqlCommand(checkSubscription, con))
                    {
                        cSubCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                        SqlDataAdapter cSubSDA = new SqlDataAdapter
                        {
                            SelectCommand = cSubCMD
                        };
                        DataSet cSubDS = new DataSet();
                        cSubSDA.Fill(cSubDS);
                        string result = cSubDS.Tables[0].Rows[0]["User_Status"].ToString();
                        if (result != "subscribed")
                        {
                            using (SqlCommand uEmailCMD = new SqlCommand(updateEmail, con))
                            {
                                //Generate UID to update the old one
                                string User_UID = System.Guid.NewGuid().ToString().Replace("-", "").ToUpper();

                                //update data
                                uEmailCMD.Parameters.AddWithValue("@User_UID", HttpUtility.HtmlEncode(User_UID));
                                uEmailCMD.Parameters.AddWithValue("@User_Email", HttpUtility.HtmlEncode(email.Text));
                                uEmailCMD.Parameters.AddWithValue("@User_Status", HttpUtility.HtmlEncode("subscribed"));
                                uEmailCMD.ExecuteNonQuery();
                                uEmailCMD.Dispose();
                                con.Close();
                                con.Dispose();
                                email.Text = null;
                                Response.Redirect("index.aspx");
                            }
                        }
                    }
                }
            }
        }
        //if there is any errors please show me (debug)
        //show the user an error message (release)
        catch (Exception ex)
        {
            Response.Write(ex.Message);
        }
        //check if the connection is still open, and if is, please close and dispose it.
        finally
        {
            if (con.State == ConnectionState.Open)
            {
                con.Close();
                con.Dispose();
            }
        }
    }
}

您現在必須發布頁面的完整標記。

我們假設您在項目中添加了一個新的 web 頁面。

所以在項目資源管理器中,在項目解決方案下方的下一個節點上,(您右鍵單擊並選擇添加->新項目)

像這樣說:

在此處輸入圖像描述

然后你得到這個:

在此處輸入圖像描述

好的,所以在上面,我們創建了一個名為 WebForm4 的新 web 頁面。

我們現在有一個空白的 web 頁面,它會有這個標記:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm4.aspx.cs" Inherits="CSharpWebApp.WebForm4" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>
        </div>
    </form>
</body>
</html>

好的,現在我們從工具箱中拖放一個按鈕 - 像這樣說:

在此處輸入圖像描述

所以,我們可以按 ctrl-s 來衡量。

現在,我們有這個:

讓我們在文本框中拖放

所以現在我們有了這個:

在此處輸入圖像描述

現在雙擊按鈕 - 我們自動跳轉到后面的代碼:

我們現在可以輸入按鈕單擊事件的代碼。 說這個:

public partial class WebForm4 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        TextBox1.Text = "Hello World";
    }
}

現在,f5 運行頁面。

如果我們點擊按鈕,我們會得到:

在此處輸入圖像描述

因此,我們假設您遵循了上述步驟。 如果沒有,請按照上述步驟並回發發生的情況。

因此,創建一個全新的 web 頁面 - 不要使用任何現有頁面。

謝謝大家嘗試回答這個問題。我花了大約 9 個小時試圖找出問題所在,當我檢查所有文件時,我看到了一個 js lib 代碼,它阻止了頁面回發和發送請求等。 . 當我刪除它時,一切正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM