asp:ImageButton not firing onclick event

六月ゝ 毕业季﹏ 提交于 2019-12-08 15:52:11

问题


I have a page that uses a master page, several RequiredFieldValidators, and the Web Toolkit autocomplete extender. The following code only shows the bare minimum of the page:

<%@ Page Language="C#" 
    AutoEventWireup="true"  
    CodeFile="Login.aspx.cs" 
    MasterPageFile="~/master.master" 
    Inherits="Login" %>

<asp:Content id="Content1" 
    contentplaceholderid="ContentPlaceHolder1" 
    runat="server">
    <asp:UpdatePanel ID="pnlUpdate" runat="server">
        <ContentTemplate>
            <div>
                <asp:ImageButton class="submitButton" 
                    imageurl="images/button_submit.gif" 
                    id="btnSubmit" 
                    runat="server" 
                    onclick="btnSubmit_ServerClick"/>
            </div>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>

Code-behind:

protected void btnSubmit_ServerClick
      (object sender, ImageClickEventArgs e)
{
    //breakpoint here does not get hit
}

The <form runat="server"> tag is in the master page. The code above does not fire the onclick event. If I get rid of the master page and add a form tag to the page, it works. Is the form tag in the master page not supported, or is this supposed to work somehow? alt text http://digitalcopy.warnerbros.com/images/mainmenu.gif?provider=00079&disc=03403AAA-1D20-47F2-91FA-5EE632832659


回答1:


You can also check if your ImageButton does not trigger validation. If it does set its CausesValidation property to false (of course if it makes sense).




回答2:


I had a similar issue (different scenario). I used Page.RegisterRequiresRaiseEvent(ImageButton) and my onclick event started to fire. Why I needed to do that? I don't know.




回答3:


My solution was to set the ImageButton's CausesValidation to false.




回答4:


I have a similar issue with the image button and found the root cause. You are using

"ib.ID = i + ":" + j;"

as the ID of the ImageButton, the ":" is illegal name to use, as you are creating it programmatically, ASP.NET allows it to be created.

At runtime, if you look at the HTML source of the page, you will see the special characters are either ignored or replaced with "_". So the page is unable to find the correct control, thus the event won't fire. Try changing the name with plain text, the event will fire.




回答5:


ib.ID = i + ":" + j;

should be changed to

ib.ID = i.toString()+":"+j.toString();

If it still doesn't work try making use of the StringBuilder to buildup the ID and assign it later to ib.ID property




回答6:


This is solution that worked for me

If you are binding through data bound controls then use OnCommand attr instead of OnClick attr




回答7:


You have to have the control in a form runat=server somewhere, it can be in the Master page or the .aspx file. Double check that the master page form tag is runat=server

AutoEventWireup is the property that allows the syntax you are using. Double check the setting in the Master Page, WebForm and it can also be set in the web.config.

if that doesnt work, you can always explicilty code it (which I prefer)

<script runat=server>

protected override void OnInit(EventArgs e)
    {
        btnSubmit.Click += delegate(object sender, EventArgs e1)
        {

        };
        base.OnInit(e);
    }

</script>

UpdatePanel can mess with server side events being raised also, so try it without the UpdatePanel. And I am sure you have a ScriptManager in the Master Page




回答8:


From the code you supplied, you seem to be missing the <asp:scriptmanager> from your page. You must do one of the following:

  1. Have the <asp:scriptmanagerproxy> on the page and the <asp:scriptmanager> on the master page.
  2. Have <asp:scriptmanager> on your page and no <asp:scriptmanager> on the master page.

Personally, I recommend having the <form> tag on the master page, but that's personal preference.




回答9:


You can always try taking out the UpdatePanel and seeing if it works. I usually start without the UpdatePanel, get everything working the way I want and then add in the UpdatePanel and debug anything that causes.

The form in the MasterPage works for me so the ScriptManager/ScriptManagerProxy mentioned by @Keltex might be an issue, though I forget them sometimes and usually get away with it.

With the UpdatePanel the button's click event will be handled via Javascript, so you might grab FireBug or equivalent (depending on browser) and follow through what actually is happening. Is it tripping on the validation and you don't see it? Is there a JS error somewhere (the Control Toolkit isn't perfect always)? Is the page actually posting back at all and just not hitting the event handler?




回答10:


On my webpage i am creating imagebuttons dynamically inside a table that is contained by an updatepanel. The buttons are created by this code:

for (int i = 0; i < 15; i++)
    {
        TableRow tr = new TableRow();
        for (int j = 0; j < 20; j++)
        {
            TableCell tc = new TableCell();
            ImageButton ib = new ImageButton();
            ib.Click += new ImageClickEventHandler(ImageButton1_Click);
            ib.ImageUrl = "../img/defaultCell.jpg";
            ib.ID = i + ":" + j;
            tc.Controls.Add(ib);
            tc.Width = 25;
            tc.Height = 25;
            tr.Cells.Add(tc);
        }
        GameTable.Rows.Add(tr);
    }

}

The image buttons will not trigger click events. HOWEVER, if the line 'ib.ID = ...' is commented out, they do! That single alternation seems to fix all the issues. I have no idea why. If anyone can explain this, and also tell me how to trigger events keeping the ability to set button id's, i'd be much thankful




回答11:


I think it may have something to do with the fact your re-assigning the id's after creating & assigning the event handler?

Do you even need to assign the id's? - Surely this is done for you anyway? - True removing the 'ib.ID = i + ":" + j;'




回答12:


Make sure you use OnClick rather than onClick

Your update panel could be messing with the postback. Try it without the UpdatePanel and see if that is the culprit.




回答13:


I had the same problem that OnClick event of ImageButton was not firing. But the actual problem was, at form level, I had onSubmit="return false;"




回答14:


I was facing the same issue, where I was dynamically creating an ImageButton and click of that event was not triggering. hence, i created the Image button in if (IsPostBack) . Now it is working fine. And even if the page gets refresh, the ImageButton will be retained.




回答15:


I've just solved a similar issue where OutputCache was enabled. When changing from asp:ImageButton to asp:Button, the event is correctly fired. Probably asp:ImageButton has some bug with OutputCache.




回答16:


After none of the above suggestions worked for me, I did one more try by calling the button creation in OnInit(). This fixed my issue and now the OnClick event is firing.



来源:https://stackoverflow.com/questions/788018/aspimagebutton-not-firing-onclick-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!