How to pass multiple values through command argument in Asp.net?

前端 未结 4 1350
暖寄归人
暖寄归人 2021-02-05 02:29

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

相关标签:
4条回答
  • 2021-02-05 02:41

    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; 
    
        }
    }
    
    0 讨论(0)
  • 2021-02-05 02:50
    CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%>
    //added the comment function
    
    0 讨论(0)
  • 2021-02-05 03:00

    You can try this:

    CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>'
    
    0 讨论(0)
  • 2021-02-05 03:04

    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];
        }
    }
    
    0 讨论(0)
提交回复
热议问题