简体   繁体   English

ASP如何在详细信息视图中绑定下拉列表的选定值

[英]ASP How to bind the selected value of a dropdownlist in detailsview

I have a dropdownlist in a detailsview control. 我在detailsview控件中有一个下拉列表。 The data source is a SQL table with a dayofthemonth column. 数据源是一个带有dayofthemonth列的SQL表。

<asp:TemplateField HeaderText="Day of Month: ">
            <EditItemTemplate>
                <asp:DropDownList ID="ddlDoM" runat="server" > 
                </asp:DropDownList>
            </EditItemTemplate>
            <InsertItemTemplate>
                <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("dayofthemonth") %>'></asp:TextBox>
            </InsertItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server" Text='<%# Bind("dayofthemonth") %>'></asp:Label>
            </ItemTemplate>
            <HeaderStyle Font-Bold="True" />
</asp:TemplateField>

I have populated the dropdownlist with numbers 1 - 31 in the DataBound event. 在DataBound事件中,我使用数字1-31填充了下拉列表。

protected void DataView1_DataBound(object sender, EventArgs e)
{
    DropDownList ddlDoM = (DropDownList)Page.FindControl("ctl00$formPlaceHolder$DataView1$ddlDoM");
    if (DataView1.CurrentMode == DetailsViewMode.Edit)
    {
        for (int i = 1; i < 32; i++)
        {
            ListItem item = ListItem.FromString(i.ToString());
            ddlDoM.Items.Add(item);
        }
    }
        DataRowView drv = DataView1.DataItem as DataRowView;
        ddlDoM.SelectedIndex = ddlDoM.Items.IndexOf(ddlDoM.Items.FindByValue(drv["dayofthemonth"].ToString()));
    }
}

In the final line here, I set the selected value of the dropdownlist to the current data source value. 在这里的最后一行中,我将下拉列表的选定值设置为当前数据源值。

ddlDoM.SelectedIndex = ddlDoM.Items.IndexOf(ddlDoM.Items.FindByValue(drv["dayofthemonth"].ToString()));

How can I bind the dropdownlist to update the value in the datasource when changed? 更改后,如何绑定下拉列表以更新数据源中的值? I have tried setting the selected value property to <%# Bind("dayofthemonth") %> , but I receive an OutOfRangeException. 我尝试将选定的value属性设置为<%# Bind("dayofthemonth") %> ,但是我收到OutOfRangeException。 I'm assuming the list hasn't populated yet in switching to Edit Mode. 我假设在切换到“编辑模式”时尚未填充列表。

Any thoughts on this? 有什么想法吗? Let me know if you need more code. 让我知道您是否需要更多代码。

Try adding a ControlParameter to your UpdateParameters in your datasource, and ensure your dropdown list is set to AutoPostBack if appropriate. 尝试将ControlParameter添加到数据源中的UpdateParameters ,并确保在适当的情况下将下拉列表设置为AutoPostBack。 You might also consider populating the dropdownlist in an earlier event (eg Page_Init ) and leave setting its default value until the DataBound event you already have. 您可能还考虑在更早的事件(例如Page_Init )中填充下拉列表,并保留其默认值,直到您已经拥有DataBound事件为止。

<asp:SqlDataSource ... UpdateCommand=""....>
    <UpdateParameters>
        <asp:ControlParameter ControlID="ddlDoM" Name="dayofthemonth" PropertyName="SelectedValue" Type="Int32" />
    </UpdateParameters>
</asp:SqlDataSource>

Also, if it's still causing problems, a safe (but verbose) option is to add the ListItems directly to the dropdownlist control, because they seem to be static values: 另外,如果仍然引起问题,一个安全(但冗长)的选择是将ListItems直接添加到dropdownlist控件中,因为它们似乎是静态值:

<asp:DropDownList ID="ddlDoM" runat="server" AutoPostBack="true">
    <asp:ListItem Value="1" />
    <asp:ListItem Value="2" />
    <asp:ListItem Value="3" />
    etc...
</asp:DropDownList>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在asp.net的下拉列表中从数据库中绑定选定的值 - How to bind selected value from database in dropdownlist in asp.net 如何将DetailsView中的下拉列表绑定到其他表 - How to bind dropdownlist in detailsview to different table 如何将数据绑定到asp.net中另一个dropdownlist的选定值上的dropdownlist? 我已经将数据绑定到所有下拉列表 - how to bind data to the dropdownlist on selected value of the other dropdownlist in asp.net?? I have already bind data to the all the dropdown 从ASP.NET DropDownList绑定选定的值 - Bind selected value from ASP.NET DropDownList 如何在asp.net中使用jQuery获取下拉列表选择的值绑定? - How to get dropdownlist selected value bind using jquery in asp.net? 如何在AspxGridView中设置asp:dropdownlist的选定值 - How to set selected value of an asp:dropdownlist in a AspxGridView 如何将ID绑定到WebGrid中@ Html.DropDownList的选定值 - How to bind an ID to the selected value of an @Html.DropDownList within a WebGrid 如何将kendoui下拉列表的选定值绑定到我的模型? - How do I bind the selected value of a kendoui dropdownlist to my model? 如何将值绑定到ASP.NET MVC中的dropdownlist? - How to bind value to dropdownlist in asp.net mvc? GridView中的DropDownList绑定到ASP.NET C#中GridView内TextBox中DropDown的选定值 - DropDownList in a GridView to bind the selected value of DropDown in TextBox inside a GridView in asp.net c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM