簡體   English   中英

XML節點編輯。

[英]XML nodes editing.

我將queryString中的值分配給這些文本框,並且工作正常,但每當我編輯其中一個文本並嘗試將編輯后的數據保存在XML節點中時,我就不能

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 
    { 
    } 
} 

這段代碼有什么用嗎? 它將未經編輯的版本保存在節點中!

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; 
}

每次加載都會觸發Page_Load(在回發時和初始加載時)。 在事件處理程序嘗試保存之前,您的代碼當前在每次加載時都會從Request.QueryString默認這些值。

改為:

        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 
            { 
            } 
        } 

如果您要提交已編輯的文本框,則需要使用IsPostback檢查包含在Pageload的代碼,以確保不會將值重置為原始值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM