如何将数据密钥从网格视图传递到服务器端 [英] How to Pass Data key from grid view to server side
本文介绍了如何将数据密钥从网格视图传递到服务器端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好:)
我有一个网格视图,其中每行都包含一个按钮
当我在某行中按下按钮时,我想对网格的数据键(主键)进行某些操作
如何在C#代码中检索用于已使用行的数据键?
谢谢大家:)
Hello :)
I have a grid view, which contain a button in each row
I want to do something with the data key (the primary key ) of the grid when I press a button in some row
can I retrieve the data key for used row to use it in my C# code ?
thank you all :)
推荐答案
您应将字段绑定到按钮的CommandArgument
中.
例如在gridview上,可以有一个像这样的按钮:
You should bind the field into theCommandArgument
of the button.
for example on the gridview, you can have a button like this one:
<asp:TemplateField HeaderText="custom column">
<asp:Button runat="server" CommandArgument='<%# Eval("UserID") %>' OnClick="OnMyButtonClick"/>
</asp:TemplateField>
请注意,此按钮位于TemplateField
上.并且正在使用评估将用户ID放入CommandArgument
属性.
现在,在后面的C#代码中,您可以执行以下操作:
note this button is on a TemplateField
. And is using evaluation the UserID into the CommandArgument
property.
Now in the C# code behind you can do this:
protected void OnMyButtonClick(object sender, EventArgs e)
{
Button b = sender as Button;
if (b!=null)
{
var hey = b.CommandArgument;//this is my value!
//do something now!
}
}
还有瞧..
希望对您有所帮助.
And voila..
Hope it helps.
这篇关于如何将数据密钥从网格视图传递到服务器端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文