dynamically generate some controls in the table’s td tags

October 9, 2007

Hi,Recently i had the follwing problem and i provided the solution for that one using the below code..My requirement is i have a table. and in that i have two rows(trs)
in first row i have  two tds .In first td i have a textbox and in second td i have a button named “save”
In second row (tr) i have only one td ……..this td consists of a button named “add new row”
if i click on the “add new row ” button another row has to be  generated dynamically with textbox and button like first row.

 Solution
 

<!–File:Dynamically_Generate_Row.aspx–><%@ Page Language=”C#” AutoEventWireup=”true”
CodeFile=”Dynamically_Generate_Row.aspx.cs”
Inherits=”Dynamically_Generate_Row” %>
<!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>
    <table>
    <% for (int i=0; i<_nRowCount; i++) {%>
     <tr>
     <td>
     <input type=”text” value=”” id=”text<%=i %>>”/>
     </td>
     <td>
     <input type=”button” value=”Save” id=”btn<%=i %>>” />
     </td>
     <% } %>
     </tr>
     <tr>
     <td>
     <asp:Button ID=”btn_CreateRow” runat=”server” OnClick=”OnCreateRow”  Text=”AddNew”/>
     </td>
     </tr>
    </table>
   
    </div>
    </form>
</body>
</html>
 
//File:Dynamically_Generate_Row.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
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 Dynamically_Generate_Row : System.Web.UI.Page
{
    public int _nRowCount=1;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (null == Session[“row_count”])
            Session[“row_count”] = “1”;
      
        _nRowCount = System.Convert.ToInt32(Session[“row_count”]);
    }
    protected void OnCreateRow(object sender, EventArgs e)
    {
        if (null == Session[“row_count”])
            Session[“row_count”] = “1”;
        else
        {
            int i = (System.Convert.ToInt32(Session[“row_count”]) + 1);
            Session[“row_count”] = i.ToString();
        }
        _nRowCount = System.Convert.ToInt32(Session[“row_count”]);
    }
}
  


how to read a data from txt file using microsoft dotnet

October 3, 2007

StreamReader: streamreader is nothing but file pointer.And here i used the stream reader to read the data from the text file(.txt) and displaying that it is in a label.

System.IO.this is the name space required to deal with the files. T

o read a text file we need to create the instance of the object,StreamReader. The instance will be the file pointer for us. Once we have a File Pointer, we need to invoke the method, OpenText method of the object, File.

The method, OpenText takes a string as an argument. The string is nothing but the path of file that is going to get created. Now, let us see an example.

                                                             
String filename=Server.MapPath("new.txt");//here in the string we are storing the filepath 
StreamReader strrr;
strrr=File.OpenText(filename);//here streamreader pointing the files..means stream reader is nothing but a file pointer 
lbltxt.Text=str;//here we are displaying the txt file data in the label. 
strrr.Close();.. fstring str=strrr.ReadToEnd();