on 1 comment

Using StreamWriter To Write HTML File

Here, I will explain how to add textbox text into HTML file using StreamWriter.
First,we need to write the following code in aspx page.
Default.aspx
<div>
        <table border="1">
            <tr>
                <td>
                    <asp:Label ID="lbnname" runat="server" Text="Name"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtname" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Label ID="lbnemail" runat="server" Text="Email"></asp:Label>
                </td>
                <td>
                    <asp:TextBox ID="txtemail" runat="server"></asp:TextBox>
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Button ID="btnwrite" runat="server" Text="Write" 
                        onclick="btnwrite_Click" />
                </td>
                <td>                    
                    <asp:Button ID="btnreset" runat="server" Text="Reset" 
                        onclick="btnreset_Click" />
                </td>
            </tr>         
        </table>
    </div>
Now, first we add namespace System.IO  in Default.aspx.cs  page  and also add html file  from addNewItem  and give "name nam_email.htm".
 Default.aspx.cs
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void btnwrite_Click(object sender, EventArgs e)
    {       
        int id = 0,;   
        StreamWriter sw = new StreamWriter(Request.PhysicalApplicationPath + "nam_email.htm", true);      
       sw.WriteLine("<tr><td>" + id +"</td><td>" + txtname.Text + "</td><td>" + txtemail.Text + "</td></tr>");        
        sw.Close();
    }  
    protected void btnreset_Click(object sender, EventArgs e)
    {
        txtname.Text = string.Empty;
        txtemail.Text = string.Empty;
    } 
}

1 comment:

  1. thank u dude, i used your tips for solving my problem, its works. Very helpful for me. ASPHostPortal.com

    ReplyDelete

aspdotnet-learn. Powered by Blogger.