【发布时间】:2015-11-27 20:19:20
【问题描述】:
我的搜索按钮单击事件中有一些验证代码,并且遇到了必须单击两次才能工作的问题。 ASP 代码:
<asp:Button ID="SearchButton" runat="server" Text="Search" Width="148px" OnClick="SearchButton_Click" style="height: 35px" />
代码背后
protected void SearchButton_Click(object sender, EventArgs e)
{
string title = TitleSearch.Text;
Regex rgx = new Regex("^[0-9A-Za-z ]+$");
if (title != "" && !rgx.IsMatch(title))
{
ErrorLabel.Text = "Special characters are not allowed";
}
else
{
SearchButton.PostBackUrl = "results.aspx";
}
}
【问题讨论】:
-
按钮的触发器
PostBacks在 aspx 页面中.. 你能在 .cs 文件中显示你的Page_Load代码的样子吗..?也使用这个Response.Redirect("results.aspx");而不是SearchButton.PostBackUrl="resuts.aspx"; -
@MethodMan 是对的。第一次单击按钮时,会设置按钮的
PostBackUrl值。第二次单击时,可以使用该值。因此,请按照建议尝试Response.Redirect。 -
问题是它似乎没有将任何数据发布到下一页。我真正想要做的是仅在正则表达式验证时回发
-
你是什么意思
Problem there is that it doesn't seem to post any data to the next page请解释你正在尝试做什么以及你对PostBack这个词的理解 -
好吧,我可能应该从这里开始。这一点是我试图防止使用正则表达式将特殊字符输入到文本框中,如果它有效,我试图将页面上的数据发布到结果页面。我决定对按钮控件进行验证,这似乎是一个错误,因为 response.redirect 似乎将数据发布到下一页。