How do I make a form submit with a LinkButton?

谁说胖子不能爱 提交于 2019-12-07 01:40:56

问题


My login usercontrol has two text boxes and a linkbutton.

<asp:TextBox id="tbUserName" runat="server" size="10" />
<asp:TextBox id="tbPassword" runat="server" TextMode="Password" size="10" />

<asp:LinkButton Text="login" CssClass="submit"  runat="server" ID="lbLogin" OnClick="btnLogin_OnClick" />

I would like to call the "btnLogin_OnClick function when someone pushes enter on tbUsername or tbPassword.

How do I do this?


回答1:


Here's a neat trick:

<asp:Panel ID="pnlLogon" runat="server" DefaultButton="lbLogin" Width="100%" >
        <asp:TextBox id="tbUserName" runat="server" size="10" />
        <asp:TextBox id="tbPassword" runat="server" TextMode="Password" size="10" />
        <asp:LinkButton Text="login" CssClass="submit"  runat="server" ID="lbLogin" OnClick="btnLogin_OnClick" />
</asp:Panel>

By wrapping the textboxes in a panel and setting the DefaultButton of the Panel to your LinkButton, any Enter in the text box inside the panel will cause the LinkButton Click to happen.




回答2:


// Code-behind

protected void btnLogin_OnClick(object sender, EventArgs e)
{
    if (Page.IsValid)
    {
        // process your form
    }
}


来源:https://stackoverflow.com/questions/7217265/how-do-i-make-a-form-submit-with-a-linkbutton

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!