C#第二届下拉列表不能保留所选项目(c# 2nd Dropdown list cant retain

2019-09-17 05:26发布

它的我第一次遇到这样的问题,所以请人帮我。 这里是我的问题,我有2个下拉列表,第二个将被从所选的项目,第一个下拉列表填充,并在第二下拉列表,将填充的GridView取决于所选择的项目。 现在的问题是,每次我选择从第2任一项目DDL回发和的第一个项目将被替代选择,我选择的项目。

继承人我的代码:(C ++)

                   <asp:TableRow> <%--Branch--%>
                        <asp:TableCell HorizontalAlign="Left">
                            <asp:Label ID="lblBranch" runat="server" Font-Bold="true">Branch:</asp:Label>
                        </asp:TableCell>
                        <asp:TableCell HorizontalAlign="Left">
                            <asp:DropDownList ID="ddlBranch" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlBranch_SelectedIndexChanged" AppendDataBoundItems="true" />
                        </asp:TableCell>
                    </asp:TableRow>

                    <asp:TableRow> <%--Recepient--%>
                        <asp:TableCell HorizontalAlign="Left">
                            <asp:Label ID="lblRecepient" runat="server" Font-Bold="true">Recepient:</asp:Label>
                        </asp:TableCell>
                        <asp:TableCell HorizontalAlign="left">
                            <asp:DropDownList ID="ddlRecepientDepartment" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlRecepientDepartment_SelectedIndexChanged"  AppendDataBoundItems="true" EnableViewState="true"></asp:DropDownList>
                        </asp:TableCell>
                    </asp:TableRow>

                    <asp:TableRow> <%--Gridview Recepient--%>
                        <asp:TableCell></asp:TableCell>
                        <asp:TableCell>
                            <asp:GridView ID="gvRecepientPosition" runat="server" AllowPaging="True" AutoGenerateColumns="False"
                                EmptyDataText="No Record found!" ShowFooter="false" ShowHeader="true" Width="99%" HorizontalAlign="Center" PageSize="5" OnPageIndexChanging="gvRecepientPosition_PageIndexChanging">
                                <Columns>
                                <%--<asp:TemplateField HeaderText="ID" Visible="false">
                                    <ItemTemplate>
                                    <asp:Label ID="lblID" runat="server" Text='<%# Bind("RECID") %>'></asp:Label>
                                    </ItemTemplate>
                                </asp:TemplateField>--%>
                                <asp:TemplateField HeaderText="Recepient" Visible="true">
                                     <ItemTemplate>
                                        <asp:CheckBox ID="cbRecepientPosition"  runat="server"/>
                                     </ItemTemplate>
                                </asp:TemplateField>
                                 <asp:BoundField DataField="Position" HeaderText="Position" ItemStyle-HorizontalAlign="Left" />
                                </Columns>
                            </asp:GridView>
                        </asp:TableCell>
                    </asp:TableRow>

后面的代码:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Page.IsPostBack)
        {
            ddlBranch.Items.Clear();
            GetBranch();

        }


    }

    private void GetBranch()
    {
             var objManager = new EmailNotificationInfoManager();
           // ddlBranch.Items.Clear();
            ddlBranch.DataSource = objManager.EmailNotificationInfoBranch();
            ddlBranch.DataTextField = "Branch";
            ddlBranch.DataValueField = "RECID";
            ddlBranch.DataBind();

            ddlBranch.Items.Insert(0,"<--Select-->");


    }

    protected void ddlBranch_SelectedIndexChanged(object sender, EventArgs e)
    {
        var objManager = new EmailNotificationInfoManager();
        ddlRecepientDepartment.Items.Clear();
        ddlRecepientDepartment.DataSource = objManager.EmailNotificationInfoDepartment(Convert.ToInt64(ddlBranch.SelectedValue));
        ddlRecepientDepartment.DataTextField = "Department";
        ddlRecepientDepartment.DataValueField = "branchID";
        ddlRecepientDepartment.DataBind();

        ddlRecepientDepartment.Items.Insert(0,"<--Select-->");

    }

    protected void ddlRecepientDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        var objManager = new EmailNotificationInfoManager();

        gvRecepientPosition.DataSource = objManager.GetPositionByDepartment(ddlRecepientDepartment.SelectedItem.Text);
        gvRecepientPosition.DataBind();

        Session["PositionDepartment"] = objManager.GetPositionByDepartment(ddlRecepientDepartment.SelectedItem.Text);

        ddlRecepientDepartment.Attributes.Add("onChange", "this.currentvalue = this.value;");
        //Page.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
    }

请帮我这一个。 谢谢!

Answer 1:

如果你的意思是第一个下拉越来越然后选择第一个项目,检查EnableViewState="true"的属性ddlBranch

为什么呢?

ddlRecepientDepartment.Attributes.Add("onChange", "this.currentvalue = this.value;");


Answer 2:

你所要做的是在事件处理程序

 protected void ddlRecepientDepartment_SelectedIndexChanged
 (object sender, EventArgs e)
 {}

设置selectedIndex属性到index当前选择的项目的。



文章来源: c# 2nd Dropdown list cant retain the selected item