Skip to main content
 首页 » 编程设计

c#之在表单 View 中的转发器中从代码隐藏设置下拉列表上的选定值

2024年02月20日23exmyth

情况是这样的。 我在表单 View 内的转发器中有一个下拉列表。

真正特别的部分是转发器被用来动态添加多行数据。

我一直在尝试做但失败的是设置下拉菜单的选定值。

<asp:FormView ID="FormView3" runat="server" DataKeyNames="t_id"  
                DataSourceID="SqlDataSource25" DefaultMode="Insert">     
                <InsertItemTemplate> 
                    t_p_id: 
                    <asp:TextBox ID="t_p_idTextBox" runat="server" Text='<%# Bind("t_p_id") %>' /> 
                        <br /> 
 
                    t_step: 
                    <asp:TextBox ID="t_stepTextBox" runat="server" Text='<%# Bind("t_step") %>' /> 
                    <br /> 
                    t_foreclosure_date: 
                    <asp:TextBox ID="t_foreclosure_dateTextBox" runat="server"  
                        Text='<%# Bind("t_foreclosure_date") %>' /> 
                    <br /> 
                    t_date: 
                    <asp:TextBox ID="t_dateTextBox" runat="server" Text='<%# Bind("t_date") %>' /> 
                    <br /> 
 
                    <hr /> 
 
                    <asp:Repeater ID="repeater1" runat="server" OnPreRender="repeater1_PreRender" OnItemCommand="repeater1_ItemCommand" >   
                    <HeaderTemplate> 
                        <table cellpadding="5" cellspacing="5"> 
                        <tr style="padding-top: 5px;"> 
                            <td colspan="7"> 
                                <asp:Label ID="lblInstructions" runat="server" Text="Add entries here:" /> 
                            </td> 
                        </tr> 
                        <tr runat="server" id="trHeader" style="font-weight: bold;"> 
                            <td>Date</td> 
                            <td>Cost</td> 
                            <td>Type</td> 
                            <td>Comment</td> 
                            <td></td> 
                            <td></td> 
                            <td></td> 
                        </tr>          
                    </HeaderTemplate>  
                    <ItemTemplate> 
                        <tr> 
                            <td><asp:TextBox ID="txtDate" runat="server" Width="55"  
                                 Text='<%#DataBinder.Eval(Container.DataItem, "date", "{0:d}")%>' /> </td> 
                            <td><asp:TextBox ID="txtAmount" runat="server" Width="55"              
                                 Text='<%#DataBinder.Eval(Container.DataItem, "amount")%>' /> </td> 
 
                            <td> 
                                 <asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource26"  
                                    DataTextField="tt_type" DataValueField="tt_id"   ></asp:DropDownList> 
                                  </td> 
 
                                 <td><asp:TextBox ID="txtComment" runat="server" Width="300"  
                                 Text='<%#DataBinder.Eval(Container.DataItem, "comment")%>' /> </td> 
                            <td style="text-align: center;"> 
                                <asp:Button ID="btnMoveUp" runat="server" Width="70"  
                                 Text="Move Up" CommandName="Up"  
                                 CommandArgument='<%# Container.ItemIndex %>' /> 
                            </td> 
                            <td style="text-align: center;"> 
                                <asp:Button ID="btnMoveDown" runat="server" Width="90"  
                                 Text="Move Down" CommandName="Down"  
                                 CommandArgument='<%# Container.ItemIndex + 1 %>' /> 
                            </td> 
                            <td style="text-align: center;"> 
                                <asp:Button ID="btnRemove" runat="server" Width="70"  
                                 Text="Remove" CommandName="Remove"  
                                 CommandArgument='<%# Container.ItemIndex %>' /> 
                            </td> 
                        </tr> 
                    </ItemTemplate>  
                    <FooterTemplate> 
                        <tr style="padding-top: 5px;"> 
                           <td colspan="6"> 
                               <asp:Button ID="btnAdd" runat="server"  
                                Text="Add Row" CommandName="Add" /> 
                           </td> 
                        </tr> 
                        </table> 
                    </FooterTemplate> 
                </asp:Repeater> 
 
 
                    <hr /> 
                    <asp:CheckBoxList ID="tpTransmittalsDoclist" runat="server" DataSourceID="SqlDataSource13"  
                        DataTextField="document_name" DataValueField="document_id"> 
                        </asp:CheckBoxList> 
 
                    <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True"  
                        CommandName="Insert" Text="Insert" /> 
                    &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server"  
                        CausesValidation="False" CommandName="Cancel" Text="Cancel" /> 
                </InsertItemTemplate> 
            </asp:FormView> 

在后面的代码中加载页面时,我在第一个转发器行中插入了虚拟值。我正在使用类成本来设置/存储转发器行的值。但是当我对中继器进行数据绑定(bind)时,我无法弄清楚如何设置下拉列表的选定值。我尝试了一些不同的东西,但没有快乐。有什么想法吗?

// 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='<%#DataBinder.Eval(Container.DataItem, "type")%>' 

请您参考如下方法:

您可以在标记页面中绑定(bind) Dropdown 的 SelectedValue 属性,即

<asp:DropDownList ID="ddlType" runat="server" DataSourceID="SqlDataSource26"  
   DataTextField="tt_type" DataValueField="tt_id"  
   SelectedValue='<%# Eval("ColumnName")%>'> 
 </asp:DropDownList> 

希望这能解决您的问题。