[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Save Form Field Values to TXT File

Posted on 2011-02-20
5
Medium Priority
?
569 Views
Last Modified: 2012-05-11
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

0
Comment
Question by:jay_eire
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 16

Expert Comment

by:BurnieP
ID: 34937467
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

0
 
LVL 7

Author Comment

by:jay_eire
ID: 34937528
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

0
 
LVL 7

Author Comment

by:jay_eire
ID: 34937587
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

0
 
LVL 16

Accepted Solution

by:
BurnieP earned 2000 total points
ID: 34937603
Hi,

Of course you can add a label.

It can be something like this :


<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" />
<asp:Label id="lblSuccess" runat="server" />

    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();
        lblSuccess.Text = "The file has been saved successfully.";
    }

Open in new window

0
 
LVL 7

Author Closing Comment

by:jay_eire
ID: 34937623
This Is awesome Thank you!
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Originally, this post was published on Monitis Blog, you can check it here . It goes without saying that technology has transformed society and the very nature of how we live, work, and communicate in ways that would’ve been incomprehensible 5 ye…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
The is a quite short video tutorial. In this video, I'm going to show you how to create self-host WordPress blog with free hosting service.
Suggested Courses

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question