2011-12-19 65 views
0

我在.aspx頁面中一個方格,就像nameidrole三個文本字段,也有一個編輯按鈕,你點擊該行的值將在txtName的,txtid顯示,txtroll如何更新gridview中的值?

該值我想更新並插入數據庫。

回答

4
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) 
{ 
    if (e.CommandName == "Update") 
    { 
     string uname = ""; 
     int index = Convert.ToInt32(e.CommandArgument); 
     GridViewRow row = GridView1.Rows[index]; 
     TextBox txtbox1_val = (TextBox)row.FindControl("TxtChangeActive"); 
     uname = Server.HtmlDecode(row.Cells[1].Text.ToString()); 
     string upd_query = "update login set active='" + txtbox1_val.Text + "' where uname='" + uname + "' "; 
     SqlConnection con = new SqlConnection(conn); 
     con.Open(); 
     SqlCommand cmd = new SqlCommand(upd_query, con); 
     cmd.ExecuteNonQuery(); 

     string query_login = "select * from Login"; 
     SqlDataAdapter da_login = new SqlDataAdapter(query_login, con); 
     DataSet ds_login = new DataSet(); 
     da_login.Fill(ds_login); 
     GridView1.DataSource = ds_login; 
     GridView1.DataBind(); 
    } 
} 
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) 
{ 

} 

在設計頁面

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowCommand="GridView1_RowCommand" 
    AlternatingRowStyle-BackColor="#006699" 
     AlternatingRowStyle-ForeColor="#FFFFFF" onrowupdating="GridView1_RowUpdating"> 
    <Columns > 

    <asp:BoundField HeaderText="Name" DataField="uname" /> 
    <asp:BoundField HeaderText="Pass" DataField="upass"/> 
    <asp:TemplateField> 
    <HeaderTemplate>Active</HeaderTemplate> 
    <ItemTemplate > 
    <asp:TextBox ID="TxtChangeActive" runat="server" Text='<%#Eval("active")%>'></asp:TextBox> 
    </ItemTemplate> 
    </asp:TemplateField> 
    <asp:ButtonField CommandName="Update" Text="Update" /> 
    </Columns> 
    </asp:GridView> 
+1

我知道代碼的更新代碼BT我不能在同一個頁面,無法更新詳細文本費爾德獲取的價值:我在ASPX到費爾德頁面如用戶名,密碼和提交按鈕後,將在aspx頁shw在網格中有更新按鈕,如果你點擊相同的aspx將出現哪個hv用戶名,密碼feild如果你在這裏更改值,值將會在數據庫中更新,這是我的工作,如果你能向我發送代碼 – 2011-12-21 07:50:56