如何在选择项值之前加载sql数据源

时间:2013-08-27 19:16:39

标签: c# asp.net sql datasource

我的asp.net页面上有SQL dataSource,并且在加载页面时出现此错误

'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>

1 个答案:

答案 0 :(得分: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" ...

后者可能是最好的选择。