[英]VS2010 asp.net : capturing an invisible GridView column value
如何在不使GridView列值可見的情況下將其傳遞給后面的代碼? 在下面的代碼中,我想捕獲ContactID而不使其在GridView中可見。
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="SqlDataSource1"
OnRowCommand="_OnRowCommand"
>
<Columns>
<asp:BoundField DataField="ContactID" HeaderText="ContactID"
SortExpression="ContactID" InsertVisible="False" ReadOnly="True" />
.....other columns in the grid....
<asp:ButtonField ButtonType=Button CommandName="Remove" Text="Remove" />
</Columns>
</asp:GridView>
我想捕獲ContactID如下。 我無法做到在GridView中將其設置為visible = true。
if (e.CommandName.Equals("Remove"))
{
int index = Convert.ToInt32(e.CommandArgument);
int ContactID = Convert.ToInt32(GridView1.Rows[index].Cells[0].Text.Trim());
}
請幫忙。
謝謝..
您想要什么以及代碼在做什么是兩回事。
我認為您正在尋找的是:
String ContactID = e.Row.Cells(0).Text;
但是,我提出了另一種方法:
您可以在Button上使用CommandArgument。 這意味着您正在通過Button本身傳遞參數。 您無需從任何其他控件獲取參數。 為此,您可以這樣做:
<asp:ButtonField ButtonType=Button CommandName="Remove" Text="Remove" CommandArgument='<%# Eval("ContactID") %>' />
然后您的代碼如下所示:
if (e.CommandName.Equals("Remove"))
{
int ContactID = Convert.ToInt32(e.CommandArgument);
}
這是帶有CommandArguments的GridView的一個很好的示例。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.