2011-04-17 155 views
2

喜歡在標題+如何處理按鈕點擊哪個按鈕在GridView頁腳也?如何從文本框中的gridview頁腳c#中獲取值?

文件的.aspx看起來這

<asp:GridView ID="GridView1" runat="server" 
    AutoGenerateColumns="False" 
     CellPadding="4" DataKeyNames="id" EnableModelValidation="True" 
     ForeColor="#333333" GridLines="None" 
     onrowcancelingedit="GridView1_RowCancelingEdit1" 
     onrowediting="GridView1_RowEditing1" 
     onrowupdating="GridView1_RowUpdating1" AllowPaging="True" 
     onrowdeleting="GridView1_RowDeleting" 
     onpageindexchanging="GridView1_PageIndexChanging" Height="322px" 
     ShowFooter="True" onselectedindexchanged="GridView1_SelectedIndexChanged" > 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:CommandField ShowEditButton="True" /> 
      <asp:BoundField DataField="id" HeaderText="ID" Visible="False" /> 
      <asp:BoundField DataField="Name" HeaderText="Name" /> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:TextBox ID="txtName" runat="server" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="LastName" HeaderText="LastName" /> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:TextBox ID="txtLastName" runat="server" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="DriveLic" HeaderText="DriveLicense" /> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:TextBox ID="txtDriveLicense" runat="server" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="country" HeaderText="Country" /> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:TextBox ID="txtWoj" runat="server" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ShowDeleteButton="True" /> 
      <asp:TemplateField> 
       <FooterTemplate> 
        <asp:Button ID="ButtonOK" Text="Confirm" runat="server" /> 
       </FooterTemplate> 
      </asp:TemplateField> 
     </Columns> 
+0

你試過了什麼? – 2011-04-17 15:00:01

+0

我必須從文本框中插入數據,我已經在頁腳的GridView加上一個按鈕還有,當我點擊這個按鈕時,它應該插入到數據庫值從這個文本框 – harry180 2011-04-17 15:04:16

回答

3

裏面你GridView_RowCommand事件中,你可以通過GridView1.FooterRow.FindControl方法訪問頁腳控制。

protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName.Equals("Insert", StringComparison.OrdinalIgnoreCase)) 
    { 
     TextBox txtSomeNewValue = ((TextBox)GridView1.FooterRow.FindControl("txtSomeNewValue")); 
     string theTextValue = txtSomeNewValue.Text; 
    } 
} 

更新:在一個if塊來檢查,如果該commandname是你期待什麼包裹的代碼。這個事件也用於刪除,編輯等,所以如果你不打算將它包含在這個事件中,你最終可能會運行你沒有打算的事件的代碼。

+0

這就是我想知道的:)但如何處理事件按鈕點擊?因爲它與此文本框位於同一位置 – harry180 2011-04-17 15:12:57

+1

在GridView頁腳內放置一個「LinkBut​​ton」,設置CommandName =「Insert」,然後通過單擊按鈕將觸發RowCommnad事件。 – 2011-04-17 15:23:16

+0

但我必須使用正常的按鈕:( – harry180 2011-04-17 15:27:54

相關問題