gridview中命令参数属性的用途是什么 [英] what is the use of command arguments properity in gridview

查看:74
本文介绍了gridview中命令参数属性的用途是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CommandArgument =<%#Container.DataItemIndex%>

解决方案

查看此链接



GridView.RowCommand Event [msdn] [ ^ ]


参考这些链接。



http://stackoverflow.com/questions/4050478/what-is-the -purpose-of-command-name-and-command-argument-for-a-control-example-b [ ^ ]



http://stackoverflow.com/questions/13363061/asp-net-linkbutton-commandname-and-commandargument [ ^ ]


< blockquote>命令参数有以下用途。



假设您正在使用gridview,其中包含三个按钮,CommandName =Custom属性set.Now您正在处理按钮点击gridview这样的RowCommand事件







< asp:GridView ID =GridView2runat =serverAutoGenerateColumns =FalseOnRowCommand =GridView1_RowCommand>

< Columns>

< asp:TemplateField>

< ItemTemplate>

< table>

< tr>

< td width =200pxalign =left>

< asp:Button ID =Button1runat =serverText =EditCommandName =Custom/> ;

< asp:Button ID =Button2runat =serverText =EditCommandName =Custom/>

< asp:Button ID =Button3runat =serverText =EditCommandName =Custom/>

< / td>

< / tr>

< / table>

< / ItemTemplate>

< / asp:TemplateField>

< /列>

< / asp:GridView>





// protected void GridView1_RowCommand(对象发送者) ,GridViewCommandEventArgs e)

// {

// if(e.CommandName ==Custom)

// {

// //代码来处理但是吨点击

//}

//}

现在的问题是你如何确定哪个按钮点击引发了RowCommand事件,因为两个三个按钮具有相同的CommandName。

所以为了避免这种混乱,我们将使用这样的CommandArgument





< asp:Button ID =Button1runat =serverText =EditCommandName =CustomCommandArgument =btn1/>

< asp:Button ID =Button2 runat =serverText =EditCommandName =CustomCommandArgument =btn2/>

< asp:Button ID =Button3runat =serverText =编辑CommandName =自定义CommandArgument =btn3/>







和代码文件







// protected void GridView1_RowCommand(object sender,GridViewCommandEventArgs e)

// {

// if(e.CommandName ==Custom) &安培;&安培; e.CommandArgument ==btn1)

// {

//}

// if(e.CommandName ==Custom && e.CommandArgument ==btn2)

// {

//}

// if(e。 CommandName ==Custom&& e.CommandArgument ==btn3)

// {

//}

/ /}





这就是为什么我们使用Command Argument


CommandArgument="<%#Container.DataItemIndex%>"

解决方案

See this link

GridView.RowCommand Event [msdn][^]


Refer these links.

http://stackoverflow.com/questions/4050478/what-is-the-purpose-of-command-name-and-command-argument-for-a-control-example-b[^]

http://stackoverflow.com/questions/13363061/asp-net-linkbutton-commandname-and-commandargument[^]


Command Argument is having the following use.

Suppose you are having gridview which is containing three button with CommandName="Custom" property set.Now you are handling button click on gridview RowCommand event like this



<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<table>
<tr>
<td width="200px" align="left">
<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Custom" />
<asp:Button ID="Button2" runat="server" Text="Edit" CommandName="Custom" />
<asp:Button ID="Button3" runat="server" Text="Edit" CommandName="Custom" />
</td>
</tr>
</table>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


// protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
// {
// if (e.CommandName=="Custom")
// {
// //code to handle the button click
// }
// }
now the question is how will you identify which button click raised the RowCommand Event because both the three button's having the same CommandName.
so for avoiding this confusion we will use CommandArgument like this


<asp:Button ID="Button1" runat="server" Text="Edit" CommandName="Custom" CommandArgument="btn1"/>
<asp:Button ID="Button2" runat="server" Text="Edit" CommandName="Custom" CommandArgument="btn2"/>
<asp:Button ID="Button3" runat="server" Text="Edit" CommandName="Custom" CommandArgument="btn3"/>



and the code file



//protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
//{
// if (e.CommandName == "Custom" && e.CommandArgument=="btn1")
// {
// }
// if (e.CommandName == "Custom" && e.CommandArgument == "btn2")
// {
// }
// if (e.CommandName == "Custom" && e.CommandArgument == "btn3")
// {
// }
//}


This is why we use Command Argument


这篇关于gridview中命令参数属性的用途是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆