简体   繁体   中英

Removing special characters from gridview data

I have a gridview object that I am using to bind to a datasource . Upon the selectedIndexChanging event of the gridview , I would like to bring the data shown in the gridview into the textboxes on the form. However, when the data contains alphanumeric characters such as &'"", the data from the grid is showing ;amp, #S etc. and all other weird characters whenever I enter an alphanumeric character. Is there a way to prevent these characters from popping up in the textboxes when taking data from the grid? The code that I have so far:

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

I used the Server.HTMLDecode() when transferring the data fromthe gridview to the textboxes. This ensured that all special characters were removed before it was sent back to the textboxes on the form

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.

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