Question

i have an aspx-page which contains a detailsview. this detailsview contains one or more templated-fields. what i need is a additional attribute (or metadata information) to determite the bound datafield.

some thing like that would be nice (simplified):

<asp:DetailsView>
    <fields>
        <TemplateField DataField="DataField1">
            ...
        </TemplateField>
    </fields>
</asp:DetailsView>

is it possible to get attribute "DataField" ? otherwise i will subclassing TemplateField and add a property :)

Was it helpful?

Solution 2

i thought subclassing of TemplateField will do the job:

[AspNetHostingPermission(SecurityAction.Demand, Level=AspNetHostingPermissionLevel.Minimal)]
[DefaultProperty("DataField")]
public class DataTemplateField : TemplateField
{
    private String _dataField;


    public String DataField
    {
        get {
            return _dataField;
        }

        set {
            _dataField = value;
        }
    }
}

now u can use this field in detailsview like this

<Fields>
    <dvt:DataTemplateField HeaderText="Feld1" DataField="DIS">
        <ItemTemplate>
            <asp:Button runat="server" Text="Button" />
        </ItemTemplate>
    </dvt:DataTemplateField>
</Fields>

and get that data

protected override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    foreach(DetailsViewRow row in DetailsView1.Rows)
    {
        DataControlFieldCell cell = (DataControlFieldCell)row.Cells[1];
        if (cell.ContainingField is DataTemplateField)
        {
            var field = (DataTemplateField)cell.ContainingField;
            cell.Enabled = !field.DataField.Equals(fieldToDisable);
        }
    }            
}

OTHER TIPS

I haven't done that for a while, but I seem to remember if you add a public set/get "DataField" property to the TemplateField class, ASP.NET should automatically initialize it with the value you pass in the attribute.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top