无法访问动态创建的选中复选框列表

本文关键字:复选框 列表 创建 访问 动态 | 更新日期: 2023-09-27 17:49:33

我找了很多次都没找到,最后还是来到了这里…

我正在成功地从数据库动态地创建一个复选框列表。然后我想提交这些选中的复选项,我无法访问。请查找代码....

protected void CreateCheckBoxListDynamically()
{
    DataTable dt = new DataTable();
    SqlConnection dBConnection = null;
    try
    {
        dBConnection = new SqlConnection();
        dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
        SqlDataAdapter dataAdapter = new SqlDataAdapter();
        SqlCommand cmd = new SqlCommand("SP_GetDomesticCountryList", dBConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        if (ddlTournamentType.SelectedValue != "Select" && ddlTournamentType.SelectedValue != "")
            cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
        else
            cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = DBNull.Value;
        dBConnection.Open();
        dataAdapter.SelectCommand = cmd;
        dataAdapter.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            CheckBoxList cblCountry = new CheckBoxList();
            cblCountry.ID = "cblCountryTeam";
            cblCountry.DataTextField = dt.Columns["CT_CountryTeamName"].ToString();
            cblCountry.DataValueField = dt.Columns["CT_CountryTeamId"].ToString();
            cblCountry.DataSource = dt;
            cblCountry.DataBind();
            //phCheckBoxList.Attributes.Add("class", "groupbox"); 
            phCheckBoxList.Controls.Add(cblCountry);
            phCheckBoxList.Controls.Add(new LiteralControl("<br />"));
        }
        dBConnection.Close();
    }
    catch (Exception Ex)
    {
        throw Ex;
    }
    finally
    {
        // Close data reader object and database connection
        if (dBConnection.State == ConnectionState.Open)
            dBConnection.Close();
    }
}
protected void btnInsert_Click(object sender, EventArgs e)
{
    SqlCommand cmd = null;
    SqlConnection dBConnection = new SqlConnection();
    SqlDataAdapter dataAdapter = new SqlDataAdapter();
    string countryTeamIds = "";
    try
    {
        dBConnection.ConnectionString = ConfigurationManager.ConnectionStrings["***"].ConnectionString;
        cmd = new SqlCommand("SP_AddNewSeries", dBConnection);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.Add("@MemberId", SqlDbType.Int).Value = memberId;
        if (ddlTournamentType.SelectedValue != "Select")
            cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = Convert.ToInt32(ddlTournamentType.SelectedValue);
        else
            cmd.Parameters.Add("@TournamentId", SqlDbType.Int).Value = DBNull.Value;
        if (!string.IsNullOrEmpty(txtSeriesStartDate.Text))
            cmd.Parameters.Add("@SeriesStartDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesStartDate.Text);
        else
            cmd.Parameters.Add("@SeriesStartDate", SqlDbType.Date).Value = DBNull.Value;
        if (!string.IsNullOrEmpty(txtSeriesEndDate.Text))
            cmd.Parameters.Add("@SeriesEndDate", SqlDbType.Date).Value = Convert.ToDateTime(txtSeriesEndDate.Text);
        else
            cmd.Parameters.Add("@SeriesEndDate", SqlDbType.Date).Value = DBNull.Value;
        // get values from dynamic controls
        CheckBoxList cb = (CheckBoxList)phCheckBoxList.FindControl("cblCountryTeam");
        if (cb != null)
        {
            foreach (ListItem li in cb.Items)
            {
                if (li.Selected)
                    countryTeamIds += li.Value + "~";
            }
        }
        if (!string.IsNullOrEmpty(countryTeamIds))
            cmd.Parameters.Add("@CountryTeamIds", SqlDbType.NVarChar).Value = countryTeamIds;
        else
            cmd.Parameters.Add("@CountryTeamIds", SqlDbType.NVarChar).Value = DBNull.Value;
        dBConnection.Open();
        dataAdapter.InsertCommand = cmd;
        int i = cmd.ExecuteNonQuery();
        //hdnseriesId.Value = cmd.Parameters["@SeriesId"].Value.ToString();
        if (i == 0)
        {
            msgNoRecords.Visible = true;
        }
        else
        {
            msgNoRecords.Visible = false;
            //Response.Redirect("~/country.aspx", false);
        }
        dBConnection.Close();
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        // Close data reader object and database connection
        cmd.Dispose();
        cmd = null;
        if (dBConnection.State == ConnectionState.Open)
            dBConnection.Close();
    }
}

无法访问动态创建的选中复选框列表

根据您的代码细节对象phCheckBoxList已在aspx页面中创建。如果我是对的,那么这将在服务器端添加复选框时产生问题。

为什么不在服务器端只创建复选框而不是checkboxLIST呢?创建复选框后,您可以添加<div>(这必须在aspx页面上声明)。这个<div>应该有一个ID,并设置为runat="server",这样你就可以得到这个。

完成上述任务后,您可以轻松地在服务器端代码中找到该div中的每个复选框。