我不得不问一下C#中的一个问题,Asp.net ...我为empid创建了3个文本框,名称和地址
如果我点击“编辑按钮”并在文本框中手动输入empid。我想分配其他两个值name&地址到他们的文本框。然后我使用更新按钮更新
任何人都可以用代码教我吗?
//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,我想要其他值名称&amp;地址ll分配给文本框
答案 0 :(得分:0)
<强>步骤:强>
在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
将此属性AutoGenerateSelectButton="True"
添加到设计页面中的gridview。
<asp:GridView ID="GridView1" runat="server"
AutoGenerateColumns="False"
AutoGenerateSelectButton="True"
CellPadding="4" >
</asp:GridView>
在Grid的SelectedIndexChanged上获取RowIndex,以便我们获取所选行的行值。
Protected Sub GridView1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles GridView1.SelectedIndexChanged
LoadValues(GridView1.SelectedRow.RowIndex)
End Sub
然后将网格值加载到相应的文本框中。
并更改您想要的值,但不要更改ID(强烈推荐)
快乐编码.. !! :)