Sunday, May 12, 2024
Homepage · c#
 Popular · Latest · Hot · Upcoming
65
rated 0 times [  72] [ 7]  / answers: 1 / hits: 11151  / 11 Years ago, wed, january 22, 2014, 12:00:00

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?


More From » c#

 Answers
15

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


[#48461] Tuesday, January 21, 2014, 11 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
jarettajb

Total Points: 678
Total Questions: 94
Total Answers: 90

Location: Guernsey
Member since Tue, Jul 6, 2021
3 Years ago
;