在从XML获取信息时,在LinkBut​​ton单击事件上填充表行和单元格表

时间:2015-03-26 22:42:29

标签: asp.net xml tablerow

基本上我创建Rows然后是Cell然后我用LinkBut​​ton填充Cells,它从XML节点获取Text值。

代码:

 private void linkBTN_Click(object sender, EventArgs e)
        {

                LinkButton btn = (LinkButton)sender;
                string text = btn.Text.ToString();
                XmlDocument clickDoc = new XmlDocument();
                clickDoc.Load(Server.MapPath("~/ProductShow.xml"));
                XmlNodeList btnNode = clickDoc.SelectNodes("products/" + text.ToString() + "/*");
                int count = btnNode.Count;


                foreach (XmlNode node in btnNode)
                {


                   TableRow row = new TableRow();

                    TableCell cell = new TableCell();
                    cell.CssClass = "Cell";

                    LinkButton linkbtn = new LinkButton();
                    linkbtn.Text = node.InnerText;
                    linkbtn.Attributes.Add("runat", "server");
                    Image img = new Image();

                    cell.Controls.Add(linkbtn);
                    cell.Controls.Add(new LiteralControl("<br/>"));
                    cell.Controls.Add(img);
                    row.Cells.Add(cell);

                    MainTable.Rows.Add(row);


                }







    }

我的问题是我想为每一行创建4个单元格,我的代码为每一行创建1个单元格,直到节点结束

1 个答案:

答案 0 :(得分:0)

您需要另一个循环来为每行添加更多列

 foreach (XmlNode node in btnNode)
    {
       TableRow row = new TableRow();

       for (int i = 0; i < 4; i++){
                TableCell cell = new TableCell();
                cell.CssClass = "Cell";

                LinkButton linkbtn = new LinkButton();
                linkbtn.Text = node.InnerText;
                linkbtn.Attributes.Add("runat", "server");
                Image img = new Image();

                cell.Controls.Add(linkbtn);
                cell.Controls.Add(new LiteralControl("<br/>"));
                cell.Controls.Add(img);

                row.Cells.Add(cell);
        }
                MainTable.Rows.Add(row);                  
   }