user2030579
user2030579

Reputation: 41

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

Upvotes: 0

Views: 1819

Answers (1)

user2030579
user2030579

Reputation: 41

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

Upvotes: 0

Related Questions