DataGrid using XML File

Last post 10-08-2008 4:34 AM by Samu Zhang - MSFT. 1 replies.

Sort Posts:

  • DataGrid using XML File

    10-06-2008, 9:47 AM
    • Member
      2 point Member
    • waynea
    • Member since 04-05-2006, 2:57 PM
    • Posts 16

    Hi chaps,

    I am binding my datagrid with an xml file, I want to be able to populate a literal control with an element taken from the xml file based on an id from the Grid - i.e

     The grid displays an ID and a Date, once the user clicks on the id, the literal control is populate with the newsletter element from the xml file

    xml file

    <NewsLetter ID ="1">

      <date></date>
     <heading></heading>
    <description></description>

    </NewsLetter>

     

    Any pointers would be most appreciated

  • Re: DataGrid using XML File

    10-08-2008, 4:34 AM
    Answer

    Hi waynea ,

    See my sample

     

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                DataTable table = new DataTable();
                table.Columns.Add("id");
                DataRow dr = table.NewRow();
                dr["id"] = "1";
                table.Rows.Add(dr);
                dg.DataSource = table;
                dg.DataBind();
            }
    
        }
    
    
        protected void dg_ItemCommand(object source, DataGridCommandEventArgs e)
        {
            Literal ph = e.Item.FindControl("Literal1") as Literal;
            XmlDocument doc = new XmlDocument();
            doc.Load(Server.MapPath("XMLFile3.xml"));
            XmlNode node = doc.SelectSingleNode("/NewsLetter[@ID='"+e.CommandArgument.ToString ()+"']");
            XmlNode date = node.SelectSingleNode("date");
            ph.Text = date.InnerText;
        }
      
        <asp:DataGrid runat="server" ID="dg" AutoGenerateColumns="False" Width="206px" OnItemCommand="dg_ItemCommand" >
            <Columns>
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:LinkButton CommandArgument='<%# Eval("id") %>' ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
                    </ItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn>
                    <ItemTemplate>
                        <asp:Literal ID="Literal1" runat="server"></asp:Literal>
                    </ItemTemplate>
                </asp:TemplateColumn>
            </Columns>
        </asp:DataGrid>
     

    Samu Zhang
    Microsoft Online Community Support

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
Page 1 of 1 (2 items)
Microsoft Communities