views:

2171

answers:

4

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

A: 

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

Aaron Hoffman
I only want to set the default to the dropdown list that appears above the GridView in List.aspx. Your suggestion appears to add dropdown list to my FK column in Gridview (all the rows with FK).
Nikos
Yes, this solution would modify the DropDown List everywhere it is displayed. Please edit your original question and note that you only want it changed in the Filter Section, not everywhere. (and see my next answer below)
Aaron Hoffman
A: 

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.

Nikos
A: 

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");
Aaron Hoffman
Thanks Aaron. This trick will do for now.
Nikos
A: 

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.

Neville Chinan