4

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?

ekad
  • 14,436
  • 26
  • 44
  • 46
Jerry
  • 67
  • 1
  • 2
  • 6
  • The above code triggers `btnSearchSuiteGroup_Click` for me. Can you try to debug and set a break point inside `btnSearchSuiteGroup_Click`? – ekad Jan 22 '14 at 07:17

2 Answers2

9

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

Nitin Varpe
  • 10,450
  • 6
  • 36
  • 60
0

I suggest the following method: 1. Create a function 'ButtonClick' and put all the code in 'btnSearchSuiteGroup_Click' function into it. 2. Add 'onkeypress' event for the textbox 'txtSuiteGroupName' by looking at this thread 3. Whenever the above event is fired, see if the entered key is 'Enter'. 4. If the key is 'Enter' key, call the function 'ButtonClick'.

Community
  • 1
  • 1
Nishanth Reddy
  • 589
  • 2
  • 14
  • 27