简体   繁体   中英

Saving textbox text into XML file

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

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

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.

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.

Any ideas?

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

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

This should solve your issue:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM