C# 如何在GridView中从标签复制到文本框
在c.NETGridView中,当我尝试在Gridview中将一个值从Label单元格复制到TextBox单元格时,TextBox单元格变为Label 在_row命令中: 行。单元格[2]。文本在从文本框分配到标签后更改 有什么原因和解决办法吗 备注:我添加了代码以进行更多解释 .aspx代码:C# 如何在GridView中从标签复制到文本框,c#,.net,gridview,C#,.net,Gridview,在c.NETGridView中,当我尝试在Gridview中将一个值从Label单元格复制到TextBox单元格时,TextBox单元格变为Label 在_row命令中: 行。单元格[2]。文本在从文本框分配到标签后更改 有什么原因和解决办法吗 备注:我添加了代码以进行更多解释 .aspx代码: <asp:GridView ID="GridView_Names" runat="server" AutoGenerateColumns="False" Data
<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>
我必须使用以下命令TextBoxrow.FindControlName在Gridview中查找文本框:
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;
}
你能澄清发生了什么事吗?一步一步地了解正在发生的事情。谢谢。我刚刚添加了更多解释的代码。
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 !!!
}
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;
}