ASP.NET DefaultButton 和 MasterPages
在我的网站中,我在母版页中有一个搜索功能(那里没有设置默认按钮,也没有在表单中)。在内容页面中,我有一个登录,在那里我使用带有默认按钮的 asp 面板。 但是当我单击登录文本框上的 Enter 时,我的网站将继续转到搜索事件处理程序... 可能是什么原因?
一些代码:
//on content page
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}
<asp:Panel ID="pnl1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:LinkButton ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</asp:Panel>
//on master page:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!txtSearch.Text.Equals(""))
{
Response.Redirect("searchresults.aspx?search=" + txtSearch.Text);
}
}
<div id="searchbar">
<asp:TextBox ID="txtSearch" CssClass="searchbar-field" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" CssClass="searchbar-btn" runat="server" Text="Zoek" OnClick="btnSearch_Click" />
</div>
OK找到了解决方案:需要使用Button
而不是LinkButton
。那么应该就可以了...
In my site i have a search function in the master page (no defaultbutton set there, also not in form). in a content page, i have a login, there i use a asp panel with defaultbutton.
but when i click enter on the login textbox then my site keeps going to the search event handler...
What could be the reason?
Some code:
//on content page
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write(Button1.Text);
}
<asp:Panel ID="pnl1" runat="server" DefaultButton="Button1">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:LinkButton ID="Button1" runat="server" Text="Button1" OnClick="Button1_Click" />
</asp:Panel>
//on master page:
protected void btnSearch_Click(object sender, EventArgs e)
{
if (!txtSearch.Text.Equals(""))
{
Response.Redirect("searchresults.aspx?search=" + txtSearch.Text);
}
}
<div id="searchbar">
<asp:TextBox ID="txtSearch" CssClass="searchbar-field" runat="server"></asp:TextBox>
<asp:Button ID="btnSearch" CssClass="searchbar-btn" runat="server" Text="Zoek" OnClick="btnSearch_Click" />
</div>
OK found the solution: It is required to use Button
and not LinkButton
. Then it should be alright...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只需在页面加载时设置页面上的默认按钮即可:
您可以使用面板的 FindControl 方法访问该按钮(这是 VB)。
You just need to set the default button on the page on the page load:
You can access the button using the FindControl method of the panel (this is VB).
发现问题,我认为需要使用
Button
而不是LinkButton
。那么应该就可以了。Found the problem, i think it is required to use
Button
and NOTLinkButton
. Then it should be alright.在加载登录控件的任何页面的标记中,您需要在两个位置更新 html。
首先,在页面的表单标签中,您需要设置默认按钮。请参阅下文,了解我是如何想出这个名字的。
(命名:美元符号之前的 ucLogin 部分需要是您的登录控件的 ID,如页面中进一步声明的那样。btnSubmit 部分需要是按钮的 ID,因为它在登录控件的 html 中命名)
接下来,您需要将登录控件的声明包装在面板中,并设置它的 DefaultButton 属性:
这应该可以为您完成。
In the markup of any pages that load your login control, you need to update the html in two places.
First, in the page's form tag, you need to set the default button. See below for how I came up with the name.
(Naming: The ucLogin part before the dollar sign needs to be the ID of your login control, as declared further down in your page. The btnSubmit part needs to be the ID of the button as it’s named in the login control’s html)
Next, you need to wrap the declaration of your login control in a panel, and set it’s DefaultButton property, as well:
That should do it for you.