如何在中继器内更改下拉列表的值

本文关键字:下拉列表 中继器 | 更新日期: 2023-09-27 17:59:08

我在中继器内有四列,Expected、Accpet、DDL&一个在页面加载上不可见的面板

如果Expect列值和下拉选择值相同,那么只有我想看到那个面板,否则就不想了。

请帮助我,我没有得到下拉列表的值,因为在选择更改时,整个中继器都在重新填充,我无法得到一个值。有人能帮我吗。

任何可能的方式都可以。

CSS

<style>
        .main
        {
            width: 100%;
        }
        .inner
        {
            width: 90%;
        }
        .fst
        {
            width: 20%;
            float: left;
        }
        .scnd
        {
            width: 20%;
            float: left;
        }
        .thrd
        {
            width: 20%;
            float: left;
        }
        .frth
        {
            width: 20%;
            float: left;
        }
        .reset-this
        {
            clear:both;
        }
    </style>

HTML

<div class="main">
        <div class="inner">
            <div class="fst">
                Except
            </div>
            <div class="scnd">
                Accpet
            </div>
            <div class="thrd">
                DDL
            </div>
            <div class="frth">
                Panel
            </div>
            <div class="reset-this">
            </div>
        </div>
        <div class="reset-this">
        </div>
        <br />
        <asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound">
            <ItemTemplate>
                <div class="inner">
                    <div class="fst">
                        <asp:Label ID="Label1" runat="server" Text='<%# Eval("Expected") %>'></asp:Label>
                    </div>
                    <div class="scnd">
                        <asp:Label ID="Label2" runat="server" Text='<%# Eval("accpet") %>'></asp:Label>
                    </div>
                    <div class="thrd">
                        <asp:DropDownList ID="DropDownList3" AutoPostBack="true" runat="server" OnSelectedIndexChanged="DropDownList3_SelectedIndexChanged">
                            <asp:ListItem Text="Select" Value="-1"></asp:ListItem>
                            <asp:ListItem Text="50"></asp:ListItem>
                            <asp:ListItem Text="51"></asp:ListItem>
                            <asp:ListItem Text="52"></asp:ListItem>
                            <asp:ListItem Text="53"></asp:ListItem>
                            <asp:ListItem Text="100"></asp:ListItem>
                        </asp:DropDownList>
                    </div>
                    <div class="frth">
                        <asp:Panel ID="Panel1" runat="server">
                            <asp:DropDownList ID="DropDownList2" runat="server">
                                <asp:ListItem Text="Succesful"></asp:ListItem>
                            </asp:DropDownList>
                        </asp:Panel>
                    </div>
                    <div class="reset-this">
                    </div>
                    <br />
                </div>
            </ItemTemplate>
        </asp:Repeater>
    </div>

CodeBehind

protected void Page_Load(object sender, EventArgs e)
    {
        GetDAtaset();
    }
    public void GetDAtaset()
    {
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("warehouseTable");
        dt.Columns.Add(new DataColumn("Expected", typeof(string)));
        dt.Columns.Add(new DataColumn("accpet", typeof(string)));
        DataRow dr = dt.NewRow();
        dr["Expected"] = "100";
        dr["accpet"] = "50";
        dt.Rows.Add(dr);

        DataRow dr1 = dt.NewRow();
        dr1["Expected"] = "50";
        dr1["accpet"] = "25";
        dt.Rows.Add(dr1);
        ds.Tables.Add(dt);
        Repeater1.DataSource = ds;
        Repeater1.DataBind();
    }
    protected void Repeater1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {
        ((Panel)(e.Item.FindControl("panel1"))).Visible = false;
    }
    protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
    {
        for (int i = 0; i < Repeater1.Items.Count; i++)
        {
            Panel pnl_active = Repeater1.Items[i].FindControl("panel1") as Panel;
            Label Expected = Repeater1.Items[i].FindControl("Label1") as Label;
            DropDownList DDLValue = Repeater1.Items[i].FindControl("DropDownList3") as DropDownList;
            if (DDLValue.SelectedItem.ToString() == Expected.ToString())
            {
                pnl_active.Visible = true;
            }
        }
    }

如何在中继器内更改下拉列表的值

您需要做几件事:

首先,将其添加到Page_Load方法中,以避免重置中继器的值:

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

接下来,将DropDownList3_SelectedIndexChanged方法修改为这样:

protected void DropDownList3_SelectedIndexChanged(object sender, EventArgs e)
{
    for (int i = 0; i < Repeater1.Items.Count; i++)
    {
        Panel pnl_active = Repeater1.Items[i].FindControl("panel1") as Panel;
        Label Expected = Repeater1.Items[i].FindControl("Label1") as Label;
        DropDownList DDLValue = Repeater1.Items[i].FindControl("DropDownList3") as DropDownList;
        //You need to compare the SelectedItem.Text with the label Expected.Text
        if (DDLValue.SelectedItem.Text == Expected.Text)
        {
            pnl_active.Visible = true;
        }
    }
}

请注意,您正在将DDLValue.SelectedItem与标签Expected进行比较,这是不正确的,因为您需要将DDLValue.ElectedItem.Text与标签Expected.Text.进行比较

希望这能有所帮助。