【问题标题】:Getting the DataKey Value in ASP.NET GridView on Button command event在按钮命令事件上获取 ASP.NET GridView 中的 DataKey 值
【发布时间】:2016-06-21 12:09:19
【问题描述】:

Gridview 已配置:

 <asp:GridView ID="gvApptList" runat="server" CssClass="fullwidth" AutoGenerateColumns="False" DataKeyNames="AppointmentID">
                <Columns>
                    <asp:BoundField DataField="Designer" HeaderText="Designer" SortExpression="Designer" />
                    <asp:BoundField DataField="AppointmentDTM" HeaderText="Appointment Date" SortExpression="AppointmentDTM" DataFormatString="{0:MM-dd-yyyy hh:mm tt}" />
                    <asp:BoundField DataField="Status" HeaderText="Status" SortExpression="Status" />
                    <asp:BoundField DataField="Disposition" HeaderText="Disposition" SortExpression="Disposition" />
                    <asp:BoundField DataField="AppointmentNotes" HeaderText="Appointment Notes" SortExpression="AppointmentNotes" />
                    <asp:ButtonField ButtonType="Button" CommandName="viewAppointment" Text="View" />
                </Columns>
            </asp:GridView>

当我单击“查看”按钮时,gvApptList_RowCommand 会触发。它的代码是:

If e.CommandName = "viewAppointment" Then
        Dim tApptID As Long
        gvApptList.SelectedIndex = e.CommandArgument
        If IsNumeric(gvApptList.DataKeys(e.CommandArgument).Value) Then
            tApptID = gvApptList.SelectedDataKey.Value
        Else
            Exit Sub
        End If
        tbAppointmentID.Text = tApptID
        DisplayInfo()
    End If

gvApptList.DataKeys(e.CommandArgument).Value 总是空无返回。我在这里想念什么?我在其他页面上有这个确切的销售代码。

【问题讨论】:

  • 您不需要将 DataKeyName 作为绑定的 DataField 吗?
  • AppointmentID 是否包含在您的基础 SELECT 查询中?另外,DataSource 和绑定代码在哪里?最好的问候,
  • 是的,约会 ID 是数据源的一部分,它从包含所有约会详细信息的 LeadAppointments 类加载。

标签: asp.net vb.net gridview datarow datakey


【解决方案1】:

与您的任务相关,在 ASP.NET GridView 控件中使用 DataKeys 的正确语法如下所示(回复:http://www.codeproject.com/Tips/225352/Nesting-GridView-control-in-ASP-NET,用 C# 编写):

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) 
{
   if (e.Row.RowType == DataControlRowType.DataRow)
   {
     // get data key from selected row
     s.SelectParameters[0].DefaultValue =Convert.ToString(((GridView)sender).DataKeys[e.Row.RowIndex].Values["AppointmentID "]);
    }
}

您应该获得与单击的命令Button 对应的Row 的引用,然后从该Row 中提取DataKeys 值。此外,请确保底层 DataSource 在其 SELECT 查询中包含 AppointmentID 字段。

与您的情况相关,它可能如下所示(参见清单 2)

清单 2。

protected void ViewButton_OnClick(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow row = btn.NamingContainer as GridViewRow;
    string strID = gvApptList.DataKeys[row.RowIndex].Values["AppointmentID"].ToString();

    int intID;
    if (String.IsNotNullOrEmpty(strID)
    {
      intID = int.Parse(strID);
    }
}

或者您可以使用TryParse()Convert()等。

希望这会有所帮助。

【讨论】:

  • 我没有嵌套网格。是显示人类可读的约会信息。
  • 嵌套与您的情况无关,只需参考核心代码 sn-p,还请包含您正在使用的事件处理程序的整个代码块(命令按钮单击)。最好的问候,
猜你喜欢
  • 2011-02-18
  • 2010-12-03
  • 2010-09-18
  • 2011-03-25
  • 1970-01-01
  • 1970-01-01
  • 2021-01-28
  • 1970-01-01
  • 2010-12-19
相关资源
最近更新 更多