选中“网格视图”,单击“赋值”文本框中的按钮
本文关键字:文本 按钮 赋值 网格 视图 网格视图 选中 单击 | 更新日期: 2023-09-27 17:51:13
我尝试完成按钮点击的动作,以实现gridview中的选中行,下面是我的asp.net代码
如果我点击编辑按钮,我想分配gridview行,列值到他们的文本框..怎么可能用代码'
来解释呢? <form id="form1" runat="server">
<div>
</div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" Width="505px" OnSelectedIndexChanged="GridView_SelectedIndexChanged" >
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
<EditItemTemplate>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField>
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<EditRowStyle BackColor="#999999" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
<br />
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
</form>
c# public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data source =;Database = ;Integrated security =true");
SqlCommand cmd = new SqlCommand("select * from Empdetails", con);
con.Open();
GridView1.DataSource = cmd.ExecuteReader();
GridView1.DataBind();
con.Dispose();
con.Close();
}
protected void GridView_SelectedIndexChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[0].Text;
TextBox2.Text = row.Cells[1].Text;
TextBox3.Text = row.Cells[2].Text;
}
}
}
}
}
这里使用的是Empid为主列和其他2列。但是它不能正确执行
你可以试试这个,也许是可能的答案。
for (int i = 0; i <= GridView1.Rows.Count - 1; i++)
{
GridViewRow row = GridView1.Rows[i];
CheckBox Ckbox = (CheckBox)row.FindControl("CheckBox1");
if (Ckbox.Checked == true)
{
TextBox1.Text = row.Cells[1].Text;
TextBox2.Text = row.Cells[2].Text;
TextBox3.Text = row.Cells[3].Text;
}
}
如果您试图将选中CheckBox所在行的GridView(命名为GridView1)单元格值分配给三个文本框,则下面是修复方法。
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[0].Text;
TextBox2.Text = row.Cells[1].Text;
TextBox3.Text = row.Cells[2].Text;
}
}
}
和最重要的是请命名GridView在aspx作为'GridView1',因为你不能有一个类名ID
我想添加'gridview行值'到文本框,所以没有这个问题的答案;分享一些值得阅读的东西
代码落后。我正在创建一个数据集绑定到gridview,并把一些虚拟值在那里。如果你不确定如何将数据绑定到gridview,谷歌一下,有很多例子!:)
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var emps = new DataTable("Employee");
emps.Columns.Add(new DataColumn() {ColumnName = "EmpId", DataType = Type.GetType("System.Int32")});
emps.Columns.Add(new DataColumn() {ColumnName = "Name", DataType = Type.GetType("System.String")});
emps.Columns.Add(new DataColumn() {ColumnName = "Address", DataType = Type.GetType("System.String")});
var dataSet = new DataSet();
dataSet.Tables.Add(emps);
var row = emps.NewRow();
row["EmpId"] = 1000;
row["Name"] = "John";
row["Address"] = "1 Smith St";
emps.Rows.Add(row);
GridView1.DataSource = dataSet;
GridView1.DataBind();
}
}
protected void Button2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
if ((row.FindControl("CheckBox1") as CheckBox).Checked)
{
TextBox1.Text = row.Cells[1].Text;
TextBox2.Text = row.Cells[2].Text;
TextBox3.Text = row.Cells[3].Text;
}
}
}
protected void Button1_Click(object sender, EventArgs e)
{
throw new NotImplementedException();
}
这是你的aspx页面的代码
<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="GridView" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</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" OnClick="Button2_Click" />