【问题标题】:Checked in Gridview, Button Click for Assign Value to textbox在Gridview中选中,单击按钮以将值分配给文本框
【发布时间】:2014-04-28 07:34:24
【问题描述】:

我尝试完成按钮单击的动作以实现gridview中的选中行,下面是我的asp.net代码

如果我单击编辑按钮,我想将 gridview 行、列值分配到它们的文本框中.. 怎么可能用代码解释`

 <form id="form1" runat="server">
<div>
    &nbsp;</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>
                    &nbsp;
                </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>
    &nbsp;<br />
    <br />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp;<asp:Button ID="Button1" runat="server" Text="Delete" OnClick="Button1_Click" />
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
    &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<br />
    <br />
    &nbsp;
    <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 列.. 但它没有正确执行

【问题讨论】:

  • 你的 btnEdit 按钮在哪里
  • 感谢您的回复格兰特·温尼,我可以知道它不起作用
  • 嗨 Ganesh Ty 回复 Ganesh_Devlekar 它在代码上,我现在编辑看看
  • @BAP : GridView 中的总列数是多少?你得到什么错误?您在这里谈论的是哪个 GridView GridView 或 GridView1(因为在 asp 代码中您有 GridView 并且在它后面的代码中说 GridView1)?
  • 嗨 user3240361,我现在只编辑了它的 GridView .. 总数。上校是 3..

标签: c# asp.net gridview checkbox buttonclick


【解决方案1】:

后面的代码。我正在创建一个数据集以绑定到 gridview 并在其中放置一些虚拟值。如果您不确定如何将数据绑定到网格视图,请在 Google 上搜索,这里有很多示例! :)

 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" />

【讨论】:

  • 错误 1 ​​无法将 [] 索引应用于“System.Data.DataTable”类型的表达式 错误2 无法将 [] 索引应用于“System.Data.DataTable”类型的表达式“这些是否出现错误”错误 3 无法将 [] 索引应用于“System.Data.DataTable”类型的表达式当前上下文中不存在名称“NewRow”
  • Ty 这么多 Garethb .. 再次显示相同的错误 .. 我正在尝试解决 .. 如果你知道请告诉我
  • 对不起,我误解了你的问题,我以为你试图将文本框值添加到 gridview,但看起来你想将 gridview 值添加到文本框?
  • 是的,这就是完美的 Garethb
  • 你能帮我找到代码吗..我想要基本的笔记,我可以学习一下
【解决方案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; 
             }
        }

【讨论】:

  • 嗨,Shreyas Tg,谢谢你在这里..它没有执行
  • 你遇到了什么异常?
  • 错误 1 ​​无法将 [] 索引应用于“System.Data.DataTable”类型的表达式 错误2 无法将 [] 索引应用于“System.Data.DataTable”类型的表达式“这些是否出现错误“错误3无法使用[]将索引应用于'System.Data.DataTable'类型的表达式在当前上下文中不存在名称'NewRow'对于我现在编辑的上述代码“这是错误”
  • @ShreyasTg:您错误地获取了单元格索引。它必须是 0,1,2,因为他的 girdView 在一行中只有三个单元格。
  • Shreyas Tg 不执行
【解决方案3】:

如果您尝试将选中 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;
                }
            }
        }

并且最重要的是,请将 aspx 中的 GridView 命名为 'GridView1',因为您不能有一个 ID 的类名

【讨论】:

  • 不,它也没有.. 我想将 gridview 值添加到文本框 user3240361
  • 那你为什么把帖子改成不同的意思?
  • @user3240361 : 是的,我试试它不执行
  • 在这种情况下,您需要对错误或您现在面临的问题发表评论,然后我才能为您提供帮助。
【解决方案4】:

我想将“gridview 行值”添加到文本框,所以这个问题没有答案;分享一些要阅读的东西,对此

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 2014-09-24
    • 2021-01-27
    • 2018-03-01
    • 1970-01-01
    • 2012-01-24
    相关资源
    最近更新 更多