【发布时间】: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 !!!
}
【问题讨论】:
-
你能澄清发生了什么吗?逐步了解正在发生的事情。
-
谢谢。我刚刚添加了更多解释的代码。