如何从后面的代码中将值设置为ItemTemplate控件? [英] How to set the value to a ItemTemplate control from the code behind?

查看:68
本文介绍了如何从后面的代码中将值设置为ItemTemplate控件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



如何从代码后面的gridview中设置或赋值给ItemTemplate Lable控件?



我试过但没有解决方案。 非常紧急



等待您的及时回复。请找下面我的代码







HTML来源:



Dear All,

How to set or assign the value to the ItemTemplate Lable control in the gridview from code behind?

I have tried but no solution. It's very urgent.

Awaiting your prompt response. Pls find the below my code



HTML Source:

<asp:GridView ID="grdXML" AutoGenerateColumns="False"  runat="server">
                     <Columns>
                     <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Label ID="UserID" runat="server" Text="" />
                        </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="">
                        <ItemTemplate>
                            <asp:Label ID="Uname" runat="server" Text="" />
                        </ItemTemplate>
                        </asp:TemplateField>
                     </Columns>
                     </asp:GridView>







代码落后(C#.Net)




Code Behind (C#.Net)

protected void Page_Load(object sender, EventArgs e)
{
  ((Label)grdXML.FindControl("UserID")).Text = "Sample text";
}





代码块已修复[/ edit]

推荐答案

访问行级别的控件,使用 FindControl 查找所需的控件。您将可以访问 RowDataBound 事件方法中的行。
Access the controls at row level and find the needed control using FindControl. You will have access to rows in RowDataBound event method.


以下示例解释了将值设置为项模板控件---- -



Following example explains the to set value to Item Template controls-----

//Add onRowDataBound event to your grid and write the following code server side

protected void gridView1_RowDataBound(object sender,GridViewRowEventArgs e)
{
 
//make sure to have UserID as itemtemplate in your grid
 
Label lblUserID= (Label)e.Row.FindControl("UserID");
 
 lblUserID.Text="Sample Text";
}





这绝对有效....



this will definitely work....


你也可以使用此代码。



You can also use this code.

protected void AspGrid_RowDataBound(object sender, GridViewRowEventArgs e)
    {    
      if(e.Row.RowType == DataControlRowType.DataRow)
        {
            DataRowView v = (DataRowView)e.Row.DataItem;           
            
            if (e.Row.Cells.Count > 0 && e.Row.Cells[0] != null && e.Row.Cells[0].Controls.Count > 0)
            {
                Label link = e.Row.Cells[0].Controls[0] as Label;
                if (link != null)
                {                    
                        link.Text = "Edit";
                }
               
            }            

        }

    }


这篇关于如何从后面的代码中将值设置为ItemTemplate控件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆