2012-12-25 35 views
-1

我寻觅了很多,并没有最后抵达这里...无法访问选定的CheckBoxList这是动态创建

我从成功的数据库中创建一个动态的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(); 
    } 
} 
+0

你有什么错误吗? – Karthik

+0

不,没有错误,但我将CheckBoxList cb作为null按钮单击事件。 –

回答

0

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

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

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

+0

是的Darshan ...这可能是一个很好的方法来解决这个问题,但我不知道复选框控件的确切数量。有时它可能超过50,这就是我选择checkboxList的原因。 是的你是对的,我已经在aspx页面创建了phCheckBoxList。 –

+0

噢好吧我想我得到了确切的问题,你可以调用你的方法'CreateCheckBoxListDynamically()'在页面加载事件也然后尝试访问这些控件它可以帮助你。 – Darshan

+0

其实CreateCheckBoxListDynamically()是基于另一个下拉,所以我打电话在dropdown_SelectedIndexChanged函数.... –