下拉列表选择

发布于 2024-10-13 04:23:22 字数 109 浏览 3 评论 0原文

我有三个下拉列表控件。每个下拉列表包含静态值 1,2,3,4 我需要做的是,当在第一个下拉列表中选择一个项目(在 SelectedIndexChanged 事件上)时,应在其他两个下拉列表中选择相同的项目

I have three DropDown List Controls.each dropdown contains the static values 1,2,3,4
what i need to do is when a item is selected on the first dropdown(on SelectedIndexChanged Event) same items should be selected in other two dropdowns

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

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

发布评论

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

评论(4

遗弃M 2024-10-20 04:23:22

这就是你要做的。

在 ASPX 代码中:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>

在代码隐藏中:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    string value = ddl.SelectedValue;

    SetValue(DropDownList1, value);
    SetValue(DropDownList2, value);
    SetValue(DropDownList3, value);
}

protected void SetValue(DropDownList ddl, string value)
{
    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}

仅键入 ddl1.SelectedValue = ddl2.SelectedValue 不起作用,因为 DropDownList.SelectedValue 是只读的。

请注意,我不仅仅将所有 DDL 的 SelectedIndex 设置为发送者的。您可以在示例场景中使用它,但是如果您的某个 DDL 的 ListItem 的顺序与其他 DDL 的顺序不同,则代码将会中断。在我看来,这使得这种做法很危险,但是 YMMV。

另外,如果您决定推广 SetValue 方法(我经常将其作为扩展方法添加到整个项目中的 DropDownList 控件中),则应该处理在DDL,大概是通过抛出异常。您还可以使用 FindByText 制作 SetText 版本。

Here's what you do.

In the ASPX code:

<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList2" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>
<asp:DropDownList ID="DropDownList3" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddl_SelectedIndexChanged">
    <asp:ListItem Text="1" Value="1" />
    <asp:ListItem Text="2" Value="2" />
    <asp:ListItem Text="3" Value="3" />
    <asp:ListItem Text="4" Value="4" />
</asp:DropDownList>

In the codebehind:

protected void ddl_SelectedIndexChanged(object sender, EventArgs e)
{
    DropDownList ddl = (DropDownList)sender;
    string value = ddl.SelectedValue;

    SetValue(DropDownList1, value);
    SetValue(DropDownList2, value);
    SetValue(DropDownList3, value);
}

protected void SetValue(DropDownList ddl, string value)
{
    ddl.SelectedIndex = ddl.Items.IndexOf(ddl.Items.FindByValue(value));
}

Just typing ddl1.SelectedValue = ddl2.SelectedValue won't work, because DropDownList.SelectedValue is read-only.

Note that I didn't just set the SelectedIndex of all the DDLs to that of the sender. You can use that in your example scenario, but if one of your DDLs ever has its ListItems in a different order from the others, the code will break. In my opinion, this makes it dangerous practice, but YMMV.

Also, if you decide to generalize the SetValue method (I often add it as an extension method to the DropDownList control across the entire project), you should handle cases where the target value isn't found in the DDL, presumably by throwing an exception. You can also make a SetText version using FindByText.

·深蓝 2024-10-20 04:23:22

将第二个和第三个下拉列表中的 selectedvalue 属性设置为第一个下拉列表中的选定值。将DropDown设置为autopostback =“true”,以便它回发到服务器,您可以适当地设置它。

或者,当第一个更改客户端事件触发时,使用客户端 JavaScript 更改其他选择元素上的 selectedindex 属性。

Set the selectedvalue property on 2nd and 3rd drop down to the selected value on the first. Set the DropDown to autopostback="true" so that it posts back to the server, and you can set it appropriately.

OR, use client-side JavaScript to change the selectedindex property on the other select elements when the change client event fires for the first.

感情旳空白 2024-10-20 04:23:22

您需要做的就是设置 <其他下拉列表的 code>SelectedIndex 如下所示:

protected void DropDownList1SelectedIndexChanged(Object sender, EventArgs e)
{ 
   dropDownList2.SelectedIndex = dropDownList1.SelectedIndex;
   dropDownList3.SelectedIndex = dropDownList1.SelectedIndex;
}

All you need to do is set the SelectedIndex of your other drop down lists like this:

protected void DropDownList1SelectedIndexChanged(Object sender, EventArgs e)
{ 
   dropDownList2.SelectedIndex = dropDownList1.SelectedIndex;
   dropDownList3.SelectedIndex = dropDownList1.SelectedIndex;
}
世态炎凉 2024-10-20 04:23:22

尝试以下操作:

标记:

<asp:DropDownList ID="dd1" OnSelectedIndexChanged="dd1_SelectedIndexChanged" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="dd2" runat="server" />
<asp:DropDownList ID="dd3" runat="server" />

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        BindData();
}

protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
    string selected = dd1.SelectedValue;

    dd1.SelectedValue = dd2.SelectedValue = dd3.SelectedValue = selected;

    BindData();
}

private void BindData()
{
    int[] values = { 1, 2, 3, 4 };

    dd1.DataSource = dd2.DataSource = dd3.DataSource = values;

    dd1.DataBind();
    dd2.DataBind();
    dd3.DataBind();
}

Try the following:

Markup:

<asp:DropDownList ID="dd1" OnSelectedIndexChanged="dd1_SelectedIndexChanged" AutoPostBack="true" runat="server" />
<asp:DropDownList ID="dd2" runat="server" />
<asp:DropDownList ID="dd3" runat="server" />

Code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
        BindData();
}

protected void dd1_SelectedIndexChanged(object sender, EventArgs e)
{
    string selected = dd1.SelectedValue;

    dd1.SelectedValue = dd2.SelectedValue = dd3.SelectedValue = selected;

    BindData();
}

private void BindData()
{
    int[] values = { 1, 2, 3, 4 };

    dd1.DataSource = dd2.DataSource = dd3.DataSource = values;

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