2014-03-19 70 views
0

我要問有關C#中的問題,Asp.net ...我在3文本框爲EMPID,姓名和地址分配的GridView值文本框中

如果我點擊「編輯按鈕」與在文本框中手動輸入empid。我想分配其他兩個值名稱&地址到他們的文本框。然後我使用更新按鈕更新

任何人都可以教我與代碼?

//Asp.net 

<form id="form1" runat="server"> 
<div> 
    <asp:Label ID="Label1" runat="server" Text="Empid" Width="50px"></asp:Label> 
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="Label2" runat="server" Text="Name" Width="50px"></asp:Label> 
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox> 
    <br /> 
    <asp:Label ID="Label3" runat="server" Text="Address" Width="50px"></asp:Label> 
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox> 
    <br /> 
    <br /> 
    <br /> 
    <br /> 
    <asp:GridView ID="GridView1" runat="server"> 
    </asp:GridView> 
    <br /> 

</div> 
    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="update " /> 
    <asp:Button ID="Button2" runat="server" Text="Edit" Width="61px" /> 
</form> 



//Cs for update button 


    SqlConnection conn = new SqlConnection("Data Source=s\\SQLEXPRESS;Initial catalog = sample;Integrated security=True"); 
    SqlCommand cmd = new SqlCommand("UPDATE empdetails SET Name='" + TextBox2.Text + "',Address='" + TextBox3.Text + "' WHERE Empid ='" + TextBox1.Text + "'", conn); 
    conn.Open(); 
    cmd.ExecuteNonQuery(); 
    cmd.CommandText = "SELECT * FROM Empdetails"; 
    GridView.DataSource = cmd.ExecuteReader(); 
    GridView.DataBind(); 
    TextBox1.Text = ""; 
    TextBox2.Text = ""; 
    TextBox3.Text = ""; 
    conn.Close(); 

我這樣的代碼,如果我編輯按鈕,用輸入EMPID,我想其他值命名&地址會分配到文本框中

+0

哈哈大聲笑,告訴我們你試過了什麼:D –

+1

請發表一些你已經嘗試過的代碼。 –

+0

通過ajax調用發送empid到服務器頁面,從服務器獲取數據名稱和地址,並在成功時顯示它。 – SeeTheC

回答

0

步驟:

  1. 在GridView中獲取員工詳細信息。

    Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load 
        GetEmpDetails() 
    End Sub 
    Private Sub GetEmpDetails() 
    SqlConnection conn = new SqlConnection("Data Source=s\\SQLEXPRESS;Initial catalog = sample;Integrated security=True"); 
    
    conn.Open(); 
    
    cmd.CommandText = "SELECT * FROM Empdetails"; 
    GridView.DataSource = cmd.ExecuteReader(); 
    GridView.DataBind(); 
    End Sub 
    
  2. 將此屬性AutoGenerateSelectButton="True"添加到設計頁面的gridview中。

    <asp:GridView ID="GridView1" runat="server" 
          AutoGenerateColumns="False" 
          AutoGenerateSelectButton="True" 
          CellPadding="4" > 
    </asp:GridView> 
    
  3. 對電網的的SelectedIndexChanged得到的rowIndex,這樣我們將獲得選擇的行的行值。

    Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged 
        LoadValues(GridView1.SelectedRow.RowIndex) 
    End Sub 
    
  4. 然後將網格值加載到相應的文本框中。

  5. ,並更改你想,但不改變ID值(強烈推薦)

  6. 然後點擊UpdateButton。在那裏調用更新查詢。
  7. 重新加載網格值並將其綁定。這樣你就可以立即看到變化。

Happy Coding .. !! :)

+0

謝謝你的回覆,我沒有在這裏使用複選框 – BAP

+0

@BAP我也沒有提到複選框..! –

+0

@BAP你想要這個過程嗎?你想達到什麼目的? –