views:

7428

answers:

9

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

A: 

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

Chad Grant
The master page also has AutoEventWireup=true, and the web.config does not override it. Bummer.
cdonner
I moved the form tag (and the script manager) out of the master page and into the content page. The event fires now, and I still have the master page. Not ideal, but enough of a workaround for now.
cdonner
If you have an UpdatePanel in your master page, try removing it
Chad Grant
Negative - only plain HTML in the master page.
cdonner
A: 

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.

Keltex
Great point. Adding a scriptmanagerproxy did not help though. I put the form back on the master, and kept the scriptmanager on the content page, which also did not work. It only works if the both the form and the scriptmanager tags are on the content page. Weird.
cdonner
A: 

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?

Jared
I did strep through the JS and saw the event being processed, and no exception raised. Why it does not get send to the server was not obvious to me. I would have to dig deeper into the generated client-side code in order to understand it. Removing the UpdatePanel did not change the behavior.
cdonner
What happens if you swap a normal button for the image button? Does it work? Does it behave the same in all browsers?
Jared
Reading your comment more closely it sounds like a validator/JS issue, since it's not making it back to the server. Do you have any other controls like Control Toolkit controls that could interfere with the Image Button?
Jared
+1  A: 

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).

korchev
It does trigger validation, because it works fine with the workaround in place.
cdonner
A: 

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

You should post this as a separate question, not as an answer.
Bill the Lizard
A: 

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;'

A: 

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.

Nate
+1  A: 

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.

Suneesh Uthaman
A: 

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.

Chris Ballance