Displaying XML data as is with tags on the datagrid

Discussion in 'ASP .Net Datagrid Control' started by Nedu N, Nov 14, 2003.

  1. Nedu N

    Nedu N Guest

    Hi,

    I want to display the XML tags (for the the XML tags is the data that is
    stored in a table) on the datagrid using the datasets. But since the data
    itself is the XML tag it doesn't appera on the datagrid since it tries to
    parse the data before it displays.

    Using Server.HtmlEncode, i was able to display the right content on the
    datagrid for each and every colum using Template column. But i don't want to
    use any template columns since i have got all the columns are having the
    data as XML tags as is and i have many cloumns.

    So, is there anyway i can directly achieve this while binding the datagrid
    with the dataset without using Template columns for each of the coulmns?
    Using Server.HtmlEncode, I was able to handle each column like
    <%#
    Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem,"TableColumn"))
    %>
     
    Nedu N, Nov 14, 2003
    #1
    1. Advertising

  2. Nedu N

    MSFT Guest

    Hi Nedu,

    I think HTNLEncode and ItemTemplate should be a nice solution for this
    problem. Anyway, if you don't prefer this, you may consider encode the
    string by yourself in the dataset. For example:

    Dim dr As DataRow

    Dim i As Integer

    For Each dr In MyDataSet.TableXML.Rows

    dr("XMLField") = Replace(dr("XMLField"), "<", "&lt")
    dr("XMLField") = Replace(dr("XMLField"), ">", "&gt")

    Next

    This will make "<" and ">" display in IE.

    Luke
    Microsoft Online Support

    Get Secure! www.microsoft.com/security
    (This posting is provided "AS IS", with no warranties, and confers no
    rights.)
     
    MSFT, Nov 17, 2003
    #2
    1. Advertising

  3. Try performing the encoding in the ItemDataBound Event as follows:

    private void DataGrid1_ItemDataBound(object sender,
    System.Web.UI.WebControls.DataGridItemEventArgs e)

    {

    switch(e.Item.ItemType)

    {

    case ListItemType.Item:

    // THIS ENCODES ANY TEXT GOING INTO CELLS SO THAT THERE IS NO CHANCE OF
    SCRIPT INJECTION

    foreach (Control c in e.Item.Controls)

    {

    TableCell cell = (TableCell) c;

    string text = cell.Text;

    if (text != " " && text != "")

    {

    if (text == "&nbsp;") text = "";

    cell.Text = HttpUtility.HtmlEncode(text);

    }

    }

    break;

    }

    }



    "Nedu N" <> wrote in message
    news:...
    > Hi,
    >
    > I want to display the XML tags (for the the XML tags is the data that is
    > stored in a table) on the datagrid using the datasets. But since the data
    > itself is the XML tag it doesn't appera on the datagrid since it tries to
    > parse the data before it displays.
    >
    > Using Server.HtmlEncode, i was able to display the right content on the
    > datagrid for each and every colum using Template column. But i don't want

    to
    > use any template columns since i have got all the columns are having the
    > data as XML tags as is and i have many cloumns.
    >
    > So, is there anyway i can directly achieve this while binding the datagrid
    > with the dataset without using Template columns for each of the coulmns?
    > Using Server.HtmlEncode, I was able to handle each column like
    > <%#
    >

    Server.HtmlEncode((string)DataBinder.Eval(Container.DataItem,"TableColumn"))
    > %>
    >
    >
    >
     
    Mark Schubert, Nov 19, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nedu N
    Replies:
    1
    Views:
    425
    Henrik Grotle
    Sep 18, 2003
  2. Nedu N
    Replies:
    2
    Views:
    417
    Mark Schubert
    Nov 19, 2003
  3. Nedu N

    displaying XML data in datagrid column

    Nedu N, Sep 17, 2003, in forum: ASP .Net Building Controls
    Replies:
    2
    Views:
    141
    Henrik Grotle
    Sep 18, 2003
  4. Nedu N

    displaying XML data in datagrid column

    Nedu N, Sep 17, 2003, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    116
  5. Nedu N

    Displaying XML data as is with tags on the datagrid

    Nedu N, Nov 14, 2003, in forum: ASP .Net Web Controls
    Replies:
    2
    Views:
    113
    Mark Schubert
    Nov 19, 2003
Loading...

Share This Page