Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In aspx how to trigger button click event when press "Enter" on a textbox while the button click event defined in source cs file

While, I want to trigger btnSearchSuiteGroup_Click event when pressing "enter" on txtSuiteGroupName which described in aspx, code below:

<asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD" onkeypress="return searchKeyPress(event)"></asp:TextBox>
<asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search"  CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
<script type="text/javascript">
    function searchKeyPress(e) {

        // look for window.event in case event isn't passed in
        if (typeof e == 'undefined' && window.event) { e = window.event; }
        if (e.keyCode == 13) {
            document.getElementById('<%=btnSearchSuiteGroup.ClientID%>').click();
        }
    }
</script>

While, the btnSearchSuiteGroup_Click is defined in the source cs file:

protected void btnSearchSuiteGroup_Click(object sender, EventArgs e)
{
    this.LinqDataSource1.WhereParameters["SuiteGroupName"].DefaultValue = this.txtSuiteGroupName.Text;
    this.GridView1.DataBind();
    if (GridView1.Rows.Count == 0)
        Response.Write("<script language='javascript'>window.alert('No record found!')</script>");
}

When I browse the website, the keypress on the textbox cannot initiate the button click event, anything wrong in the code?

like image 591
Jerry Avatar asked Jan 11 '23 09:01

Jerry


1 Answers

If you use Panel you would not need to use any javascript function. You can specify default button Id for panel like below

    <asp:Panel runat="server" DefaultButton="btnSearchSuiteGroup">
       <asp:TextBox ID="txtSuiteGroupName" runat="server" clientidmode="Static" CssClass="DD">          
       </asp:TextBox>
        <asp:Button ID="btnSearchSuiteGroup" runat="server" Text="Search"  CssClass="DD" Width="64px" onclick="btnSearchSuiteGroup_Click" />
        </asp:Button>
    </asp:Panel>

OfCourse you can have Multiple panels on single page for assigning different default buttons for separate panels!

For More On Panel.DefaultButton Property

like image 182
Nitin Varpe Avatar answered Jan 26 '23 11:01

Nitin Varpe