为什么在asp.net中没有选中checkboxList?

时间:2016-11-12 06:09:24

标签: asp.net

此代码单独工作,但当我在其他页面上集成时,它不允许复选框列表保持选中状态...我已尝试删除updatepanel ...仍未获得输出

<asp:UpdatePanel runat="server" >
                    <ContentTemplate>
                        <asp:CheckBoxList ID="chkCountries" EnableViewState="true" runat="server"  OnSelectedIndexChanged="Country_Selected"   AutoPostBack="true" CausesValidation="false" runat="server">
                            <asp:ListItem Text="Allopathy" Value="Allopathy" ></asp:ListItem>
                            <asp:ListItem Text="Ayurvedic" Value="Ayurvedic"></asp:ListItem>
                            <asp:ListItem Text="Cardiologist" Value="Cardiologist"></asp:ListItem>
                            <asp:ListItem Text="Dentist" Value="Dentist"></asp:ListItem>
                            <asp:ListItem Text="industrial" Value="industrial"></asp:ListItem>
                        </asp:CheckBoxList>
                    </ContentTemplate>


                </asp:UpdatePanel>
                <hr />
                <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CssClass="grid"
                    AllowPaging="true" OnPageIndexChanging="OnPageIndexChanging">
                    <Columns>
                        <asp:BoundField HeaderText="Contact Name" DataField="subcategory_id" />
                        <asp:BoundField HeaderText="Country" DataField="subcategory_name" />
                    </Columns>
                </asp:GridView>

enter code here

protected void Page_Load(object sender,EventArgs e)     {         位置();

    if (!IsPostBack)
    {
        second_repeater();

      this.  BindGrid();
    }


}




private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["publicity"].ConnectionString;
    string query = "SELECT subcategory_id,subcategory_name FROM subcategories";

    string condition = string.Empty;
    foreach (ListItem item in chkCountries.Items)
    {
        condition += item.Selected ? string.Format("'{0}',", item.Value) : string.Empty;
    }

    if (!string.IsNullOrEmpty(condition))
    {
        condition = string.Format(" WHERE subcategory_name IN ({0})", condition.Substring(0, condition.Length - 1));
    }

    using (SqlConnection con = new SqlConnection(constr))
    {
        using (SqlCommand cmd = new SqlCommand(query + condition))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                cmd.Connection = con;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}

protected void Country_Selected(object sender, EventArgs e)
{
    this.BindGrid();
}

0 个答案:

没有答案