32

I have ImageButton with CommandArgument attribute which is having multiple Eval value. When I click one of them I want to pass values to ImageButton2_Click event but it does not work because Command arguments is null.

<div class="sag-re-icerik" id="icerik2" runat="server">Lorem ipsum dolor sit amet, consectetur commodo et convallis et, auctor viverra metus. Aenean pharetra, arcu nec viverra mollis, turpis neque feugiat massa, non dapibus neque nunc ac orci. </div>
    <div class="oy-verme">
        <div class="yildiz"><asp:ImageButton ID="ImageButton4" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left:6px; float:left;  "    commandArgument='<%#Eval("sdasdas") + ","+Eval("fafasfa") %>' /></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton5" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton5_Click" Width="20px"  style="position: relative; top: 13px; left:8px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>'/></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton6" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left:10px ; float:left; " commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="yildiz"><asp:ImageButton ID="ImageButton3" runat="server" Height="19px"  ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px" style="position: relative; top:13px; left:12px ; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="yildiz"> <asp:ImageButton ID="ImageButton2" runat="server" Height="19px" ImageUrl="~/images/yildiz.png" onclick="ImageButton2_Click" Width="20px"  style="position: relative; top: 13px; left: 14px; float:left;" commandArgument='<%#Eval("row[0].ToString()") + ","+Eval("row[1].ToString()") %>' /></div>
        <div class="oy-sil"><img src="images/oy-sil.png" width="11" height="13" style="position: relative; top: 30px; " /></div>
    </div>
</div>

This is the code-behind:

protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
{
    ImageButton objImage = (ImageButton)sender;

    string[] commandArgs = objImage.CommandArgument.ToString().Split(new char[] { ',' });
    string id = commandArgs[0];
    string text = commandArgs[1];


    //  string s= Imageid.UniqueID.ToString();
    //this.baslik2.Text = s;
}
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
leventkalay92
  • 573
  • 3
  • 11
  • 28
  • Use OnCommand not OnClick. http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.imagebutton.oncommand.aspx – Lloyd Mar 22 '12 at 13:08
  • Please check the eval values `<%#Eval("row[0]........etc %>` which are retrieving correctly. – Shebin Mar 23 '12 at 06:06
  • In then event ItemDataBound set CommandArgument with a json object – enraged Feb 24 '22 at 10:52

4 Answers4

49

I checked your code and seems to be no problem at all. please make sure Image commandArgument getting value. check it first binding in label whether you are getting value.

However, here is sample which I'm using in my project

<asp:GridView ID="GridViewUserScraps" ItemStyle-VerticalAlign="Top" AutoGenerateColumns="False" Width="100%" runat="server" OnRowCommand="GridViews_RowCommand" >
        <Columns>
            <asp:TemplateField SortExpression="SendDate">
                <ItemTemplate>
                <asp:Button ID="btnPost" CssClass="submitButton" Text="Comment" runat="server" CommandName="Comment" CommandArgument='<%#Eval("ScrapId")+","+ Eval("UserId")%>' />

                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>

first bind the GridView.

public void GetData()
{
   //bind ur GridView
   GridViewUserScraps.DataSource = dt;
   GridViewUserScraps.DataBind();
}

protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "Comment")
    {
        string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
        string scrapid = commandArgs[0];
        string uid = commandArgs[1];
    }
}
Ashwini Verma
  • 7,477
  • 6
  • 36
  • 56
  • I don't use the gridview there and e.CommandArgument gives error which is that clickeventargs does not contatin a definition for command argument – leventkalay92 Mar 23 '12 at 10:59
  • I have just shown you the sample you have to bind gridview before getting the value through CommandArgument. – Ashwini Verma Mar 26 '12 at 07:47
0

You can try this:

CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>'
Leigh
  • 28,765
  • 10
  • 55
  • 103
mcirak
  • 11
0
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%>
//added the comment function
demongolem
  • 9,474
  • 36
  • 90
  • 105
-1

Use OnCommand event of imagebutton. Within it do

<asp:Button id="Button1" Text="Click" CommandName="Something" CommandArgument="your command arg" OnCommand="CommandBtn_Click" runat="server"/>

Code-behind:

void CommandBtn_Click(Object sender, CommandEventArgs e) 
{    
    switch(e.CommandName)
    {
        case "Something":
            // Do your code
            break;
        default:              
            break; 

    }
}
abatishchev
  • 98,240
  • 88
  • 296
  • 433
incomplete
  • 548
  • 1
  • 7
  • 12
  • 2
    If you are going to copy code or answers give credit, like to MSDN! – Lloyd Mar 22 '12 at 13:28
  • So, @leventkalay92 you get the point how to pass multiple values through command args? Heres a good reference http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx – incomplete Mar 22 '12 at 13:54