ASP中如何用Button传递两个值给后台?
ASP中用Button传递两个值给后台的方法:
1、在CommandArgument中用逗号分隔要传到后台的两个参数:
<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>
2、C#后台接收到button传递的参数后的处理方法:
protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
if (e.CommandName == "Comment")
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' }
string scrapid = commandArgs[0]; //传递参数1
string uid = commandArgs[1];//传递参数2
1、在CommandArgument中用逗号分隔要传到后台的两个参数:
<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>
2、C#后台接收到button传递的参数后的处理方法:
protected void GridViews_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Comment")
{
string[] commandArgs = e.CommandArgument.ToString().Split(new char[] { ',' });
string scrapid = commandArgs[0]; //传递参数1
string uid = commandArgs[1];//传递参数2
}
}