如何在Asp.net中通过命令参数传递多个值?

32

我有一个带有CommandArgument属性的ImageButton,该属性具有多个Eval值。当我单击其中一个时,我希望将值传递给ImageButton2_Click事件,但它不起作用,因为命令参数为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>

以下是代码后台:

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

使用 OnCommand 而不是 OnClick。http://msdn.microsoft.com/zh-cn/library/system.web.ui.webcontrols.imagebutton.oncommand.aspx - Lloyd
请检查 eval 值 <%#Eval("row[0]........etc %> 是否正确获取。 - Shebin
在 ItemDataBound 事件中,使用 JSON 对象设置 CommandArgument。 - enraged
4个回答

49

我查看了你的代码,似乎没有任何问题。请确保Image命令参数获得了值。首先检查绑定在标签上的值是否能够获得。

不过,这里是我在我的项目中使用的示例。

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

首先绑定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];
    }
}

我在那里没有使用GridView,而且e.CommandArgument会出现错误,即ClickEventArgs不包含CommandArgument的定义。 - leventkalay92
我刚刚向您展示了示例,您必须在通过CommandArgument获取值之前绑定gridview。 - Ashwini Verma

0
你可以尝试这个:
CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>'

0
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%>
//added the comment function

-1
使用ImageButton的OnCommand事件,在其中执行以下操作:
<asp:Button id="Button1" Text="Click" CommandName="Something" CommandArgument="your command arg" OnCommand="CommandBtn_Click" runat="server"/>

代码后台:

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

    }
}

2
如果你要复制代码或答案,请注明出处,比如MSDN! - Lloyd
那么,@leventkalay92,你明白如何通过命令参数传递多个值了吗?这里有一个很好的参考资料http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.button.commandargument.aspx - incomplete

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接