几个小时前我发布了一个关于我的代码不起作用的问题,在看到答案和解决方案并尝试后,我的代码仍然不起作用!
所以我尝试了调试技巧和技巧来看看发生了什么,我注意到问题不在我的代码中!这是因为按钮没有触发事件。
我已经尝试过这些解决方案,但没有一个有效:
还尝试将按钮上的
CausesValidation
添加到false和true,EnableEventValidation
添加到网络表单顶部的 true 和 false ..<script></script>
这是 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();
}
}
}
}
您现在必须发布页面的完整标记。
我们假设您向项目添加了一个新网页。
因此在项目资源管理器中,在项目解决方案正下方的下一个节点上, (右键单击并选择添加->新项目)
这样说:
然后你会得到这个:
好的,所以在上面,我们创建了一个名为 WebForm4 的新网页。
我们现在有一个空白网页,它将具有以下标记:
<%@ 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 运行页面。
如果我们点击按钮,我们会得到:
因此我们假设您遵循了上述步骤。如果没有,请按照上述步骤操作并回发发生的情况。
因此,创建一个全新的网页 - 不要使用任何现有页面。
感谢大家尝试回答这个问题。 我花了大约 9 个小时试图找出问题所在,当我检查所有文件时,我看到了一个 js lib 代码,该代码阻止页面回发和发送请求等。当我删除它时,一切正常。
我最近遇到一个问题,在 Visual Studio 2022 中,我的 ASP.Net 网页上的按钮不会创建按钮单击事件处理程序。
页面顶部有以下代码:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="MyProject.WebForm1" %>
SendMail.aspx.cs的顶部有代码:
namespace MyProject
{ public partial class WebForm1 : System.Web.UI.Page
{ }
}
问题原来是我将WebForm1.aspx重命名为SendEmail.aspx,并且我没有将所有出现的“WebForm1”修改为“SendEmail”。进行更改后,VS2022 允许我创建事件处理程序,并且当我测试代码时会触发事件。