开发者

How do I get a reference to a telerik:GridTemplateColumn from a child control?

Simplified code from page:

<%@ Page Language="C#" etc... %>
<%@ Register src="~/controls/RequiredField.ascx" tagname="rf" tagprefix="custom" %>

<telerik:RadGrid runat="server">
    <MasterTableView>
        <Columns>
            <telerik:GridTemplateColumn DataField="Name" HeaderText="Name" SortExpression="Name">
                <ItemTemplate><%#Eval("Name")%></ItemTemplate>                    
           开发者_StackOverflow中文版     <EditItemTemplate>
                    <asp:TextBox ID="NewName" runat="server" Text='<%#Bind("Name")%>'></asp:TextBox>
                    <custom:rf runat="server" />
                </EditItemTemplate>
            </telerik:GridTemplateColumn>
        </Columns>
    </MasterTableView>
</telerik:RadGrid>

In my control, I want to check if the parent is an EditItemTemplate and then set a property of the telerik:GridTemplateColumn. For example:

public partial class controls_RequiredField : System.Web.UI.UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Parent is Telerik.Web.UI.GridEditFormItem.EditFormTableCell)
        {
            // how do I get a reference to 'Telerik.Web.UI.GridTemplateColumn' (or any other object that lets me set the header text)
            ((Telerik.Web.UI.GridTemplateColumn)this.Parent.Parent).EditFormHeaderTextFormat = "{0}:" + RequiredText.Text;
            RequiredText.Visible = false;
        }
    }
}


I don't have the telerik:RadGrid but it is pretty similar to the MS GridView, so I was able to test your issue using asp:GridView (both inherit from CompositeDataBoundControl Class (System.Web.UI.WebControls))

since your custom control is located in the EditItemTemplate your RequiredField control's Page_Load event will not fire until the RadGrid switches to edit mode so you should be able to drop the if (this.Parent is...) check as you'll know the grid is in edit mode.

So with the custom control's page load indicating the grid is in edit mode you can set the HeaderText of the GridTemplateColumn by doing something like:

if (typeof(DataControlFieldCell) == Parent.GetType())
{
    ((DataControlFieldCell)this.Parent).ContainingField.HeaderText = "Your Custom Heading"; // Or += if appending
}


Well this is the code I'm currently using that works:

protected void Page_Init(object sender, EventArgs e)
{
    if (this.Parent is GridEditFormItem.EditFormTableCell)
    {
        GridEditFormItem.EditFormTableCell parentCell = (GridEditFormItem.EditFormTableCell)this.Parent;
        string col = parentCell.ColumnName;
        // ridiculous:
        Control parentFormItem = this.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent.Parent;
        if (parentFormItem is GridItem)
        {
            GridItem gi = (GridItem)parentFormItem;
            GridColumn parentColumn = gi.OwnerTableView.Columns.FindByUniqueNameSafe(col);
            if (parentColumn != null)
            {
                parentColumn.EditFormHeaderTextFormat = "{0}:" + RequiredText.Text;
                RequiredText.Visible = false;
            }
        }
    }
}

But having to cycle up through all those .Parents makes me uneasy.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜