DevExpress修改templateitem中的标签

时间:2017-03-31 08:27:42

标签: devexpress

我正在使用 DevExpress 平台。 我有一个包含带有Label的ItemTemplate的axGridview,我需要在每行中修改值。 使用asp.net和GridView我用来管理 RowDataboundEvent 中的FindControl,但在这里我真的需要帮助。 FindCellTemplate 函数始终返回NULL。 在这里我的代码:

        <dx:ASPxGridView ID="gvRecapiti" ClientIDMode="Static" ClientInstanceName="gvRecapiti" Width="100%"  runat="server" AutoGenerateColumns="False"  
            OnHtmlRowCreated="gvRecapiti_HtmlRowCreated"  >
            <Columns>
                        <dx:GridViewDataColumn  Caption="RecapitoTipo" >                                                  
                        <SettingsHeaderFilter>
                        <DateRangePickerSettings EditFormatString=""></DateRangePickerSettings>
                        </SettingsHeaderFilter>
                        </dx:GridViewDataColumn>                                      

                    <dx:GridViewDataTextColumn FieldName="DescRecapito" >
                        <DataItemTemplate>        
                                <dx:ASPxLabel  ID="lblRecapito" ClientIDMode="Static" runat="server" ClientInstanceName="lblRecapito" Text='<%# Eval("DescRecapito") %>' ></dx:ASPxLabel>                                              
                            </DataItemTemplate>
                    </dx:GridViewDataTextColumn>        
            </Columns>

            </dx:ASPxGridView>


using DevExpress.Web;

namespace ProvaGridItem
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Page.IsPostBack != false)
            {
                BindGrid();
            }
        }
        private void BindGrid() 
        {
            List<Recapito> R = new List<Recapito>();

            for (int i=0;i<5;i++)
            {
                Recapito Recapito = new Recapito();
                Recapito.DescRecapito = "Recapito: " + i;
                Recapito.RecapitoTipo="RecapitoTipo: "+i;
                R.Add(Recapito);
            }

            gvRecapiti.DataSource = R;
            gvRecapiti.DataBind();
        }

        protected void gvRecapiti_HtmlRowCreated(object sender, DevExpress.Web.ASPxGridViewTableRowEventArgs e)
        {
            if (e.RowType != DevExpress.Web.GridViewRowType.Data) return;
            ASPxLabel lblRecapitoTipo = (ASPxLabel)gvRecapiti.FindRowCellTemplateControl(e.VisibleIndex, null, "lblRecapitoTipo");          
            lblRecapitoTipo.Text = "Label Updated by code!!";
        }


      internal  class Recapito
        {
            public string RecapitoTipo { get; set; }
            public string DescRecapito { get; set; }
        }
    }
}

1 个答案:

答案 0 :(得分:1)

  (ASPxLabel)gvRecapiti.FindRowCellTemplateControl(e.VisibleIndex, null, "lblRecapitoTipo");

这应该有列来查找控件。这样首先获取变量中的列,然后找到该列的模板控件。请使用下面的代码。

 GridViewDataTextColumn col = gvRecapiti.Columns["lblRecapito"] as GridViewDataTextColumn;
   ASPxLabel lblRecapitoTipo =  gvRecapiti.FindRowCellTemplateControl(e.VisibleIndex, col , "lblRecapitoTipo") as ASPxLabel;

然后您可以根据需要更改 lblRecapitoTipo 标签属性。在您的情况下,它是

lblRecapitoTipo.Text = "Label Updated by code!!";