ASP.NET动态数据设置下拉列表的默认值

时间:2022-10-17 14:41:33

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.

我有一个下拉列表(FK),我想设置并显示基于登录用户ID的默认值。你能告诉我怎么做吗?我只想影响出现在Gridview上方顶部的下拉过滤器。

Thanks

Nikos

4 个解决方案

#1


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)

如果您只想在“筛选条件”部分中显示的“DropDown”列表中使用此功能,则只需通过添加要筛选的QueryString参数来修改URL。 DynamicFilter将从QueryString中获取值并相应地设置DropDown列表。 (fyi。这与ForeignKey.ascx FieldTemplate提供的功能相同)

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.

如果有更好的方法来实际创建这个URL(而不是使用字符串)会很好,但截至目前,我提供的任何解决方案可能会在后续版本中中断。

example (in page_load)

示例(在page_load中)

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

#2


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.

假设它仅用于一个外键关系,您可以创建一个新的FieldTemplate,仅用于该关系。 New FieldTemplate将是默认“ForeignKey”FieldTemplate的副本。在New FieldTemplate中,修改OnDataBinding(或Page_PreRender)事件以设置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)

然后,要强制将New FieldTemplate用于该关系,您需要在实体类的成员上使用表示该外键关系的System.ComponentModel.DataAnnotations.UIHint属性。 (链接如下)

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)

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.uihint.aspx或http://www.asp.net/learn/3.5-SP1/video-291.aspx(大约07:30分钟)

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

如需一点帮助,您可以查看CodePlex上的DynamicData Futures版本。特别是“使用过滤器中的值填充插入模板”部分。 http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475

#3


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);

我通过在PopulateListControl(DropDownList1)之后的Page_Init中插入以下行来编辑FilterUserControl.ascx.cs;

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

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(“Bob”)); //用户名是硬编码的,仅用于测试

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

这似乎工作,但我更喜欢使用带有元数据的自定义部分实体类来解决这个问题。

#4


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:

我在我正在处理的应用程序中解决了这个问题,在后面的插入视图模板代码中:在ItemCreated事件中进行详细信息视图:

       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.

使用此代码,当用户登录并且他们要插入某个实体(tblName)时,已经选择并禁用了下拉列表(fk到userId)。

#1


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)

如果您只想在“筛选条件”部分中显示的“DropDown”列表中使用此功能,则只需通过添加要筛选的QueryString参数来修改URL。 DynamicFilter将从QueryString中获取值并相应地设置DropDown列表。 (fyi。这与ForeignKey.ascx FieldTemplate提供的功能相同)

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.

如果有更好的方法来实际创建这个URL(而不是使用字符串)会很好,但截至目前,我提供的任何解决方案可能会在后续版本中中断。

example (in page_load)

示例(在page_load中)

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

#2


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.

假设它仅用于一个外键关系,您可以创建一个新的FieldTemplate,仅用于该关系。 New FieldTemplate将是默认“ForeignKey”FieldTemplate的副本。在New FieldTemplate中,修改OnDataBinding(或Page_PreRender)事件以设置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)

然后,要强制将New FieldTemplate用于该关系,您需要在实体类的成员上使用表示该外键关系的System.ComponentModel.DataAnnotations.UIHint属性。 (链接如下)

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)

http://msdn.microsoft.com/en-us/library/system.componentmodel.dataannotations.uihintattribute.uihint.aspx或http://www.asp.net/learn/3.5-SP1/video-291.aspx(大约07:30分钟)

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

如需一点帮助,您可以查看CodePlex上的DynamicData Futures版本。特别是“使用过滤器中的值填充插入模板”部分。 http://www.codeplex.com/aspnet/Release/ProjectReleases.aspx?ReleaseId=14475

#3


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);

我通过在PopulateListControl(DropDownList1)之后的Page_Init中插入以下行来编辑FilterUserControl.ascx.cs;

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

DropDownList1.SelectedIndex = DropDownList1.Items.IndexOf(DropDownList1.Items.FindByText(“Bob”)); //用户名是硬编码的,仅用于测试

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

这似乎工作,但我更喜欢使用带有元数据的自定义部分实体类来解决这个问题。

#4


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:

我在我正在处理的应用程序中解决了这个问题,在后面的插入视图模板代码中:在ItemCreated事件中进行详细信息视图:

       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.

使用此代码,当用户登录并且他们要插入某个实体(tblName)时,已经选择并禁用了下拉列表(fk到userId)。