Note that there are some explanatory texts on larger screens.

plurals
  1. POPass asp.net ClientId to Javascript Function - inline as a param
    text
    copied!<p>I have a asp Image Button that needs to be clicked via a javascript function. The keypress of a textbox should fire this image buttons server side event as well upon hitting the enter key. the coed is as follows: -</p> <p>Markup</p> <pre><code> &lt;asp:ImageButton ID="ImageButton3" runat="server" ImageUrl="images/butt_searchoff.png" class="sb_search" ToolTip="Search the Database" AlternateText="Search" OnClick="ImageButton3_Click" OnClientClick="SetExpandedCount()"/&gt; &lt;input id="tbSearch" runat="server" class="sb_input" type="text" autocomplete="off" onkeypress="return OverridePostBackOnEnter(event, '" &amp; ImageButton3.ClientID &amp; "');" /&gt; </code></pre> <p>I want to pass the ImageButton3 ClientId as a param to a Javascript function called OverridePostBackOnEnter</p> <p>The Javascript looks like this:</p> <pre><code> function OverridePostBackOnEnter(event, ctrl) { if (event.keyCode == 13) { alert(ctrl); if ($.browser.mozilla) { __doPostBack(ctrl, 'OnClick'); //for IE } else { //but for other browsers you should use __doPostBack(ctrl, 'OnClick'); } } }; </code></pre> <p>What im finding is a) i cant get the correct ClientId to be passed, i just get either undefined or null.</p> <p>and b) if i hard code and change the ctrl to 'cmain_ctl00_ImageButton3' the __dopostback is not invoking my serverside code for the ImageButton3.</p> <p>Any help on this would be greatly apreciated.</p> <p><strong>Edit</strong></p> <p>Ok think i have found a solution for this and thought i should update the post in case anyone needs it.</p> <p>Firstly i have set the 'ClientIdMode' on ImageButton3 to 'Static'</p> <pre><code> &lt;asp:ImageButton ID="ImageButton3" ClientIDMode="Static" runat="server" ImageUrl="images/butt_searchoff.png" class="sb_search" ToolTip="Search the Database" AlternateText="Search" OnClick="ImageButton3_Click" OnClientClick="SetExpandedCount()"/&gt; </code></pre> <p>this allows me to pass the id of the button to the function 'OverridePostBackOnEnter'</p> <pre><code> &lt;input id="tbSearch" runat="server" class="sb_input" type="text" autocomplete="off" onkeypress="return OverridePostBackOnEnter(event, 'ImageButton3');" /&gt; </code></pre> <p>Then my javascript becomes:</p> <pre><code> function OverridePostBackOnEnter(event, ctrl) { if (event.keyCode == 13) { if ($.browser.mozilla) { var overridctrl = document.getElementById(ctrl); __doPostBack(overridctrl.name, ''); //for IE } else { //but for other browsers you should use var overridctrl = document.getElementById(ctrl); __doPostBack(overridectrl.name, ''); } } }; </code></pre> <p>By looking into 'yourForm._EVENTTARGET.value' and 'yourForm.<em>EVENTARGUMENT.value' i could see those values were not being set. By using 'document.getelementId' and passing the 'control.name' sets those values and enables the '</em>_dopostpack' to invoke the serverside event.</p> <p>Thankyou to everyone for taking time out of their day to help me, much apreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload