将变量传递给动态下拉列表

发布于 2024-11-03 14:12:10 字数 1996 浏览 1 评论 0原文

我有 2 个下拉列表,第一个是州,第二个是城市。我正在尝试创建它,以便当用户单击“州”时,第二个下拉列表将填充数据表中的城市名称。

这是代码

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<%--<cc2:CascadingDropDown ID="CascadingDropDown1" runat="server"> </cc2:CascadingDropDown>--%>
<h1>Live Event Search Engine</h1><br />
State: <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" /><asp:SqlDataSource
    ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
    SelectCommand="SELECT [ST_Code] FROM [State]">
    </asp:SqlDataSource>
City:  <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" /><asp:SqlDataSource
    ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
    SelectCommand="web_PublicProgramListbyState" SelectCommandType="StoredProcedure">
<SelectParameters>
        <asp:SessionParameter Name="State" SessionField="ST_Code" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
       <asp:Button ID="Submit" runat="server" Text="Submit" />

</asp:Content>

,后面的代码是

Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlState.SelectedIndexChanged
        Me.ddlCity.Items.Clear()
        Dim da As New SqlDataAdapter("web_PublicProgramListbyState", New SqlConnection(ConfigurationManager.AppSettings("dbConnection")))
        Dim ds As New DataSet
        da.Fill(ds, "@State")
        da.Dispose()
        Me.ddlCity.DataSource = ds.Tables("Products")
        Me.ddlCity.DataTextField = "ProductName"
        Me.ddlCity.DataValueField = "ProductID"
        Me.ddlCity.DataBind()
    End Sub
End Class

I have 2 dropdownlists, one is State and the second is City. I am trying to create it so, when a user clicks the State, the second dropdownlist is populated with City names from a datatable.

Here is the code

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<%--<cc2:CascadingDropDown ID="CascadingDropDown1" runat="server"> </cc2:CascadingDropDown>--%>
<h1>Live Event Search Engine</h1><br />
State: <asp:DropDownList ID="ddlState" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="ST_Code" DataValueField="ST_Code" /><asp:SqlDataSource
    ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
    SelectCommand="SELECT [ST_Code] FROM [State]">
    </asp:SqlDataSource>
City:  <asp:DropDownList ID="ddlCity" runat="server" DataSourceID="SqlDataSource2" DataTextField="RS_City" DataValueField="RS_City" /><asp:SqlDataSource
    ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:dbConnection %>"
    SelectCommand="web_PublicProgramListbyState" SelectCommandType="StoredProcedure">
<SelectParameters>
        <asp:SessionParameter Name="State" SessionField="ST_Code" Type="String" />
    </SelectParameters>
</asp:SqlDataSource>
       <asp:Button ID="Submit" runat="server" Text="Submit" />

</asp:Content>

and the code behind is

Public Sub ddlState_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlState.SelectedIndexChanged
        Me.ddlCity.Items.Clear()
        Dim da As New SqlDataAdapter("web_PublicProgramListbyState", New SqlConnection(ConfigurationManager.AppSettings("dbConnection")))
        Dim ds As New DataSet
        da.Fill(ds, "@State")
        da.Dispose()
        Me.ddlCity.DataSource = ds.Tables("Products")
        Me.ddlCity.DataTextField = "ProductName"
        Me.ddlCity.DataValueField = "ProductID"
        Me.ddlCity.DataBind()
    End Sub
End Class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

初见 2024-11-10 14:12:10

当您已经定义了 SqlDataSource 时,您似乎正在尝试将数据集引入其中。只需修改 SqlDataSource 的参数并重新绑定:

Public Sub ddlState_SelectedIndexChanged(...)
    SqlDataSource2.SelectParameters.Clear()
    SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
    ddlCity.DataBind()
End Sub

编辑:或者您可以使用在 SqlDataSource2.SelectParameters 中引用 ddlState.SelectedValue 的 ControlParameter,如另一个答案中所述。唯一的技巧是您必须仔细管理默认值,以便 ddlCity 仅在您需要时才进行绑定。

Looks like you are trying to bring a dataset into this when you already have a SqlDataSource defined. Just modify the parameters of the SqlDataSource and re-bind:

Public Sub ddlState_SelectedIndexChanged(...)
    SqlDataSource2.SelectParameters.Clear()
    SqlDataSource2.SelectParameters.Add(New Parameter("@State", DbType.String, ddlState.SelectedValue))
    ddlCity.DataBind()
End Sub

Edit: Or you can use a ControlParameter referencing ddlState.SelectedValue in SqlDataSource2.SelectParameters as mentioned in another answer. Only trick there is you have to manage your default values carefully so ddlCity only binds when you want it to.

我要还你自由 2024-11-10 14:12:10

我会将其更改为控制参数并调用数据绑定。无需自己填写。

 <asp:ControlParameter Name="State" ControlID="ddlState" Type="String" /> 

然后在选择事件中只需调用:

Me.ddlCity.DataBind()

或者,如果您想删除所有隐藏代码,请将其放入带有触发器的更新面板中。

I would change it to a control param and just call the databind. No need to do the fill yourself.

 <asp:ControlParameter Name="State" ControlID="ddlState" Type="String" /> 

and then in the select event just call:

Me.ddlCity.DataBind()

Or if you want to remove the codebehind all together put it in an update panel with a trigger.

故事↓在人 2024-11-10 14:12:10

我没有看到 ddlState_SelectedIndexChanged 事件在哪里写入会话变量。这是必需的。

I don't see where the ddlState_SelectedIndexChanged event writes the session variable. This is required.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文