【发布时间】:2022-01-06 15:08:33
【问题描述】:
几个小时前我发布了一个关于我的代码不起作用的问题,在看到答案和解决方案并尝试之后,我的代码仍然不起作用!
所以我尝试了调试技巧和提示以查看发生了什么,我注意到问题不在我的代码中!这是因为按钮没有触发事件。
我已经尝试了这些解决方案,但都没有奏效:
- asp.net button not firing event - (stack overflow)
- asp.net Button OnClick event not firing - (stack overflow)
- Asp.net button is not firing event - (stack overflow)
- asp.net Button OnClick event not firing - (CODE REDIRECT)
- Button click event is not firing? - (Microsoft Developer Network)
- asp.net Button OnClick event not firing - (NEWBEDEV)
还尝试将按钮上的CausesValidation添加到false和true,
重新编码按钮事件并从源代码和 html 标记中删除旧事件,
尝试将EnableEventValidation 添加到网络表单顶部的 true 和 false..
修复了 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();
}
}
}
}
【问题讨论】:
-
我在一个新项目 ASP.NET Webforms 中使用了您的代码(与此处发布的完全相同),一切都按预期工作
-
您可以尝试删除 OnClick="btnSubmit_Click" 并删除 c# 方法(备份),保存页面,然后在拆分视图中双击按钮,这应该会自动创建事件连线.获取您备份的代码并更新自动创建的方法。