C# 将数据存储值作为参数传递到RowCommand asp:ButtonField
我想知道你们中是否有人能帮助我或者为我指出正确的方向 我使用asp:GridView来显示一些数据,我添加了一个ButtonField作为列之一,我要做的是将其中一个文本列的值传递给OnRowCommand函数,并使用它进行一些计算 我的网格视图:C# 将数据存储值作为参数传递到RowCommand asp:ButtonField,c#,asp.net,gridview,C#,Asp.net,Gridview,我想知道你们中是否有人能帮助我或者为我指出正确的方向 我使用asp:GridView来显示一些数据,我添加了一个ButtonField作为列之一,我要做的是将其中一个文本列的值传递给OnRowCommand函数,并使用它进行一些计算 我的网格视图: <asp:GridView ID="GridViewSchedule" runat="server" OnSorting="TaskGridView_Sorting" OnRowCommand="RowButtonClicked" AutoGe
<asp:GridView ID="GridViewSchedule" runat="server" OnSorting="TaskGridView_Sorting" OnRowCommand="RowButtonClicked" AutoGenerateColumns="False" Font-Size="11px"
CssClass="Grid" AllowSorting="True">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Name" HeaderText="Schedule Name" SortExpression="Name" />
<asp:BoundField DataField="Message" HeaderText="Message Text" SortExpression="Message">
<ItemStyle Font-Size="10px" />
</asp:BoundField>
<asp:ButtonField ButtonType="Button" CommandName="ViewDetails" Text="View" ControlStyle-CssClass="ActionButton" />
</Columns>
</asp:GridView>
所以我想知道的是,即使我必须将某列添加到网格并将其可见性设置为false,我如何传递其中一列的值?或者,您可以使用GridView的
DataKeyNames
和SelectedIndexChanged
事件来获取所需的值。数据键名称将映射到数据源中数据的列名
<asp:GridView ID="GridViewSchedule" runat="server" DataKeyNames="ID,Date,Name" OnSorting="TaskGridView_Sorting"
AutoGenerateColumns="False" Font-Size="11px" CssClass="Grid"
AllowSorting="True"
onselectedindexchanged="GridViewSchedule_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Name" HeaderText="Schedule Name" SortExpression="Name" />
<asp:BoundField DataField="Message" HeaderText="Message Text" SortExpression="Message">
<ItemStyle Font-Size="10px" />
</asp:BoundField>
<asp:ButtonField ButtonType="Button" CommandName="ViewDetails" Text="View" ControlStyle-CssClass="ActionButton" />
</Columns>
</asp:GridView>
我最终还是使用了
OnRowCommand=“RowButtonClicked”
,然后使用int val=Convert.ToInt32(GridViewSchedule.DataKeys[GridViewSchedule.SelectedIndex].Values[“ID”]代码>由您提供。谢谢你,指引我正确的方向
<asp:GridView ID="GridViewSchedule" runat="server" DataKeyNames="ID,Date,Name" OnSorting="TaskGridView_Sorting"
AutoGenerateColumns="False" Font-Size="11px" CssClass="Grid"
AllowSorting="True"
onselectedindexchanged="GridViewSchedule_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="Date" HeaderText="Date" SortExpression="Date" />
<asp:BoundField DataField="Name" HeaderText="Schedule Name" SortExpression="Name" />
<asp:BoundField DataField="Message" HeaderText="Message Text" SortExpression="Message">
<ItemStyle Font-Size="10px" />
</asp:BoundField>
<asp:ButtonField ButtonType="Button" CommandName="ViewDetails" Text="View" ControlStyle-CssClass="ActionButton" />
</Columns>
</asp:GridView>
protected void GridViewSchedule_SelectedIndexChanged(object sender, EventArgs e)
{
int val = Convert.ToInt32(GridViewSchedule.DataKeys[GridViewSchedule.SelectedIndex].Values["ID"]);
}