Question

I have a dropdown list (FK) which I would like to set and display a default value based on a login userid. Can you please tell me how to do it? I only want to affect the dropdown filter that appear at the top above the Gridview.

Thanks

Nikos

Was it helpful?

Solution

If you only want this functionality in the DropDown list that appears in the Filter Criteria section, just modify the URL by adding the QueryString parameters you would like to filter by. The DynamicFilter will pick up the values from the QueryString and set the DropDown lists accordingly. (fyi. this is the same functionality that the ForeignKey.ascx FieldTemplate provides)

It would be nice if there was a better way to actually create this URL (instead of using string), but as of right now, any solution I provide is probably going to break in a subsequent version.

example (in page_load)

Response.Redirect("~/Employees/List.aspx?ReportsTo=1234");

OTHER TIPS

Is this a universal change, or just for one foreign key relationship?

Assuming it is for just one foreign key relationship, you could create a new FieldTemplate, to be used just for that relationship. The New FieldTemplate would be a copy of the default "ForeignKey" FieldTemplate. In the New FieldTemplate modify the OnDataBinding (or Page_PreRender) event to set the "default value" of the DropDownList.

Then, to force the New FieldTemplate to be used for that relationship, you would need to use the System.ComponentModel.DataAnnotations.UIHint attribute on the member of your Entity Class that represents that foreign key relationship. (links below)

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.uihint.aspx or http://www.asp.net/learn/3.5-SP1/video-291.aspx (around 07:30 mins)

For a little help, you could take a look at the DynamicData Futures release on CodePlex. Specifically the "Populate Insert templates with values from filters" section. http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475

I have figured out a workaround, but am open to a more elegant solution.

I edited the FilterUserControl.ascx.cs by inserting the following line in the Page_Init after PopulateListControl(DropDownList1);

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText("Bob")); // Username is hardcoded just for test

This seems to work but I would prefer using the custom Partial Entity Class with metadata to solve this if possible.

I have solved this in an application I am working on, in your insert view template code behind: In ItemCreated event for the details view:

       foreach (DetailsViewRow row in DetailsView1.Rows)
        {
            foreach (Control ctl in row.Controls)
                foreach (Control c in ctl.Controls)
                    foreach (Control x in c.Controls)
                    {
                        if (x.ClientID.Contains("tblName"))
                        {
                            foreach (Control y in x.Controls)
                            {
                                if (y.ClientID.Contains("DropDownList"))
                                {
                                    ddl = y as DropDownList;
                                    ddl.SelectedValue = Convert.ToString(UserId);
                                    ddl.Enabled = false;
                                }
                            }
                        }
                    }
        }

With this code, when a user is logged in and they go to insert some entity (tblName) the drop down list (fk to userId) is already selected and disabled.

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