2012-09-14 124 views
0

我有一個具有數據綁定項目的gridview。它綁定到SQLDATASOURCE。默認的編輯,更新工作正常,但是,我想在用戶更新行時也執行查詢。
這裏是我的aspxGridView如何從編輯中的文本框中獲取值MDOE

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" DataKeyNames="Customer_id" 
     DataSourceID="SqlDataSource1" 
     EmptyDataText="There are no data records to display." AllowPaging="True" 
     AllowSorting="True" CellPadding="4" ForeColor="#333333" GridLines="Horizontal" 
     PageSize="5" Width="873px" OnRowCommand = "RunCustomMethod" > 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:TemplateField> 
       <EditItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" 
         CommandName="myCustomUpdateMethod" Text="Update" CommandArgument = '<%# Eval("Customer_ID") %>' 
         onclientclick="return Confirm ('Are You Sure You Want To Make These Changes?')"></asp:LinkButton> 
        &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" 
         CommandName="Edit" Text="Edit" ></asp:LinkButton> 
        &nbsp; 
        <asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" 
         CommandName="Select" Text="Select"></asp:LinkButton> 
        &nbsp; 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:BoundField DataField="Customer_id" HeaderText="Customer_id" ReadOnly="True" 
       SortExpression="Customer_id" InsertVisible="False" /> 
      <asp:TemplateField HeaderText="Customer_Name" SortExpression="Customer_Name"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Customer_Name") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label1" runat="server" Text='<%# Bind("Customer_Name") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 

     </Columns> 

    </asp:GridView> 

這裏是我的C#代碼

protected void RunCustomMethod(object sender, GridViewCommandEventArgs e) 
     { 
       // Custom Method To Update Row 
      if (e.CommandName == "myCustomUpdateMethod") 
      { 
       int customerID = Convert.ToInt32(e.CommandArgument); 

        SqlConnection conn = new SqlConnection(SqlDataSource1.ConnectionString); 
       SqlCommand Cmd = new SqlCommand(); 

       try 
       { 
        conn.Open(); 

        Cmd.CommandText = "update Customers set customer_name = '" + **I WANT TO GET THE UPDATED VALUE BUT FROM WHERE SHALL I GET IT?????** + "', modified_on = getdate(), modified_by = '" + System.Environment.UserName + "' where customer_id = '" + customerID + "'"; 
        Cmd.CommandType = System.Data.CommandType.Text; 
        Cmd.Connection = conn; 

        Cmd.ExecuteNonQuery(); 
        GridView1.DataBind(); 

        // Close the Edit Mode 
        GridView1.EditIndex = -1; 

       } 
       catch (SqlException ee) 
       { 

        ValidationError.Display(ee.Message); 

       } 

       finally 
       { 
        Cmd.Dispose(); 
        conn.Close(); 
        conn.Dispose(); 

       } 
      } 

     } 

    } 

任何幫助,將不勝感激

+0

重複的問題。請參閱http://stackoverflow.com/questions/2528420/how-to-get-textbox-in-gridview-when-click-on-edit-button-using-hovermenu-and-get – Ravia

回答

2

我認爲你正在尋找的代碼是:

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer); 
string name = ((TextBox)row.FindControl("TextBox1")).Text 

Cmd.CommandText = "update Customers set customer_name = '" + name + "', modified_on = getdate(), modified_by = '" + System.Environment.UserName + "' where customer_id = '" + customerID + "'"; 
+0

關於這一點,右估計的正好。謝謝。這解決了我的問題:) –

+0

沒問題,任何時候; o)很高興幫助! – bUKaneer

3

嘗試這樣:

string newTitle = ((TextBox)GridView_Galerie.Rows[e.RowIndex].FindControl("tb_titre")).Text; 
+0

您無法直接從EventArgs獲取此事件的RowIndex - 您的解決方案適用於OnRowDataBound,但不適用於OnRowCommand – bUKaneer

0

您正在聽錯了事件。如果您想在數據庫更新之前獲取值,則應該聽取RowUpdating事件。

參見:RowUpdating

protected void TaskGridView_RowUpdating(object sender, GridViewUpdateEventArgs e) 
    {  
    //Retrieve the table from the session object. 
    DataTable dt = (DataTable)Session["TaskTable"]; 

    //Update the values. 
    GridViewRow row = TaskGridView.Rows[e.RowIndex]; 
    dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text; 
    dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked; 

    //Reset the edit index. 
    TaskGridView.EditIndex = -1; 

    //Bind data to the GridView control. 
    BindData(); 
    } 

同樣存在RowUpdated情況下,如果你想要的值在數據庫中更新後運行查詢。

相關問題