Problems with special characters in Asp.net GRIDVIEW

Asked

Viewed 912 times

3

I am building a dynamic GRIDVIEW, where the information of each TD will be a DIV.

exemplo: <td><div id="d1" class="redips-drag t1">z</div></td>

I am filling the valve as follows:

if (dtWorkServiceTimeByWorkerAndDate.Rows.Count > 0)
    finalResult = "<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\">" + formatSecondsInHourAndMinute(Convert.ToInt32(dtWorkServiceTimeByWorkerAndDate.Rows[0]["work_ServiceTime"])) + "</div>";

My problem is that GRID does not recognize div as an object, but rather as a String. The value of each column is coming out as follows:

exemplo &lt;div id=&quot;column1&quot; class=&quot;redips-drag t1&quot;&gt;23:59&lt;/div&gt;

Does anyone know how to avoid this problem?

Vlw.

EDIT

To construct the grid, I am using a function that returns a Datatable, as follows:

public DataTable constructWorkerAvailabilityTime()
    {
        DateTime fDateDataType = new DateTime(Int32.Parse(fYear), Int32.Parse(fMonth), Int32.Parse(fDay));
        DateTime tDateDataType = new DateTime(Int32.Parse(tYear), Int32.Parse(tMonth), Int32.Parse(tDay));

        this.fromDate = fDateDataType.ToString("yyyy-MM-dd");
        this.toDate = tDateDataType.ToString("yyyy-MM-dd");

        DataTable dtFinalResult = new DataTable();
        List<String> allWorkers = getAllWorkers();
        List<String> columnsSequence = getDateColumnsSequence();

        dtFinalResult.Columns.Add("worker", typeof(string));

        int count = 0;

        foreach (string column in columnsSequence)
            dtFinalResult.Columns.Add(column, typeof(string));

        foreach (string worker in allWorkers)
        {
            DataRow newRow = dtFinalResult.NewRow();

            newRow["worker"] = getWorkerName(worker);

            foreach (string column in columnsSequence)
            {
                HtmlString htmlString = getWorkServiceTimeByWorkerAndDate(worker, column, count);
                newRow[column] = htmlString;
                //newRow[column] = "@Html.Raw(" + getWorkServiceTimeByWorkerAndDate(worker, column, count) + ")";
                count++;
            }

            dtFinalResult.Rows.Add(newRow);
        }

        return dtFinalResult;
    }

private HtmlString getWorkServiceTimeByWorkerAndDate(string worker, string executionDate, int count)
    {
        HtmlString finalResult = new HtmlString("");
        DataTable dtWorkServiceTimeByWorker = cmd.selectWorkServiceTimeByWorker(worker);
        DataTable dtWorkServiceTimeByWorkerAndDate = cmd.selectWorkServiceTimeByWorkerAndDate(worker, executionDate);

        if (dtWorkServiceTimeByWorker.Rows.Count == 1)
            finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">" + formatSecondsInHourAndMinute(Convert.ToInt32("28860")) + "</div>");
        else if (dtWorkServiceTimeByWorker.Rows.Count > 1)
        {
            if (dtWorkServiceTimeByWorkerAndDate.Rows.Count > 0)
                finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">" + formatSecondsInHourAndMinute(Convert.ToInt32(dtWorkServiceTimeByWorkerAndDate.Rows[0]["work_ServiceTime"])) + "</div>");
            else
                finalResult = new HtmlString("<div id=\"column" + count.ToString() + "\" class=\"redips-drag t1\" runat=\"server\">00:00</div>");
        }

        return finalResult;
    }

And finally fill the grid in the page Load:

protected void Page_Load(object sender, EventArgs e)
    {
        WorkerManagement wm = new WorkerManagement();
        GridWorkerAvailability.DataSource = wm.constructWorkerAvailabilityTime();
        GridWorkerAvailability.DataBind();
        GridWorkerAvailability.SelectedIndex = -1;
    }
  • Flavio, take a look at this reply and see if you can solve with one of these two solutions, I’m not being able to test now.

  • @Filipeoliveira BOAA! Solved my problem, vlww man.

2 answers

1


To solve the problem, I used:

    protected void GridWorkerAvailability_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        int cellCount = e.Row.Cells.Count;

        for (int j = 1; j < cellCount; j++)
        {
            string encoded = e.Row.Cells[j].Text;
            e.Row.Cells[j].Text = Context.Server.HtmlDecode(encoded);
        }
    }

0

You can use the Html.Raw().

Example:

@Html.Raw(finalResult)
  • Eai Felipe, first, vlw for help. But come on, I tried to use what you said, but I don’t know if the syntax is correct, because it didn’t work. Take a look: newRow[column] = "@Html.Raw(" + getWorkServiceTimeByWorkerAndDate(worker, column, count) + ")"; This function returns the value of the finalResult.

  • @Flavioluiz, this string finalResult do you send to view? This @Html.Raw has to be used in html, but will depend on the view engine you are using (in my example, it was with Razor). Could you illustrate how you are trying to display this data?

  • @Filipioliveira I am building a dynamic gridView, IE, I am building a Datatable, filling the columns with the respective values, and in the Datasource property of gridView, I am inserting the Datatable. I will edit the code and insert some functions that are part of the grid construction.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.