如何从标签复制到 GridView 中的文本框
How to Copy from Label to TextBox inside GridView
我已经 c# .Net Gridview
,当我尝试将值从 Label 单元格复制到 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 !!!
}
我应该使用此命令 (TextBox) 在 Gridview 中找到 TextBoxrow.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;
}
我已经 c# .Net Gridview
,当我尝试将值从 Label 单元格复制到 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 !!!
}
我应该使用此命令 (TextBox) 在 Gridview 中找到 TextBoxrow.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;
}