【问题标题】:How to Copy from Label to TextBox inside GridView如何在 GridView 中从标签复制到文本框
【发布时间】:2020-05-11 23:54:07
【问题描述】:

我是c# .Net Gridview,当我尝试将标签单元格中的值复制到Gridview 中的TextBox 单元格时,TextBox 单元格变为Label !

_RowCommand

row.Cells[2].Text = row.Cells[1].Text;

row.Cells[2].Text 从 TextBox 分配到 Label 后更改!

知道原因和解决方案吗?

备注:我加了代码解释。

.aspx 代码:

<asp:GridView ID="GridView_Names" runat="server" 
                AutoGenerateColumns="False" DataKeyNames="Num" 
                OnRowDataBound="GridView_Names_RowDataBound"
                OnRowCommand="GridView_Names_RowCommand"
                AllowPaging="false"  >
                <Columns>
                    <asp:BoundField DataField="Num" >
                    </asp:BoundField>
                    <asp:BoundField DataField="Name1" >
                    </asp:BoundField>
                    <asp:TemplateField >
                        <ItemTemplate >
                            <asp:TextBox ID="Name2" runat="server" />
                        </ItemTemplate>
                    </asp:TemplateField> 
                    <asp:TemplateField HeaderText="Google" Visible="false" >
                        <ItemTemplate>
                            <asp:Button ID="Button_Copy" 
                                CommandArgument="<%# ((GridViewRow) Container).RowIndex %>" 
                                CommandName="OnClickButton_Copy" runat="server"/>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns></asp:GridView>

后面的代码:

    protected void GridView_Names_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
            e.Row.Cells[2].Text = e.Row.Cells[1].Text; // Cells[2] changes from TextBox to Label !!!
    }
}
protected void GridView_Names_RowCommand(object sender, GridViewCommandEventArgs e)
{
    if (e.CommandName == "OnClickButton_Copy")
    {
        int index = Convert.ToInt32(e.CommandArgument);

        GridViewRow row = GridView_Names.Rows[index];
        GridView_Names.Rows[index].Cells[2].Text = row.Cells[1].Text; // Cells[2] changes from TextBox to Label !!!
    }

【问题讨论】:

  • 你能澄清发生了什么吗?逐步了解正在发生的事情。
  • 谢谢。我刚刚添加了更多解释的代码。

标签: c# .net gridview


【解决方案1】:

我应该使用这个命令在 Gridview 中找到文本框 (TextBox)row.FindControl("Name"):

protected void GridView_Names_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "OnClickButton_Copy")
{
    int index = Convert.ToInt32(e.CommandArgument);

    GridViewRow row = GridView_Names.Rows[index];
    //GridView_Names.Rows[index].Cells[2].Text = row.Cells[1].Text; // Cells[2] changes from TextBox to Label !!!
        TextBox txtName = (TextBox)row.FindControl("Name");
        txtName.Text= row.Cells[3].Text;

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-07-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多