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总是一无所获。我在这里错过了什么?我在其他页面上有这个确切的销售代码。
答案 0 :(得分:0)
与您的任务相关,使用ASP.NET DataKeys
控件中的GridView
的正确语法如下所示(re: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 "]);
}
}
您应该获得与点击的Row
命令对应的Button
的引用,然后从DataKeys
中提取Row
值。另外,请确保基础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()
等
希望这可能会有所帮助。