C# 如何在 Asp.net 中通过命令参数传递多个值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9822827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass multiple values through command argument in Asp.net?
提问by leventkalay92
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.
我有带有 CommandArgument 属性的 ImageButton,它有多个 Eval 值。当我单击其中之一时,我想将值传递给 ImageButton2_Click 事件,但它不起作用,因为命令参数为空。
<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;
}
采纳答案by Ashwini Verma
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.
我检查了您的代码,似乎完全没有问题。请确保 Image commandArgument 获得值。检查它首先绑定在标签中是否获得价值。
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.
首先绑定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];
}
}
回答by incomplete
Use OnCommand event of imagebutton. Within it do
使用图像按钮的 OnCommand 事件。在它里面做
<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;
}
}
回答by mcirak
You can try this:
你可以试试这个:
CommandArgument='<%# "scrapid=" + Eval("ScrapId")+"&"+"UserId="+ Eval("UserId")%>'
回答by Malkiat Singh
CommandArgument='<%#Eval("ScrapId").Tostring()+ Eval("UserId")%>
//added the comment function

