0

Greetings StackOverflow,

TL;DR

In a field template control's OnLoad method how can I find the Data Control of other fields in a FormView by property or column name.

END TL;DR.

I'm trying to add a some logic to the Boolean_Edit field template so that if the property bound to it has a new attribute I made the template will inject JavaScript. The JavaScript is intended to disable all of the data controls of the column/property names listed in the attribute's ControlledFieldNames property.

This is sort of confusing so I'll share some code.

Here is the attribute class I made for this:

/// <summary>
/// Attribute used to insert javascript into an ASP.NET web page that uses Dynamic Controls so that if the field's value changes it disables (or enables) 
/// other web controls on the page which correspond to the other bound property names.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Field, Inherited = false, AllowMultiple = true)]
public sealed class InputRestrictorFieldAttribute : Attribute
{
    public Boolean TargetEnabledState { get; set; }
    public String[] ControlledFieldNames { get; set; }

    public InputRestrictorFieldAttribute(Boolean targetEnabledState, params String[] controlledFieldNames)
    {
        this.TargetEnabledState = targetEnabledState;
        this.ControlledFieldNames = controlledFieldNames;
    }
}

So I might have a property in some scaffolded class like so:

[ScaffoledTable(true)]
public class Person
{
    /* Other properties... */

    [InputRestrictorFieldAttribute(false, new String[]
    {
        "StreetAddress",
        "City",
        "State",
        "Zip"
    })]
    public Boolean AddressUnknown { get; set; }

    public String SteetAddress { get; set; }

    public String City { get; set; }

    public String State { get; set; }

    public String Zip { get; set; }

    /* some more code */
}

Now in the Boolean_Edit.ascx.cs file I am trying to check if the currently scaffold property has the InputRestrictorFieldAttribute and if so inject JavaScript onto the page such that when the AddressUnknown CheckBox control is check is disabled the TextBox controls for StreetAddress, City, State, and Zip.

Below is what I have tried most recently.

protected override void OnLoad(EventArgs e)
{
    var attributes = this.Column.Attributes;
    foreach (Attribute attr in attributes)
    {
        if (attr is InputRestrictorFieldAttribute)
        {
            InputRestrictorFieldAttribute restrictor = (InputRestrictorFieldAttribute)attr;
            String restrictorScriptFunctionName = String.Format(RESTRICTOR_SCRIPT_FUNCTION_NAME, ClientID);
            String restrictorScript = String.Format(RESTRICTOR_SCRIPT_TEMPLATE_ONCLICK,
                restrictorScriptFunctionName,
                restrictor.BoundFieldNames.Aggregate("", (aggr, item) =>
                {
                    var bc = this.NamingContainer.BindingContainer;
                    var ctrl = bc.FindFieldTemplate(item);
                    return aggr + String.Format(RESTRICTOR_SCRIPT_TEMPLATE_ELEMENT, ctrl.ClientID);
                }));

            Page.ClientScript.RegisterStartupScript(Page.GetType(), "restrictorScript_" + ClientID, restrictorScript, true);
            CheckBox1.Attributes.Add("onchange", restrictorScriptFunctionName + "(this);");
        }
    }

        base.OnLoad(e);
    }

Now I know doing things like getting the this.NamingContainer.BindingContainer many not (or probably wont) work in other pages but for now (in the context of the Insert.aspx page template) works. this.NamingContainer.BindingContainer is the FormView1 control of the Insert.aspx page. But everything I've tried so far to get the various data dontrols or field templates, or dynamic controls by their property name it always returns null or throws an exception.

Lastly, the aggregate method is just concatenating pieces of JavaScript together so that all of the controls are deactivated using just one JavaScript function. The contents of those script are not important to this question.

LamdaComplex
  • 871
  • 1
  • 8
  • 21

1 Answers1

0

there is an extension method called FindFieldTemplate see see it here

Wizzard
  • 924
  • 5
  • 9