【问题标题】:Removing special characters from gridview data从 gridview 数据中删除特殊字符
【发布时间】:2013-02-03 22:00:35
【问题描述】:

我有一个 gridview object 用于绑定到 datasource。在gridviewselectedIndexChanging 事件上,我想将gridview 中显示的数据带入表单上的textboxes。但是,当数据包含诸如 &'"" 之类的字母数字字符时,每当我输入字母数字字符时,网格中的数据就会显示 ;amp、#S 等以及所有其他奇怪的字符。从网格中获取数据时,有没有办法防止这些字符在textboxes 中弹出? 我到目前为止的代码:

protected void grdActions_SelectedIndexChanged(object sender, EventArgs e) {

            int selectedRow1 = grdActions.SelectedRow.RowIndex;
            hdnIndexNo.Value = grdActions.Rows[selectedRow1].Cells[1].Text;
            ddlActionType.SelectedValue = grdActions.Rows[selectedRow1].Cells[3].Text;


            if (grdActions.Rows[selectedRow1].Cells[4].Text == null || grdActions.Rows[selectedRow1].Cells[4].Text == string.Empty || grdActions.Rows[selectedRow1].Cells[4].Text == " ")
            {
                txtDetails.Text = string.Empty;
            }
            else
            {
                txtDetails.Text = grdActions.Rows[selectedRow1].Cells[4].Text;
            }

            if (grdActions.Rows[selectedRow1].Cells[5].Text == null || grdActions.Rows[selectedRow1].Cells[5].Text == string.Empty || grdActions.Rows[selectedRow1].Cells[5].Text == " ")
            {
                txtCompletedDate.Text = string.Empty;
            }
            else
            {
                txtCompletedDate.Text = Convert.ToDateTime(grdActions.Rows[selectedRow1].Cells[5].Text).ToString("dd-MMM-yyyy");
            }

            ddlActionOwner.SelectedValue = grdActions.Rows[selectedRow1].Cells[7].Text;

            if (grdActions.Rows[selectedRow1].Cells[8].Text == null || grdActions.Rows[selectedRow1].Cells[8].Text == string.Empty || grdActions.Rows[selectedRow1].Cells[8].Text == " ")
            {
                txtAssignedTo.Text = string.Empty;
            }
            else
            {
                txtAssignedTo.Text = grdActions.Rows[selectedRow1].Cells[8].Text;
            }


            if (grdActions.Rows[selectedRow1].Cells[9].Text == null || grdActions.Rows[selectedRow1].Cells[9].Text == string.Empty || grdActions.Rows[selectedRow1].Cells[9].Text == " ")
            {
                lblComments.Visible = false;
                txtComments.Visible = false;
            }
            else
            {
                lblComments.Visible = true;
                txtComments.Visible = true;
                txtComments.Text = grdActions.Rows[selectedRow1].Cells[9].Text;
            }

【问题讨论】:

  • 请发布您迄今为止尝试过的内容...我的意思是代码。
  • 不要拒绝特殊字符。相反,您需要修复代码以便正确显示它们。请向我们展示您的代码。
  • 字母数字字符实际上是字符,既可以表示字母,例如 a 或 b,也可以表示数字。您的特殊字符 &'"" 不是字母数字。
  • 我为 gridview 的 selectedindexchanged 事件添加了代码。我曾尝试使用 server.htmlencode 为 txtComments 和 txtDetails 赋值。但是,对于字母数字字符,它给了我相同的结果。我正在考虑使用正则表达式函数来查找字母数字字符并替换它们......
  • 我在将文本从 gridview 转换为文本框时使用了 server.htmldecode(),它似乎可以工作。

标签: c# gridview alphanumeric


【解决方案1】:

在将数据从网格视图传输到文本框时,我使用了 Server.HTMLDecode()。这确保在将所有特殊字符发送回表单上的文本框之前删除所有特殊字符

【讨论】:

    猜你喜欢
    • 2014-05-15
    • 2023-03-25
    • 1970-01-01
    • 2011-08-29
    • 1970-01-01
    • 2018-04-16
    • 1970-01-01
    • 2011-04-11
    • 2016-01-23
    相关资源
    最近更新 更多