2016-03-07 75 views
1

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的總是回來如無物。我在這裏錯過了什麼?我有這個確切的銷售代碼在其他頁面上工作。

+0

您是否需要將DataKeyName作爲綁定的DataField? – Kramb

+0

也嘗試過。 – RobWaibel

+0

是否將AppointmentID包含在基礎SELECT查詢中?另外,DataSource和綁定代碼在哪裏?最好的問候, –

回答

0

相關的你的任務,在ASP.NET中使用DataKeys正確的語法GridView控制如下所示(重: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,然後從該中提取DataKeys值。另外,請確保底層DataSource在其SELECT查詢中包含AppointmentID字段。

有關你的情況下,它可能看起來像下面的(參見清單2)

清單2.

​​

,或者您可以使用TryParse()Convert()

但願這幫幫我。

+0

我不是嵌套網格。顯示可讀的預約信息。 – RobWaibel

+0

嵌套與您的案例無關,只需引用核心代碼片段,並且還請包括您正在使用的事件處理程序(Command Button Click)的整個代碼塊。最好的祝福, –

相關問題