views:

417

answers:

1

I am using Dynamic Data Web Entities Application website and need control of the dynamicfilters placed on the PageTemplates/list.aspx page.

I have a table using the default PageTemplates/list.aspx view and this table has a foreign key to a table of users. The foreign key is showing up in the filters correctly but I want to default the filter to the current user if exist in the filter list.

How can I set the selectedvalue of a dynamicfilter listing users to the current user logged in?

I am using windows authentication, so I have the userid. The user table also has userid and name, dynamic filter currently displaying name.

vs2008 3.5 sp1 (no futures).

A: 

I ended up capturing the FilterRepeater.ItemDataBound event on the PageTemplates/list.aspx and looking for MetaForeignKeyColumn with name of the filter I am trying to manipulate. Then using the model to lookup the user data and set the selected value to match the user logged in.

The follow code is from my test and needs to be cleaned up: TK_DBA is a reference table of database administrators using the app and appears as a filter for tables referencing it.

    protected void Page_Init(object sender, EventArgs e)
    {
        DynamicDataManager1.RegisterControl(GridView1, true /*setSelectionFromUrl*/);
        FilterRepeater.ItemDataBound += new RepeaterItemEventHandler(FilterRepeater_ItemDataBound);
    }

    void FilterRepeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        object e1 = e.Item.DataItem;
        MetaForeignKeyColumn e2 = e.Item.DataItem as MetaForeignKeyColumn;
        Test2.FilterUserControl e3 = ((RepeaterItem)e.Item).FindControl("DynamicFilter") as Test2.FilterUserControl;

        if (e2 != null && e2.Name == "TK_DBA")
        {
            var dba = e2.Model.GetTable("TK_DBA").GetQuery().OfType<Test2.Model.TK_DBA>();
            var q = from d in dba
                    where d.userid == HttpContext.Current.User.Identity.Name
                    select d;
            if (q.Count() == 1) e3.DropDownList1.SelectedValue = q.First().id.ToString();
        }
    }