c#2nd下拉列表无法保留所选项目

时间:2012-05-15 08:09:50

标签: c# asp.net drop-down-menu

这是我第一次遇到这个问题所以请任何人帮助我。 这是我的问题我有2个下拉列表,第二个将从第一个下拉列表中的选定项目填充,而在第二个下拉列表中,它将填充gridview取决于所选项目。现在的问题是,每当我从第二个ddl中选择任何项目时它会回发,而第一个项目将被选中而不是我选择的项目。

继承我的代码:(aspx)

                   <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);
    }

请帮助我。谢谢!

2 个答案:

答案 0 :(得分:2)

如果您的意思是第一个下拉列表选中了第一个项目,请使用EnableViewState="true"

ddlBranch属性进行检查

为什么会这样?

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

答案 1 :(得分:1)

您需要做的是在事件处理程序

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

selectedIndex属性设置为当前所选项目的index