保存文本框的文本转换成XML文件转换成、文本框、文本、文件

2023-09-05 03:23:11 作者:是一种习惯

我有1个按钮和4个文本框的ASP.NET Web窗体。

I have an ASP.NET WebForm with 1 button and 4 textboxes.

每次加载页面,如下code读取XML文件,并显示数据,在文本框中执行:

Every time the page loads, the following code to read data from an XML file and display in the textboxes is executed:

private void PutWhatWasBefore()
{
    var xml = XDocument.Load(@"C:\Settings.xml");

    From_display.Text = xml.Element("Settings").Element("Remember").Attribute("fromdisplay").Value.ToString();
    From_Smtp.Text = xml.Element("Settings").Element("Remember").Attribute("fromsmtp").Value.ToString();
    subject.Text = xml.Element("Settings").Element("Remember").Attribute("subject").Value.ToString();     
}

这code效果很好,它把一切都在文本框中。但,这是一个很大的,但是,当我点击该按钮,出现如下code编写XML文件不起作用:

This code works well, it puts everything in the textboxes. BUT, and this is a big but, when i click the button, the following code to write to the XML file does not work:

string tem = Template1.Text;
string from = From_Smtp.Text;
string dis = From_display.Text;
string sub = subject.Text;
var x = new XDocument(
    new XElement("Settings",
        new XElement("Remember",
            new XAttribute("fromsmtp", from),
            new XAttribute("subject", sub),
            new XAttribute("fromdisplay", dis),
            new XAttribute("template", tem)
        )
    )
);
x.Save(@"C:\Settings.xml");   

无论我如何更改文本框中的数据,我每次点击的数据恢复到它以前的按钮。

No matter how I change the data in the text boxes, every time I click on the button the data reverts back to what it was before.

我想它的回发,这就是为什么发生这种情况,但即使我禁用后回来的OnClientClick =返回false; 它仍然无法正常工作。

I was thinking its a post back and that's why this is happening, but even if i disable the post back with OnClientClick = return false; it still does not work.

任何想法?

修改(12:06):

我不认为我已经说过那里的问题是,我想多进点。

I don't think I have said where the problem was and I want to be more into the point.

当我点击下面的函数首先被执行的按钮:

When I click the button the following function is executed first:

private void SaveNames()
{
    try
    {
        string tem = Template1.Text;
        string from = From_Smtp.Text;
        string dis = From_display.Text;
        string sub = subject.Text;
        var x = new XDocument(
            new XElement("Settings",
                new XElement("Remember",
                    new XAttribute("fromsmtp", "He2"),
                    new XAttribute("subject", sub),
                    new XAttribute("fromdisplay", dis),
                    new XAttribute("template", tem)
                )
            )
        );
        x.Save(@"C:\Program Files (x86)\ActivePath\MailSenderWeb\Settings.xml");
    }
    catch (Exception ex)
    {
        AnswerAndError.Text = ex.Message;
    }
}

这是不工作的功能。它只是不保存新的数据到XML文件中。

That's the functions that doesn't work. It just doesn't save new data into the XML file.

推荐答案

这应该解决您的问题:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {
        PutWhatWasBefore();
    }
}

这将确保code运行,只有当页面最初访问。

This will ensure the code runs only when the page is initially visited.