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 DropDownLists to show and allow change of a property group for an item.
我需要创建一组DropDownLists 来显示和允许更改项目的属性组。
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_OnInitpopulates the DropDownListwith all the possible values with a database query.
将ddProperty_OnInit填充DropDownList与所有使用数据库查询的可能值。
How can I set the selected value of each created DropDownListaccording to the Repeater's source data?
如何DropDownList根据Repeater的源数据设置每个创建的选定值?
Let's say, for example, that we have the possible property values of A, Band C.
If the database output for the Repeatercontains two of those values, Aand B, the Repeateroutputs two DropDownLists, both with all 3 values availabla and the first one with Aas selected value and the second one with Bas selected value.
例如,假设我们有A,B和的可能属性值C。
如果 的数据库输出Repeater包含这些值中的两个,A并且B,则Repeater输出两个DropDownLists,都具有所有 3 个值,第一个具有A选定值,第二个具有B选定值。
Edit:
It seems that adding OnItemDataBound="repeater_ItemDataBound"to the Repeaterand 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 ItemDataBoundevent of the Repeateris fired before the OnClickevent on a Buttonand 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_OnInitpopulates the drop down list with all the possible choises, while the repeater_ItemDataBounduses 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 DataBindplays an important role. I used to do the databinding in the Page_Initevent, which caused the repeater_ItemDataBoundevent to be fired before the button_OnClickevent.
对于我在编辑中指定的问题,时间DataBind起着重要作用。我以前在事件中做数据绑定Page_Init,导致repeater_ItemDataBound事件在button_OnClick事件发生之前被触发。
The solution was to move the databinding to the Page_PreRenderevent.
The population of the DropDownListwith all the choises is still done in its OnInitevent.
解决方案是将数据绑定移动到Page_PreRender事件。具有所有选择的
人口DropDownList仍然在其OnInit事件中完成。

