在窗体视图中的转发器中的代码隐藏中设置下拉列表中的选定值
所以这就是情况。 我在formview中的转发器中有一个下拉列表。
真正特别的部分是转发器用于动态添加多行数据。
我一直试图做的和失败的是设置下拉列表的选定值。
t_p_id: <asp:TextBox ID="t_p_idTextBox" runat="server" Text='' />
t_step: <asp:TextBox ID="t_stepTextBox" runat="server" Text='' />
t_foreclosure_date: <asp:TextBox ID="t_foreclosure_dateTextBox" runat="server" Text='' />
t_date: <asp:TextBox ID="t_dateTextBox" runat="server" Text='' />
Date Cost Type Comment <asp:TextBox ID="txtDate" runat="server" Width="55" Text='' /> <asp:TextBox ID="txtAmount" runat="server" Width="55" Text='' /> <asp:TextBox ID="txtComment" runat="server" Width="300" Text='' /> <asp:Button ID="btnMoveUp" runat="server" Width="70" Text="Move Up" CommandName="Up" CommandArgument='' /> <asp:Button ID="btnMoveDown" runat="server" Width="90" Text="Move Down" CommandName="Down" CommandArgument='' /> <asp:Button ID="btnRemove" runat="server" Width="70" Text="Remove" CommandName="Remove" CommandArgument='' />
在代码后面的页面加载我在第一个转发器行中插入虚拟值。 我正在使用类成本来设置/存储转发器行的值。 但是当我对转发器进行数据绑定时,我无法弄清楚如何设置下拉列表的选定值。 我尝试了一些不同的东西,但没有快乐。 有什么想法吗?
// load up costs in transmittals tab if (!Page.IsPostBack) { Costs mycost = new Costs(); string date = DateTime.Now.ToShortDateString(); Costs.Cost cost1 = new Costs.Cost(date, "1.99", 2, "1"); mycost.Add(cost1); Repeater r = FormView3.FindControl("repeater1") as Repeater; r.DataSource = mycost; r.DataBind(); }
编辑:如果您使用整数。 你会想要使用。
SelectedIndex=''
你可以在你的标记页面中绑定Dropdown的SelectedValue属性,即
希望,这将解决您的问题。
如果值不能设置在drodownlist标签中,如…
SelectedValue='<%# Eval("ColumnWithValue")%>'
…然后你需要使用OnItemDataBound
基本上:
在转发器标记上,添加以下属性:
OnItemDataBound="FormatRepeaterRow"
在页面代码隐藏:
protected void FormatRepeaterRow(Object sender, RepeaterItemEventArgs e) { if( (e.Item.ItemType == ListItemType.Item) || ( e.Item.ItemType == ListItemType.AlternatingItem)) { ((DropDownList)e.Item.FindControl("ddlType")).SelectedValue = "a_value"; } }