Link to home
Start Free TrialLog in
Avatar of jay_eire
jay_eireFlag for United States of America

asked on

Save Form Field Values to TXT File

Hi,
I have a ASP.NET C# web-page that has two form fields called:
WebAddress
WebAddressContents

When user clicks the SAVE button I want the contents of the form to be saved to a TXT file, with the WebAddress as the TXT file name and also as the first line in the body of the TXT file along with the contents of  WebAddressContents in the body of the TXT file.

How do I go about doing this?

Thanks
Jay

 
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:TextBox ID="WebAddress" runat="server" style="width:300px; Height:20px;" value="http://www.google.com"></asp:TextBox><asp:Button ID="SaveContents"
            runat="server" Text="Save" /><br /><br />
        <asp:TextBox ID="WebAddressContents" runat="server" style="width:300px; Height:200px;" value="7+ GB of storage, less spam, and mobile access. Gmail is email that's intuitive, efficient, and useful. And maybe even fun."></asp:TextBox>
    </div>
    </form>
</body>
</html>

Open in new window

Avatar of BurnieP
BurnieP
Flag of Canada image

Hi,

Here is a quick example:

The reason i'm replacing "http://" with nothing is because it will not save the file since it is an invalid format.
<div>
        <asp:TextBox ID="WebAddress" runat="server" style="width:300px; Height:20px;" value="http://www.google.com"></asp:TextBox><asp:Button ID="SaveContents"
            runat="server" Text="Save" /><br /><br />
        <asp:TextBox ID="WebAddressContents" runat="server" style="width:300px; Height:200px;" value="7+ GB of storage, less spam, and mobile access. Gmail is email that's intuitive, efficient, and useful. And maybe even fun."></asp:TextBox>
    </div>
    <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />

    protected void btnSave_Click(object sender, EventArgs e)
    {
      StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://","") + ".txt");
      sw.WriteLine(WebAddress.Text);
      sw.WriteLine(WebAddressContents.Text);
      sw.Close();
    }

Open in new window

Avatar of jay_eire

ASKER

Thanks for the reply, Im getting back an error

Error      3      'ASP.default_aspx' does not contain a definition for 'btnSave_Click'      C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\WebSites\WebSite5\Default.aspx      16      

Does this go into the code behind page?
 
protected void btnSave_Click(object sender, EventArgs e)
    {
      StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://","") + ".txt");
      sw.WriteLine(WebAddress.Text);
      sw.WriteLine(WebAddressContents.Text);
      sw.Close();
    }

Open in new window


where should I place it? my code behind looks like this.
 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
}

Open in new window

Thanks for your help BurnieP: I added the button code into the code behind page and also added the name space using System.IO;

Is there a way I can include a label on the page to say the file has been saved successfully?
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
<div>
        <asp:TextBox ID="WebAddress" runat="server" style="width:300px; Height:20px;" value="http://www.google.com"></asp:TextBox><br /><br />
        <asp:TextBox ID="WebAddressContents" runat="server" style="width:300px; Height:200px;" value="7+ GB of storage, less spam, and mobile access. Gmail is email that's intuitive, efficient, and useful. And maybe even fun."></asp:TextBox>
    </div>
    <asp:Button ID="btnSave" runat="server" OnClick="btnSave_Click" Text="Save" />


    </form>
</body>
</html>


using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;

public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        StreamWriter sw = new StreamWriter("c:\\temp\\" + WebAddress.Text.Replace("http://", "") + ".txt");
        sw.WriteLine(WebAddress.Text);
        sw.WriteLine(WebAddressContents.Text);
        sw.Close();
    }
}

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of BurnieP
BurnieP
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This Is awesome Thank you!