ImageButton on Command - not firing

Rate It (1)

Last post 07-22-2009 2:47 PM by BenHur. 18 replies.

Sort Posts:

  • ImageButton on Command - not firing

    06-27-2009, 12:35 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Hi, 

    I have an image button like this:

    <asp:ImageButton ID="ImageButton1" runat="server" 
                            OnCommand="addProductToWishList" 
                            ImageUrl="../Site/images/featured-wishlist.jpg" 
                            CommandName="Click" 
                            CommandArgument="<%# Container.ItemIndex%>" />

    The problem is that on click this button nit fires this method:

     public void addProductToWishList(object sender, CommandEventArgs e)
        {
            if (e.CommandName == "Click")
            {
                ImageButton imgBtnWishList = (ImageButton)sender;
                Repeater repetear = (Repeater)imgBtnWishList.Parent;
    
                if (MySession.Current.userId != 0)
                {
                    int userId = MySession.Current.userId;
                    wishQ.insertInWishList(userId, utils.getCategoryAliasById(category), getProductId(e.CommandArgument.ToString()));
                }
                else
                //
                {
                    ModalPopupExtender loginPopUp = (ModalPopupExtender)Page.FindControl("loginPopUp");
                    loginPopUp.Show();
                }
            }
        }
    Do you have any sugestions because i spent over 2 hours struggling with this problem, and no way....

    Thanks in advance!



  • Re: ImageButton on Command - not firing

    06-27-2009, 1:01 PM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

     use onclick event instead

     

  • Re: ImageButton on Command - not firing

    06-27-2009, 2:34 PM
    • Member
      116 point Member
    • galib
    • Member since 04-16-2008, 8:19 AM
    • Dhaka
    • Posts 22

    hi BenHur,

    Change the method's Access Modifier like,

    public void addProductToWishList(object sender, CommandEventArgs e)

    To

    protected void addProductToWishList(object sender, CommandEventArgs e)

    Hope this will work for you,


    Thanks,

    Please Mark it as answer if u find it usefull.


    Thanks,
    Shahriar Iqbal Chowdhury
    Please Mark as Answer if you find my answer helpful.
    http://www.imgalib.com
  • Re: ImageButton on Command - not firing

    06-27-2009, 2:44 PM
    • Contributor
      5,624 point Contributor
    • RatheeshC
    • Member since 04-25-2008, 2:05 PM
    • Posts 1,198

    Hi,

    Your code works well for me.. Put abreak point in your code and see if it is firing..

    or put Response.Write("event is fired"); in the function and see it is writing on the browser..

     

    Thanks

    Thanks
    Ratheesh

    Please mark it as answer if it resolves your issue.
  • Re: ImageButton on Command - not firing

    07-01-2009, 1:15 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    My ImageButton is inside a repeater which is inside an UpdatePanel.

    I have registered as postback trigger like i;ve made for other buttons that worked.

    Repeater repeaterDetalii = (Repeater)Tab1.FindControl("repeaterDetalii");
    
            foreach (RepeaterItem rptItem in repeaterDetalii.Items)
            {
                ImageButton imgBtnWishList = (ImageButton)rptItem.FindControl("imgBtnWishList");
                AsyncPostBackTrigger triggerWishList = new AsyncPostBackTrigger();
                //triggerWishList.EventName = "Click";
                triggerWishList.ControlID = imgBtnWishList.UniqueID.ToString();
                UpdatePanelSortTop.Triggers.Add(triggerWishList);
                _scriptMan.RegisterAsyncPostBackControl(imgBtnWishList);
            }


    But the problem is the same.

    My method (although i put it protected) still does not work.

    I put breakpoints before started this thread but the method is not firing...

    Any other advice?


    Repeater repeaterDetalii = (Repeater)Tab1.FindControl("repeaterDetalii");
            foreach (RepeaterItem rptItem in repeaterDetalii.Items)
            {
                ImageButton imgBtnWishList = (ImageButton)rptItem.FindControl("imgBtnWishList");
                AsyncPostBackTrigger triggerWishList = new AsyncPostBackTrigger();
                //triggerWishList.EventName = "Click";
                triggerWishList.ControlID = imgBtnWishList.UniqueID.ToString();
                UpdatePanelSortTop.Triggers.Add(triggerWishList);
                _scriptMan.RegisterAsyncPostBackControl(imgBtnWishList);
            }



  • Re: ImageButton on Command - not firing

    07-01-2009, 1:17 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Hi mudassarkhan

    I cannot use OnClick event because i need to send this CommandArgument="<%# Container.ItemIndex%>"

    because i need it in the method.

  • Re: ImageButton on Command - not firing

    07-01-2009, 1:39 PM
    Answer
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs
  • Re: ImageButton on Command - not firing

    07-01-2009, 1:59 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Yes, u are right.

    Now the image button fires the method when clicked and do a postback too.

    BUT if i put it inside the UpdatePanel and register the control like i showed above,

    it no longer fires...

    Do u know why? 

  • Re: ImageButton on Command - not firing

    07-01-2009, 2:59 PM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

     It will not fire the event unless the eent is binded to the control before the click event handler is called

    meaning every time you needto bind the event when the button is clicked and then only the event wil get called

  • Re: ImageButton on Command - not firing

    07-01-2009, 3:15 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Sorry, but i do not understand what you mean.


    So i have an UpdatePanel set to Conditional.

    Inside it, i have a Repeater which contains that Image Button

    I put:

    <asp:ImageButton ID="imgBtnWishList" runat="server" 
                            OnClick="addProductToWishList" 
                            ImageUrl="../Site/images/featured-wishlist.jpg" 
                            />


    <asp:ImageButton ID="imgBtnWishList" runat="server" 
                            OnClick="addProductToWishList" 
                            ImageUrl="../Site/images/featured-wishlist.jpg" 
                            />


    When the page is loading i register this ImageButton for the UpdatePanel as trigger:

     Repeater repeaterDetalii = (Repeater)Tab1.FindControl("repeaterDetalii");
    
            foreach (RepeaterItem rptItem in repeaterDetalii.Items)
            {
                ImageButton imgBtnWishList = (ImageButton)rptItem.FindControl("imgBtnWishList");
                AsyncPostBackTrigger triggerWishList = new AsyncPostBackTrigger();
                //triggerWishList.EventName = "Click";
                triggerWishList.ControlID = imgBtnWishList.UniqueID.ToString();
                UpdatePanelSortTop.Triggers.Add(triggerWishList);
                _scriptMan.RegisterAsyncPostBackControl(imgBtnWishList);
            }


    And when the image button is clicked is should fire this method: 


     public void addProductToWishList(object sender, EventArgs e)
        {
            ImageButton imgButton = (ImageButton)(sender);
            RepeaterItem rptParentItem = (RepeaterItem)imgButton.Parent;
    
    
            ImageButton imgBtnWishList = (ImageButton)sender;
            RepeaterItem repetear = (RepeaterItem)imgBtnWishList.Parent;
    
            if (MySession.Current.userId != 0)
            {
                int userId = MySession.Current.userId;
                wishQ.insertInWishList(userId, utils.getCategoryAliasById(category), getProductId(rptParentItem.ItemIndex));
            }
            else
            //
            {
                ModalPopupExtender loginPopUp = (ModalPopupExtender)Page.FindControl("loginPopUp");
                loginPopUp.Show();
            }
    
        }


    That's all. If i put the ImageButton outside the UpdatePanel and comment the lines that triggers it,

    it causes a postback and the method is fired.

    But within the UpdatePanel, as i showed above the method is not FIRED :((

    .


    Anyone knows WHY???


          




  • Re: ImageButton on Command - not firing

    07-01-2009, 3:22 PM
    • All-Star
      59,893 point All-Star
    • mudassarkhan
    • Member since 02-28-2008, 5:28 AM
    • Mumbai, India
    • Posts 10,551
    • TrustedFriends-MVPs

    BenHur:
    1. Repeater repeaterDetalii = (Repeater)Tab1.FindControl("repeaterDetalii");   
    2.   
    3.        foreach (RepeaterItem rptItem in repeaterDetalii.Items)   
    4.        {   
    5.            ImageButton imgBtnWishList = (ImageButton)rptItem.FindControl("imgBtnWishList");   
    6.            AsyncPostBackTrigger triggerWishList = new AsyncPostBackTrigger();   
    7.            //triggerWishList.EventName = "Click";   
    8.            triggerWishList.ControlID = imgBtnWishList.UniqueID.ToString();   
    9.            UpdatePanelSortTop.Triggers.Add(triggerWishList);   
    10.            _scriptMan.RegisterAsyncPostBackControl(imgBtnWishList);   
    11.        }

     

    If this part is present in

    !isPostBack condition remove from it so that it is executed on each request

  • Re: ImageButton on Command - not firing

    07-01-2009, 3:25 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    It's not putted in !isPostBack. It is running every time the page loads.

  • Re: ImageButton on Command - not firing

    07-01-2009, 10:36 PM
    Answer

     

    BenHur:

    So i have an UpdatePanel set to Conditional.

    Inside it, i have a Repeater which contains that Image Button

    Based on your description, it seems that you want to fire the Click event of ImageButton which host in Repeater. If this is the case, I suggest you solve it by using Repeater1_ItemCommand event instead of ImageButton_Click. Please refer to the following code:

        <form id="form1" runat="server">
        <asp:ScriptManager ID="ScriptManager1" runat="server">
        </asp:ScriptManager>
        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
         <ContentTemplate>
             <asp:Repeater ID="Repeater1" runat="server" DataSourceID="SqlDataSource1" 
                 onitemcommand="Repeater1_ItemCommand" >
             <ItemTemplate>
              <table>
                <tr><td>
                   <asp:ImageButton ID="ImageButton1" runat="server"    
                            ImageUrl="~/Images/aspdotnet.png"    
                            CommandName="Click"    
                            CommandArgument='<%#Eval("CategoryID") %>' />  
                             <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
                </td></tr>
              </table>   
             </ItemTemplate>
             </asp:Repeater>
         </ContentTemplate>
        </asp:UpdatePanel>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:connstr %>" 
            SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories]"></asp:SqlDataSource>
        </form>

    Code:

        protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
        {
            if (e.CommandName == "Click")
            {
                Label obj = e.Item.FindControl("Label1") as Label;
                obj.Text = e.CommandArgument as string;
            }
        }


     

    Gary yang - MSFT
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
  • Re: ImageButton on Command - not firing

    07-03-2009, 1:31 PM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Hi Gary,

    I took your example, put it to my page and was not running...

    I made a NEW page, only with your example and it was running !!!!

    After another hour of struggling my head with the code...i discovered the point.

    Why my function (your or that old mine) was not firing????

    Because i manually put a SELECT command to my datasource and after that i bind it to the repeater.

    And if i call repeaterDetalii.DataBind() (this my repeater which contains the ImageButton...and this repeater is a child of an updatePanel like said before), the method for the ImageButton Click is no longer fired.

    If i comment //repeaterDetalii.DataBind() it works like a charm!!

    Do you know why this things happen? I mention that i need to call the refresh of my repeater for some operation

    when changing the select!


    Please let me know if you have any solution.


     public void getCarti()
        {
            sqlDataSourceCarti.SelectCommand = select;
            sqlDataSourceCarti.DataBind();
            //repeaterDetalii.DataBind();
            repeaterImagini.DataBind();
            //aici punem curentpage ca fiind lastPage
            Session["lastPage"] = currentPage;
            UpdatePanelSortTop.Update();
        }


    Gary you can just try to put Repeater1.DataBind() in your Page_Load method, and you'll see that your example is no longer working........


  • Re: ImageButton on Command - not firing

    07-04-2009, 6:18 AM
    • Member
      12 point Member
    • BenHur
    • Member since 08-15-2008, 2:39 AM
    • Posts 53

    Not solved yet....

    Any suggestions?

Page 1 of 2 (19 items) 1 2 Next >