简体   繁体   English

ASP.NET DefaultButton和MasterPages

[英]ASP.NET DefaultButton and MasterPages

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. 在一个内容页面,我有一个登录,我使用带有defaultbutton的asp面板。 but when i click enter on the login textbox then my site keeps going to the search event handler... What could be the reason? 但是当我点击登录文本框中的Enter然后我的网站继续前往搜索事件处理程序......可能是什么原因?

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 . 确定找到了解决方案:需要使用Button而不是LinkButton Then it should be alright... 那应该没问题......

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). 您可以使用面板的FindControl方法访问该按钮(这是VB)。

Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load

    Me.Form.DefaultButton = pnl1.FindControl("Button1").UniqueID

End Sub

Found the problem, i think it is required to use Button and NOT LinkButton . 发现问题,我认为需要使用Button和NOT LinkButton Then it should be alright. 那应该没问题。

In the markup of any pages that load your login control, you need to update the html in two places. 在加载登录控件的任何页面的标记中,您需要在两个位置更新html。

First, in the page's form tag, you need to set the default button. 首先,在页面的表单标签中,您需要设置默认按钮。 See below for how I came up with the name. 请参阅下文,了解我如何提出名称。

<form id="form1" runat="server" defaultbutton="ucLogin$btnSubmit">

(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) (命名:美元符号之前的u​​cLogin部分需要是您的登录控件的ID,如在页面中进一步声明的那样.btnSubmit部分需要是按钮的ID,因为它在登录控件的html中命名)

Next, you need to wrap the declaration of your login control in a panel, and set it's DefaultButton property, as well: 接下来,您需要在面板中包装登录控件的声明,并设置它的DefaultButton属性:

<!-- Login Control - use a panel so we can set the default button -->
<asp:Panel runat="server" ID="loginControlPanel" DefaultButton="ucLogin$btnSubmit">                         
     <uc:Login runat="server" ID="ucLogin"/>                                                    
</asp:Panel>

That should do it for you. 那应该为你做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM