I'm stuck into this problem where on a web application , I'm uploading contacts and retrieving them into a GridView . I'm using CheckBoxes to select or deselect the mobile numbers from the Gridview and storing the selected numbers into a multiline TextBox separated with commas "," to send multiple SMS . Now, the problem is that I'm not able to remove the added numbers or string from the TextBox when I'm unchecking the CheckBoxes in the GridView . Please help me through this problem. It's eating my time up!! Thanks in advance..
Below is my code:
protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
{
bool SomethingChecked = false;
CheckBox ChkBoxHeader = (CheckBox)gvcntct.HeaderRow.FindControl("chkboxSelectAll");
foreach (GridViewRow row in gvcntct.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
if (ChkBoxHeader.Checked == true)
{
ChkBoxRows.Checked = true;
}
else
{
ChkBoxRows.Checked = false;
TextBox2.Text = "";
}
}
if (!SomethingChecked)
{
TextBox2.Text = "";
}
}
You can remove the unchecked ID text from the TextBox2
by using:
TextBox2.Text.Replace(string.Format(", {0}", ID), "");
Here's the complete code (Note I reomved some unnecessary code your'e using, like the 3rd else
statement which can never be reached, since a checkbox is either true
or false
):
protected void chkboxSelectAll_CheckedChanged(object sender, EventArgs e)
{
TextBox2.Text = ""
CheckBox ChkBoxHeader = (CheckBox)gvcntct.HeaderRow.FindControl("chkboxSelectAll");
foreach (GridViewRow row in gvcntct.Rows)
{
CheckBox ChkBoxRows = (CheckBox)row.FindControl("chkEmp");
ChkBoxRows.Checked = ChkBoxHeader.Checked;
}
}
protected void chkEmp_CheckedChanged(object sender, EventArgs e)
{
System.Web.UI.WebControls.CheckBox CheckBox1 = (System.Web.UI.WebControls.CheckBox)sender;
GridViewRow row = (GridViewRow)CheckBox1.NamingContainer;
Label lblUser = (Label)row.FindControl("lblMake");
string ID = row.Cells[2].Text;
if (TextBox2.Text.Length > 0)
{
string[] Ids = TextBox2.Text.Split(',');
if (CheckBox1.Checked == true)
{
TextBox2.Text += string.Format(", {0}", ID);
}
else if (CheckBox1.Checked == false)
{
CheckBox ChkBoxHeader = (CheckBox)gvcntct.HeaderRow.FindControl("chkboxSelectAll");
foreach (GridViewRow rows in gvcntct.Rows)
{
CheckBox ChkBoxRows = (CheckBox)rows.FindControl("chkEmp");
ChkBoxHeader.Checked = false;
ChkBoxRows.Checked = false;
TextBox2.Text = TextBox2.Text
.Replace(string.Format(", {0}", ID), "")
.Replace(string.Format("{0}", ID), "");
}
}
else
{
TextBox2.Text = string.Empty;
int index = 0;
foreach (string s in Ids)
{
if (!s.Equals(","))
{
TextBox2.Text += string.Format("{0},", Ids[index]);
}
index++;
}
if (TextBox2.Text.Contains(","))
{
TextBox2.Text = TextBox2.Text.Replace(string.Format(", {0}", ID), "");
}
}
}
else
{
TextBox2.Text = ID;
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.