GridView的配置:獲取在ASP.NET GridView中DataKey值的按鈕命令事件
<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? – Kramb
也嘗試過。 – RobWaibel
是否將AppointmentID包含在基礎SELECT查詢中?另外,DataSource和綁定代碼在哪裏?最好的問候, –