i have a textbox on my form with a linkbutton next to it.
the textbox’ id is textbox1 and the linkbutton is lbSearch
in the page_load event i add:
this.TextBox1.Attributes.Add("onkeydown", "if(event.which || event.keyCode)
{
if ((event.which == 13) || (event.keyCode == 13))
{
document.getElementById('" + this.lbSearch.ClientID + "').click();
return false;
}
}
else
{
return true
}; ");
and in firefox this works but in internet explorer it doesn’t
this is because the linkbutton get’s rendered as
<a href="..."
how can i resolve this?
without changing the linkbutton to an imagebutton or regular button.
the linkbutton gets rendered like this:
<a href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$zoeken1$lbSearch", "", true, "search", "", false, true))' id="ctl00_zoeken1_lbSearch">zoek</a>
edit: i tried the following and received the following message:
function onkeydown does not always return a value
rule: 1, column: 243
source:
if(event.which || event.keyCode)
{
if ((event.which == 13) || (event.keyCode == 13))
{
var link = document.getElementById('ctl00_zoeken1_lbSearch');
__doPostBack(link.id.replace('_','$'),'');
return false;
}
else
{
return true;
}
}
else
{
return true;
};
You would be better off detecting the enter key using the
keypressrather thankeydownevent, and it would be better to use a function rather than a long piece of code in an attribute. Also if you want to prevent the default action you’ll need to returnfalsein theonkeypressattribute, as below.Doing the search and replace on the ID to get the value to pass to
__doPostBackseems hopelessly brittle and hacky. I really think you should find another way. Can you get hold of that valuectl00$zoeken1$lbSearchfrom thethis.lbSearchLinkButton object somehow?