Question

I have a Dynamic Data app that uses two tables. For one of the tables, there is a column that I would like to set based on a column in the other table, using a dropdown populated with the values from this other table. Currently this column is generated on the page as a text box entry. However, Dynamic Data doesn't provide a dropdown item to provide a UIHint for this. I tried modifying the Enumeration field control to do this, but couldn't seem to get it to work.

I should specify that this is for adding a new entry from the Insert page for one of the tables. I'd like to have one of the items on the insert page to be set based on a value from a column in the other table, using a dropdown of values populated from the column in that other table.

Was it helpful?

Solution 2

So I managed to figure this one out on my own actually. You can simply modify code for one of the existing Field Templates to do what I'm looking to do.

As stated, the idea is to take a text box for data entry on the Insert page and turn it into a dropdown list of values, where that dropdown is comprised of data from another table. The user would then select the value needed, and it would be passed back to the database as part of the insert process.

Start by creating a new control in the FieldTemplates folder. Name it something like DropDown. You'll have your ascx, the code behind file, and the designer file.

Then, take the code from the ascx file for Enumeration_Edit, and copy it into the ascx file for your new dropdown control. You may need to rename the ID of the DropDownList control.

With the code behind, copy the code from the Enumeration_Edit control's code behind and alter them to meet the needs of the dropdown.

Your PageLoad class should look like this:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        OnDataBinding();
    }

    SetUpValidator(RequiredFieldValidator1);
    SetUpValidator(DynamicValidator1);
}

Your OnDataBinding should be setup like this:

public void OnDataBinding()
{
    //Code for binding data from a query/stored proc to a dropdown should go here.
}

Keep the ExtractValues() and DataControl() classes, but you can get rid of the EnumType() class.

Also be sure to add any references you may need. Compare the code behind for the Enumeration_Edit control and your new control to figure this out.

Now, to make the control work on the text box for the column you want it to control data insertion for. You can simply add a [UIHint("DropDown")] to the appropriate column item for your partial class for the table, but that will apply the control to all instances of that column when displayed. I have custom pages setup for each of my tables, and I only want this control used for the insert functionality of a specific column. As such, I altered my Insert page for this table to make the line controlling this column to look like this:

<asp:DynamicField DataField="COLUMN_NAME" UIHint="DropDown" ReadOnly="true" />

Doing it this way you only apply the control to this column on this page. You need to set the ReadOnly value to true, otherwise it will still render as a text input field and not a dropdown.

From there, your insert page should have a dropdown for data entry on that column.

I only needed it for this one column, so the code for tying the data to my dropdown is only setup to work for this one column. You may be able to write a query that looks up all values for a particular column based on the column you've tied the control to, or you may simply need to create your own Field Templates for each column you need to have a dropdown list for.

OTHER TIPS

Did you use a filter control ?

FilterControl property returns the control that should receive focus when user selects the Label generated for it by the QueryableFilterRepeater. In our date filter, this can be either the drop-down list or the text box.

maybe check this post will help you: http://www.olegsych.com/2010/07/understanding-aspnet-dynamic-data-filter-templates/

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