How to read a html file in ASP.NET
Use namespace System.IO VB.NET Dim file As String = Server.MapPath('temp.html') Dim sr As StreamReader Dim fi As New FileInfo(file) Dim input As String = '<pre>' If File.Exists(file) Then sr = File.OpenText(file) input += Server.HtmlEncode(sr.ReadToEnd()) sr.Close() End If input += '</pre>' Me.Label1.Text = input C# string file = Server.MapPath ('temp.html'); StreamReader sr; FileInfo fi = new FileInfo(file); string input = '<pre>'; if(File.Exists(file)) { sr = File.OpenText(file); input += Server.HtmlEncode(sr.ReadToEnd()); sr.Close(); } input += '</pre>'; this.Label1.Text = input;
How to display the Attribute values in an XML Document in a DataGrid
products.xml <?xml version='1.0' standalone='yes'?> <NewDataSet> <Product> <ProductID pcode='P2'>2</ProductID> <ProductName>Chang</ProductName> <SupplierID>1</SupplierID> <CategoryID>1</CategoryID> <QuantityPerUnit>24 - 12 oz bottles</QuantityPerUnit> </Product> <Product> <ProductID pcode='P4'>3</ProductID> <ProductName>Aniseed Syrup</ProductName> <SupplierID>1</SupplierID> <CategoryID>2</CategoryID> <QuantityPerUnit>12 - 550 ml bottles</QuantityPerUnit> </Product> </NewDataSet> Use Namespace System.Xml VB.NET <asp:DataGrid id='DataGrid1' AutoGenerateColumns='False' runat='server'> <Columns> <asp:TemplateColumn HeaderText='ProductCode'> <ItemTemplate> <%#CType(Container.DataItem, System.Xml.XmlNode).Attributes('pcode').value%> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> dim xmldoc as XmlDocument = new XmlDocument() xmldoc.Load(Server.MapPath('products.xml')) dim xmlnodes as XmlNodeList = xmldoc.SelectNodes('NewDataSet/Product/ProductID') DataGrid1.DataSource = xmlnodes DataGrid1.DataBind () C# <asp:DataGrid id='DataGrid1' AutoGenerateColumns='False' runat='server'> <Columns> <asp:TemplateColumn HeaderText ='ProductCode'> <ItemTemplate> <%# ((System.Xml.XmlNode)Container.DataItem).Attributes['pcode'].Value %> </ItemTemplate> </asp:TemplateColumn> </Columns> </asp:DataGrid> XmlDocument xmldoc = new XmlDocument(); xmldoc.Load(Server.MapPath('products.xml')); XmlNodeList xmlnodes = xmldoc.SelectNodes('NewDataSet/Product/ProductID'); DataGrid1.DataSource = xmlnodes; DataGrid1.DataBind ();
How to convert the datetime into a string for use in the SQL ‘ statement
<asp:label id='Label2' runat='server'>Select a culture: </asp:label> <asp:dropdownlist id='ddlCulture' runat='server' autopostback='True'></asp:dropdownlist> <P></P> <asp:label id='Label3' runat='server'>DateTime in Selected Culture</asp:label> <asp:textbox id='TextBox1' runat='server'></asp:textbox> <p> <asp:label id='Label1' runat='server'></asp:label> VB.NET Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ’Put user code to initialize the page here If Not Page.IsPostBack Then Dim cInfo As CultureInfo For Each cInfo In CultureInfo.GetCultures(CultureTypes.SpecificCultures) ddlCulture.Items.Add(cInfo.Name) Next End If End Sub Private Sub ddlCulture_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ddlCulture.SelectedIndexChanged ’ Get a CultureInfo object based on culture selection in dropdownlist Dim cInfo As CultureInfo = New CultureInfo(ddlCulture.SelectedItem.Text) ’ Get a CultureInfo object based on Invariant culture Dim cInfoNeutral As CultureInfo = New CultureInfo('') ’ Display the datetime based on the formatting of the selected culture TextBox1.Text = Convert.ToString(Now, cInfo.DateTimeFormat) ’ Create a DateTime variable to hold the Invariant time Dim dt As DateTime dt = Convert.ToDateTime(TextBox1.Text, cInfo.DateTimeFormat) ’Convert the datetime into a string for use in the SQL statement Label1.Text = '... WHERE ([Date] < ’' & _ Convert.ToString(dt, cInfoNeutral.DateTimeFormat) & '’)' End Sub C# private void Page_Load(object sender, System.EventArgs e) { // Put user code to initialize the page here if (!Page.IsPostBack ) { foreach(CultureInfo cInfo in CultureInfo.GetCultures(CultureTypes.SpecificCultures)) { ddlCulture.Items.Add(cInfo.Name); } } } private void ddlCulture_SelectedIndexChanged(object sender, System.EventArgs e) { // Get a CultureInfo object based on culture selection in dropdownlist CultureInfo cInfo = new CultureInfo(ddlCulture.SelectedItem.Text); // Get a CultureInfo object based on Invariant culture CultureInfo cInfoNeutral = new CultureInfo(''); // Display the datetime based on the formatting of the selected culture TextBox1.Text = Convert.ToString(DateTime.Now , cInfo.DateTimeFormat); // Create a DateTime variable to hold the Invariant time DateTime dt ; dt = Convert.ToDateTime(TextBox1.Text, cInfo.DateTimeFormat); //Convert the datetime into a string for use in the SQL statement Label1.Text = '... WHERE ([Date] < ’' + Convert.ToString(dt, cInfoNeutral.DateTimeFormat) + '’)'; }
How To work with TimeSpan Class
VB.NET Dim adate As DateTime = DateTime.Parse('06/24/2003') Dim bdate As DateTime = DateTime.Parse('06/28/2003') Dim ts As New TimeSpan(bdate.Ticks - adate.Ticks) Response.Write(ts.TotalDays & '<br>') Response.Write(ts.TotalHours & ':' & ts.TotalMinutes & ':' & ts.TotalSeconds & ':' & ts.TotalMilliseconds) C# DateTime adate = DateTime.Parse('06/24/2003'); DateTime bdate = DateTime.Parse('06/28/2003'); TimeSpan ts = new TimeSpan (bdate.Ticks - adate.Ticks); Response.Write(ts.TotalDays.ToString () + '<br>'); Response.Write(ts.TotalHours.ToString() + ':' + ts.TotalMinutes.ToString() + ':' + ts.TotalSeconds.ToString() + ':' + ts.TotalMilliseconds.ToString() );
Can I specify ItemStyle to a Repeater Control
The Repeater class is not derived from the WebControl class, like the DataGrid and DataList. Therefore, the Repeater lacks the stylistic properties common to both the DataGrid and DataList. What this boils down to is that if you want to format the data displayed in the Repeater, you must do so in the HTML markup. i.e using <b>/<u>.. tags