加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – XML节点编辑.

发布时间:2020-12-15 21:43:32 所属栏目:百科 来源:网络整理
导读:我将queryString中的值分配给这些文本框,并且工作正常,但每当我编辑其中一个文本并尝试将编辑后的数据保存在 XML节点中时,我就不能 protected void Page_Load(object sender,EventArgs e) { if (Request.QueryString != null) { TextBox_firstname.Text = Re
我将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 
            { 
            } 
        }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读