Editing XML nodes.

I assign values ​​from the query string to these text fields, and it works fine, but whenever I edit the text in one of them and try to save the edited data in an XML node, I cannot

protected void Page_Load(object sender, EventArgs e) { if (Request.QueryString != null) { TextBox_firstname.Text = Request.QueryString["column1"]; TextBox_lastname.Text = Request.QueryString["column2"]; } else { } } 

Is there anything with this code? It saves the unedited version in nodes!

 public string str_id; public int id; id = int.Parse(str_id); XDocument xdoc = XDocument.Load(filepath); if (id == 1) { var StudentNodeWithID1 = xdoc.Descendants("students") .Elements("student") .Where(s => s.Element("id").Value == "1") .SingleOrDefault(); StudentNodeWithID1.Element("first_name").Value = TextBox_firstname.Text; StudentNodeWithID1.Element("last_name").Value = TextBox_lastname.Text; } 
+4
source share
2 answers

The_Load page is launched at every load (during postback, as well as during initial loading). Currently, your default code executes these values ​​from Request.QueryString every time it loads, before your event handler tries to save it.

Do this instead:

  protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack && Request.QueryString != null) { TextBox_firstname.Text = Request.QueryString["column1"]; TextBox_lastname.Text = Request.QueryString["column2"]; } else { } } 
+1
source

If you send edited text fields, you will need to wrap the code in Pageload with the IsPostback tag to ensure that the values ​​do not get reset to their originals.

0
source

All Articles