简体   繁体   English

如何使用CheckBox实现从Asp.Net GridView向TextBox添加和删除收件人

[英]How to implement add & remove recipients from Asp.Net GridView to TextBox using CheckBox

I'm stuck into this problem where on a web application , I'm uploading contacts and retrieving them into a GridView . 我陷入了这个问题,在Web应用程序上 ,我正在上传联系人并将其检索到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 . 我正在使用CheckBoxesGridview中选择或取消选择移动电话号码并将所选号码存储到用逗号“,”分隔的多行TextBox中,以发送多个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 . 现在的问题是,当我取消选中 GridView中CheckBoxes时,无法从TextBox中删除添加的数字或字符串。 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删除未经检查的ID文本:

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 ): 这是完整的代码(请注意,我取消了您正在使用的一些不必要的代码,例如第三个else语句,因为复选框为truefalse ,所以永远无法访问它们):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM