C# 如何在选择项值之前加载sql数据源
我的asp.net页面上有SQL数据源,加载页面时出现此错误 “ddlTypes”具有一个SelectedValue,该值无效,因为它不在项目列表中。参数名称:valueC# 如何在选择项值之前加载sql数据源,c#,asp.net,sql,datasource,C#,Asp.net,Sql,Datasource,我的asp.net页面上有SQL数据源,加载页面时出现此错误 “ddlTypes”具有一个SelectedValue,该值无效,因为它不在项目列表中。参数名称:value private void GetQueryString(Uri tempUri) { if (HttpUtility.ParseQueryString(tempUri.Query).Get("IntrestFocus") != null) { ddlTopics.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>
private void GetQueryString(Uri tempUri)
{
if(HttpUtility.ParseQueryString(tempUri.Query).Get(“IntrestFocus”)!=null)
{
ddlTopics.SelectedValue=HttpUtility.ParseQueryString(tempUri.Query.Get(“IntrestFocus”);
}
其他的
if(HttpUtility.ParseQueryString(tempUri.Query).Get(“Skills”)!=null)
{
ddlSkills.SelectedValue=HttpUtility.ParseQueryString(tempUri.Query.Get(“技能”);
}
其他的
if(HttpUtility.ParseQueryString(tempUri.Query).Get(“Type”)!=null)
{
ddlTypes.SelectedValue=HttpUtility.ParseQueryString(tempUri.Query.Get(“Type”);
}
}
选择一种类型
这是您的主要选择:
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
逻辑放在其中。它将在下拉列表
数据绑定后触发,然后:
您的代码隐藏:
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" ...
您是否已验证SelectedValue属性设置的值是否确实在绑定到该属性的项目的值列表中???您能否只发布下拉列表的客户端标记?另外,这里有3个ddl,哪一个导致了错误?要绑定dropdownlist,我使用asp:SqlDataSource