从gridview中添加文本并对其进行排序

时间:2016-01-11 06:12:18

标签: c# asp.net c#-4.0

我已经在ASP.NET应用程序中编写了代码,如

  protected void chkbox_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox chkbox = sender as CheckBox;
        GridViewRow currentRow = chkbox.NamingContainer as GridViewRow;
        Label lblJurisdiction = grdView.Rows[currentRow.RowIndex].FindControl("lblJurisdiction") as Label;
        RequiredFieldValidator rfv = grdView.Rows[currentRow.RowIndex].FindControl("ValReqED") as RequiredFieldValidator;
        RequiredFieldValidator rfvExpD = grdView.Rows[currentRow.RowIndex].FindControl("ValReqExpD") as RequiredFieldValidator;
        RequiredFieldValidator rfvLN = grdView.Rows[currentRow.RowIndex].FindControl("ValReqLN") as RequiredFieldValidator;
        if (!chkbox.Checked)
        {
            rfv.Enabled = false;
            rfvExpD.Enabled = false;
            rfvLN.Enabled = false;
            string JurisdictionRemoved = lblJurisdiction.Text + ",";
            string Jurisdiction = txtJurisdiction.Text;
            string ReplacedJurisdiction = Jurisdiction.Replace(JurisdictionRemoved, "");
            txtJurisdiction.Text = ReplacedJurisdiction;
        }
        else
        {
            string Jurisdiction = txtJurisdiction.Text;
            txtJurisdiction.Text = Jurisdiction + "," + lblJurisdiction.Text.ToString(); 
        }
    }         

请注意,字符串Jurisdictions包含一组诸如

之类的司法管辖区
     Delaware,Georgia,Illinois,Kansas,Maine

字符串JurisdictionRemoved只包含一个管辖区(用户将取消选中该复选框)

   Georgia

然后txtJurisdicton必须包含

     Delaware,Illinois,Kansas,Maine

如果字符串JurisdictionRemoved包含“Maine”,则上面的代码不起作用,则不会删除。

现在我希望每当用户取消选中要从txtJurisdiction文本框中删除管辖权的复选框时,每当用户检查复选框时,相应的权限应该附加到txt辖区。让我们假设用户再次检查了“Georgia” 那么文本框应该包含

       Delaware,Georgia,Illinois,Kansas,Maine   

上面的代码不能像这样工作。此外,我希望它按字母顺序在文本框中附加检查的管辖权,并应消除冗余状态。请帮帮我!!!

1 个答案:

答案 0 :(得分:0)

要解决此问题,您可以这样做

  1. 使用append函数
  2. 将conman分隔列表拆分为数组
  3. 使用条件
  4. 从数组中删除文本
  5. 用户split方法创建新字符串。
  6. 将该值分配给您的文本框。
  7. 实施例

    String.Join