LinkButton click event not firing when any action done on ENTER key instead button click

631 Views Asked by At

There is ImageButton and LinkButton combined in a span and bounded with same ClickEvent to exit from current location. enter image description here On the same page there searchTextBox and button to start search action.

Now the problem is:

  • When I enter some text in textbox and hit ENTER to perform search action (it searched the data) and then clicked on LinkButton to exit, the LinkButton event not getting fire but imageButton event is getting fired.
  • Did same action as above but instead of hitting ENTER to search I clicked on search button and then I click on LinkbUtton to exit. Now the event getting fired on click of both i.e. ImageButton and LinkButton.

Why only ImageButton event is working and not LinkButton event when I search the text by pressing ENTER key.

        LinkButton newUtilityLink;

        //Separator
        Panel_UtilityLink.Controls.Add(CreateUtilityLinkSeparator());

        HtmlGenericControl span = new HtmlGenericControl("span");
        span.ID = "ExitSpanId";
        span.AddCssClass("exitspan");

        ImageButton buttonExit = new ImageButton();
        buttonExit.ID = "IDButtonExit";
        buttonExit.ImageUrl = "~/WebResources/Exit.gif";
        buttonExit.Click += new ImageClickEventHandler(UtilityLink_Click);
        buttonExit.ImageAlign = ImageAlign.Bottom;
        span.Controls.Add(buttonExit);

        newUtilityLink = new LinkButton();
        newUtilityLink.ID = "IDULinkExit";
        newUtilityLink.Text = "Exit";
        newUtilityLink.Attributes.Add("EVENT", "eventExit");
        newUtilityLink.Click += new EventHandler(UtilityLink_Click); 
        span.Controls.Add(newUtilityLink);

        Panel_UtilityLink.Controls.Add(span);

Edit 1: HTML code <span class="exitspan" id="ExitSpanId"> <input name="ctl00$IDButtonExit" align="bottom" id="IDButtonExit" style="border-top-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-left-width: 0px;" type="image" src="WebResources/Exit.gif"/> <a id="IDLinkExit" href="javascript:__doPostBack('ctl00$IDLinkExit','')" EVENT="eventExit">

Edit 2: Reason

I feel the actual cause is from Update Panel. On Hit Enter on search box does not performing Postback so Link is not getting bind. When I added AutopostBack trigger in update panel it started working but PAGE STARTED FLICKERING.

`<div id="SearchZone" runat="server">
    <span id="SearchTextSpan">
       <asp:Label ID="Label2" runat="server" meta:resourceKey="LabelSearch" />
             &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
       <asp:TextBox runat="server" Width="200px" ID="TextBoxSearch" MaxLength="70" />
              &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</span>
       <asp:Button runat="server" ID="ButtonSearch" meta:resourceKey="ButtonSearch" cssClass="button" Width="150px" OnClick="ButtonSearch_Click"  />
       <asp:HiddenField ID="PreviousSearchIndex" runat="server" />
   </div>                    
   </asp:Panel>
   </ContentTemplate>
   <Triggers>
       <asp:PostBackTrigger ControlID="ButtonSearch" />
   </Triggers>
</asp:UpdatePanel>   `
1

There are 1 best solutions below

1
On

Looking at HTML code I can see the reason why LinkButton event is not fired.

Id of element is 'IDLinkExit' while value passed to __doPostback is 'ctl00$IDLinkExit'

Compare HTML code before and after you hit enter and see if id changes. You can also debug your code and see what's the value of ClientID for LinkButton.