In my codebehind i created a checkboxlist from sql db in my Page_Load event and in my button_click event did all the get values from checkboxlist etc.
So when i checked some checkboxes and then clicked my button the first thing that happend was that my page_load event recreated the checkboxlist thus not having any boxes checked when it ran my get checkbox values...
I've missed to add in the page_load event the if (!this.IsPostBack)
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
// db query and create checkboxlist and other
SqlConnection dbConn = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnString"].ConnectionString);
string query;
try
{
query = "SELECT [name], [mail] FROM [users]";
dbConn.Open();
SqlDataAdapter da = new SqlDataAdapter(query, dbConn);
DataSet ds = new DataSet();
da.Fill(ds);
if (ds.Tables[0].Rows.Count != 0)
{
checkboxlist1.DataSource = ds;
checkboxlist1.DataTextField = "name";
checkboxlist1.DataValueField = "mail";
checkboxlist1.DataBind();
}
else
{
Response.Write("No Results found");
}
}
catch (Exception ex)
{
Response.Write("<br>" + ex);
}
finally
{
dbConn.Close();
}
}
}
protected void btnSend_Click(object sender, EventArgs e)
{
string strChkBox = string.Empty;
foreach (ListItem li in checkboxlist1.Value)
{
if (li.Selected == true)
{
strChkBox += li.Value + "; ";
// use only strChkBox += li + ", "; if you want the name of each checkbox checked rather then it's value.
}
}
Response.Write(strChkBox);
}
And the output was as expected, a semicolon separeted list for me to use in a mailsend function:
[email protected]; [email protected]; [email protected]
A long answer to a small problem. Please note that i'm far from an expert at this and know that there are better solutions then this but it might help out for some.