无法访问动态创建的选定CheckBoxList

时间:2012-12-25 08:08:30

标签: c# asp.net dynamically-generated

我搜索了很多但未能到达终于来到这里......

我正在从数据库中成功创建一个checkboxList。然后我想提交这些选中的已检查项目,我无法访问。请找到代码......

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

1 个答案:

答案 0 :(得分:0)

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

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

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