views:

118

answers:

2

hi! 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 :)

A: 

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.

axel_c
A: 

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);
        }
    }            
}
mo
Subclassing won't do that job in your case. What you did was assigning a string value to the Property "DataField". This will not bind a value.
Arthur
i need to know which field is represented by the templatefield.some fields are readonly for specific users, so that fields need to be disabled.
mo
that's not nice, but it solve my problem.
mo