C# 如何在中继器中设置 DropDownList 的选定值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/556044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set DropDownList's selected value inside a Repeater?
提问by vipirtti
I need to create a group of DropDownList
s to show and allow change of a property group for an item.
我需要创建一组DropDownList
s 来显示和允许更改项目的属性组。
I have the following code on my ASP page.
我的 ASP 页面上有以下代码。
<asp:Repeater runat="server" ID="repeaterProperties">
<ItemTemplate>
<p><asp:DropDownList runat="server" ID="ddProperty" OnInit="ddProperty_OnInit" /><p>
</ItemTemplate>
</asp:Repeater>
The ddProperty_OnInit
populates the DropDownList
with all the possible values with a database query.
将ddProperty_OnInit
填充DropDownList
与所有使用数据库查询的可能值。
How can I set the selected value of each created DropDownList
according to the Repeater
's source data?
如何DropDownList
根据Repeater
的源数据设置每个创建的选定值?
Let's say, for example, that we have the possible property values of A
, B
and C
.
If the database output for the Repeater
contains two of those values, A
and B
, the Repeater
outputs two DropDownList
s, both with all 3 values availabla and the first one with A
as selected value and the second one with B
as selected value.
例如,假设我们有A
,B
和的可能属性值C
。
如果 的数据库输出Repeater
包含这些值中的两个,A
并且B
,则Repeater
输出两个DropDownList
s,都具有所有 3 个值,第一个具有A
选定值,第二个具有B
选定值。
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound"
to the Repeater
and selecting the appropriate value in there is not the way to go in my case. This is because I also need to save the possibly changed values to a database.
编辑:
看来,加入OnItemDataBound="repeater_ItemDataBound"
到Repeater
并选择在有合适的值是不是在我的情况下要走的路。这是因为我还需要将可能更改的值保存到数据库中。
The ItemDataBound
event of the Repeater
is fired before the OnClick
event on a Button
and changes the selected values to their old values before the new selections can be saved.
在ItemDataBound
的情况下,Repeater
在发射之前OnClick
对事件Button
和改变选择的值旧值可以保存新的选择之前。
Any suggestion on how to work around this?
关于如何解决这个问题的任何建议?
Current code:
当前代码:
<asp:Repeater runat="server" ID="repeaterJako" OnItemDataBound="repeater_ItemDataBound">
<ItemTemplate>
<asp:DropDownList id="ddJako" runat="server" OnInit="ddJako_OnInit">
</asp:DropDownList><br />
</ItemTemplate>
</asp:Repeater>
<asp:Button runat="server" id="updateButton" Text="Save" OnClick="update_OnClick" />
In the code-behind, ddJako_OnInit
populates the drop down list with all the possible choises, while the repeater_ItemDataBound
uses the method suggested by Bryan Parker to select the proper value.
在代码隐藏中,ddJako_OnInit
使用所有可能的选项填充下拉列表,同时repeater_ItemDataBound
使用 Bryan Parker 建议的方法选择正确的值。
采纳答案by Bryan
Maybe I'm misunderstanding something about your question... but it seems like this is exactly what OnItemDataBound is for. :)
也许我误解了你的问题......但似乎这正是 OnItemDataBound 的用途。:)
Use FindControl to get a reference to your DropDownList in the event handler. Also check to make sure the item is not the header/footer. The example from MSDN does both these things:
使用 FindControl 在事件处理程序中获取对 DropDownList 的引用。还要检查以确保该项目不是页眉/页脚。MSDN 中的示例同时完成了以下两件事:
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.repeater.onitemdatabound.aspx
回答by vipirtti
In regard the problem I specified in my edit, the time of the DataBind
plays an important role. I used to do the databinding in the Page_Init
event, which caused the repeater_ItemDataBound
event to be fired before the button_OnClick
event.
对于我在编辑中指定的问题,时间DataBind
起着重要作用。我以前在事件中做数据绑定Page_Init
,导致repeater_ItemDataBound
事件在button_OnClick
事件发生之前被触发。
The solution was to move the databinding to the Page_PreRender
event.
The population of the DropDownList
with all the choises is still done in its OnInit
event.
解决方案是将数据绑定移动到Page_PreRender
事件。具有所有选择的
人口DropDownList
仍然在其OnInit
事件中完成。