动态设置Gridview中TextBox和Label的Text属性

时间:2015-02-24 19:48:52

标签: c# asp.net

<asp:TemplateField ConvertEmptyStringToNull="True">
    <ItemTemplate>
        <asp:Label ID="lblpsaia" Visible='<%# !(bool) IsInEditMode %>' 
                   runat="server" Text='<%# Eval("psaia") %>' />
        <asp:TextBox ID="txtpsaia" ControlStyle-CssClass="wide" 
                     Visible='<%# IsInEditMode %>'
                     runat="server" Text='<%# Eval("psaia") %>' />
    </ItemTemplate>
</asp:TemplateField>

在此示例中,我可以动态设置标签和文本框的Text属性(在C#中)吗?

1 个答案:

答案 0 :(得分:1)

尝试这样的事情。

protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
     Label lblpsaia = (Label)e.Row.FindControl("lblpsaia");
     lblpsaia.Text = "Sample Text Here";

     TextBox txtpsaia = (TextBox)e.Row.FindControl("txtpsaia");
     txtpsaia.Text = "Sample Text Here";
   }
}