i am a starter at asp and while trying to do some table dynamic updates i got stuck with a strange issue:
first on page load i take some data from xml file and add it to my table. this part did not cause any problem and the data is shown on the page.
the second part is when i try to write the table’s updated data back to the file, there i get back an empty file. after looking at the values in the debug mode it seems that when i get the HtmlTable instance and check the Row attribute there is only one row which is the header row.
attaching the table declaration:
<table id="users" class="ui-widget ui-widget-content" runat="server">
<thead>
<tr class="ui-widget-header ">
<th>Title</th>
<th>Link From Ibms</th>
<th>Dismiss</th>
</tr>
</thead>
</table>
and the reading and writing function on the server:
private void readLinkXml()
{
if (!File.Exists(path)) // no file.
return;
XmlTextReader reader = new XmlTextReader(path);
reader.WhitespaceHandling = WhitespaceHandling.None;
// reader.Read();
while (!reader.EOF) // load loop
{
if ((reader.Name == "ChannelMap" && !reader.IsStartElement()))
{
break;
}
while ((reader.Name != "links" || !reader.IsStartElement()))
reader.Read(); // advance to next <links> tag
HtmlTableRow row = new HtmlTableRow();
HtmlTableCell title = new HtmlTableCell();
title.InnerHtml = reader.GetAttribute("TITLE"); // get link title.
row.Cells.Add(title);
HtmlTableCell link = new HtmlTableCell();
link.InnerHtml = reader.GetAttribute("VOD_URL"); // get link url.
row.Cells.Add(link);
HtmlTableCell buttonCell = new HtmlTableCell();
buttonCell.Attributes.Add("id", "table_button");
row.Cells.Add(buttonCell);
users.Rows.Add(row); // add xml asset as row in link table.
reader.Read(); // and now either at <links> tag or </ChannelMap
}
reader.Close();
}
private void writeLinksXml()
{
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = " ";
settings.NewLineChars = "\r\n";
settings.NewLineHandling = NewLineHandling.Replace;
XmlWriter writer = XmlWriter.Create(path, settings);
writer.WriteStartDocument();
writer.WriteStartElement("ChannelMap");
bool first = true;
foreach (HtmlTableRow row in users.Rows)
{
if (first)
{
first = false;
continue;
}
writer.WriteStartElement("links");
writer.WriteAttributeString("TITLE", row.Cells[0].InnerText);
writer.WriteAttributeString("VOD_URL", row.Cells[1].InnerText);
writer.WriteEndElement();
}
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
does anyone knows why i am not able to retrieve the data i just added to the table?
thanks,
moshe
Well, It seems with the issue as all called Why view-state is required in aspnet.
From your question I guess that you have bind Data in
Page_Load()and you may also have usedwhich is good thing to do.
Now when you do post back you need to make sure that you are binding the same control ( It doesn’t matter what it is GridView,DataList anything…).
So Bind Again after each Postback or put your data in ViewState if you want to reduce calls with the XML File.