0

我的 asp.net 页面上有 SQL 数据源,页面加载时出现此错误

“ddlTypes”有一个无效的 SelectedValue,因为它不存在于项目列表中。参数名称:值

private void GetQueryString(Uri tempUri)
{
    if (HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus") != null)
    {
        ddlTopics.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus"); 
    }

    else
        if (HttpUtility.ParseQueryString(tempUri.Query).Get("Skills") != null)
        {
            ddlSkills.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Skills"); 
        }
    else
        if(HttpUtility.ParseQueryString(tempUri.Query).Get("Type") != null)
        {
            ddlTypes.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Type"); 
        }

}
<asp:SqlDataSource ID="SqlDSTypes" runat="server" ConnectionString="<%$ ConnectionStrings:SiteSqlServer %>" SelectCommand="SELECT * FROM [Types]"></asp:SqlDataSource>


                <asp:DropDownList CssClass="selectpicker mobile-stack select-type" data-style="lts-white font-black drop-shadow select-height" ID="ddlTypes" runat="server" DataSourceID="SqlDSTypes" DataTextField="Description" DataValueField="Id" AppendDataBoundItems="True" ClientIDMode="Static">
                    <asp:ListItem Value="0">Select a Type</asp:ListItem>
                </asp:DropDownList>
4

1 回答 1

0

这是您的主要选择:

private void GetQueryString(Uri tempUri)
{
  ddlTopics.DataSource = SqlDSTypes.Select(DataSourceSelectArguments.Empty);
  ddlTopics.DataBind();

  //Put your logic here, behind databind ...
  if (HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus") != null)
  {
      ddlTopics.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus"); 
  }

  else
      if (HttpUtility.ParseQueryString(tempUri.Query).Get("Skills") != null)
      {
        ddlSkills.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Skills"); 
      }
  else
      if(HttpUtility.ParseQueryString(tempUri.Query).Get("Type") != null)
      {
        ddlTypes.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Type"); 
      }

}

当您选择上述答案时,还要确保未设置 DataSourceID。

但是,还有另一种可能性,即使用OnDataBound事件并将您的DropDownList逻辑放入其中。它将在DropDownList数据绑定之后被触发:

您背后的代码:

protected function ddlTopics_DataBound(Object sender, EventArgs e)
{
  //Put your logic here, behind databind ...
  if (HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus") != null)
  {
      ddlTopics.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus"); 
  }

  else
      if (HttpUtility.ParseQueryString(tempUri.Query).Get("Skills") != null)
      {
        ddlSkills.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Skills"); 
      }
  else
      if(HttpUtility.ParseQueryString(tempUri.Query).Get("Type") != null)
      {
        ddlTypes.SelectedValue = HttpUtility.ParseQueryString(tempUri.Query).Get("Type"); 
      }  
}

你的aspx:

<asp:DropDownList ID="ddlTopics" OnDataBound="ddlTopics_DataBound" runat="server" ...

后者可能是最简洁的选择。

于 2013-08-27T20:00:23.190 回答