【问题标题】:ASP How to bind the selected value of a dropdownlist in detailsviewASP如何在详细信息视图中绑定下拉列表的选定值
【发布时间】:2015-12-06 18:50:57
【问题描述】:

我在 detailsview 控件中有一个下拉列表。数据源是一个带有 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>

我已在 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()));
    }
}

这里的最后一行,我将下拉列表的选中值设置为当前数据源值。

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

如何绑定下拉列表以在更改时更新数据源中的值?我尝试将选定的值属性设置为&lt;%# Bind("dayofthemonth") %&gt;,但我收到了 OutOfRangeException。我假设在切换到编辑模式时列表尚未填充。

对此有什么想法吗?如果您需要更多代码,请告诉我。

【问题讨论】:

  • 这与经典 ASP 无关 - 请重新标记到 ASP.NET。
  • 我的错误,感谢您的关注

标签: c# asp.net data-binding detailsview


【解决方案1】:

尝试在您的数据源中将ControlParameter 添加到您的UpdateParameters,并确保您的下拉列表设置为AutoPostBack(如果合适)。您也可以考虑在较早的事件(例如Page_Init)中填充下拉列表,并在您已经拥有的 DataBound 事件之前设置其默认值。

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

另外,如果它仍然导致问题,一个安全(但冗长)的选项是将 ListItems 直接添加到下拉列表控件,因为它们似乎是静态值:

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

【讨论】:

  • 我按照这些步骤操作,现在收到错误Could not find control 'ddlDoM' in ControlParameter 'dayofthemonth'. 这可能是因为控件不存在,除非 DetailsView 处于编辑模式?我该如何解决这个问题?
  • @NickOban:对不起,我的原始代码更关注选择参数,但您正在寻找更新参数。查看更新的答案。
  • 再次感谢,迈克尔。这肯定更接近,但现在我回到了 OutOfRangeException。 'ddlDoM' has a SelectedValue which is invalid because it does not exist in the list of items. Parameter name: value 我认为这可能是一个生命周期问题。我需要知道在 ddlDoM 的数据绑定之前但在它创建之后(切换到编辑模式时)是否有一个事件触发。我现在将继续使用详细的 ListItem 解决方案。
  • 是的,我做到了。我认为问题在于没有为要查找的代码创建 ddlDoM 控件。 (DropDownList)Page.FindControl("ctl00$formPlaceHolder$DataView1$ddlDoM"); 返回 null 并且我的 DetailsView 的 CurrentMode 仍然是 ReadOnly。
  • @NickOban:是的,我想这是有道理的,但是您可以尝试许多其他事件,它们可能会在生命周期中到达正确的位置。例如,DetailsView.ModeChanged
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-02-08
  • 1970-01-01
  • 2023-03-21
  • 1970-01-01
  • 1970-01-01
  • 2023-04-07
  • 1970-01-01
相关资源
最近更新 更多