if (!Request.IsSecureConnection) { // send user to SSL string serverName =HttpUtility.UrlEncode(Request.ServerVariables["SERVER_NAME"]); string filePath = Request.FilePath; Response.Redirect("https://" + serverName + filePath); }
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="GridViewIndideRepeater.aspx.cs" Inherits="GridViewIndideRepeater" %> <!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:Repeater ID="ItemsList" OnItemDataBound="Item_Bound" runat="server"> <HeaderTemplate> <table> <caption> List of Categories </caption> </HeaderTemplate> <ItemTemplate> <tr> <td> <%# DataBinder.Eval(Container.DataItem, "Category") %> </td> </tr> <tr> <td> <asp:DataGrid ID="dataGrid1" runat="server" BorderColor="black" GridLines="Both" HeaderStyle-BackColor="#aaaadd" PagerStyle-Mode="NextPrev" PagerStyle-HorizontalAlign="Center" PagerStyle-CssClass="GridPager" PagerStyle-Position="Bottom" PagerStyle-PageButtonCount="25" PagerStyle-NextPageText="next" PagerStyle-PrevPageText="Prev" PagerStyle-BackColor="gainsboro" OnPageIndexChanged="ChangeGridPage" PageSize="5" AllowPaging="True" AutoGenerateColumns="false"> <Columns> <asp:BoundColumn Visible="False" ReadOnly="true" DataField="ID" /> <asp:BoundColumn HeaderText="Qty" ReadOnly="true" DataField="Qty" /> <asp:BoundColumn HeaderText="Description" ReadOnly="true" DataField="Description" /> <asp:BoundColumn HeaderText="Price" DataField="Price" DataFormatString="{0:c}"> <ItemStyle HorizontalAlign="right"></ItemStyle> </asp:BoundColumn> </Columns> </asp:DataGrid> </td> </tr> </ItemTemplate> <FooterTemplate> </table> </FooterTemplate> </asp:Repeater> </div> </form> </body> </html>
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 GridViewIndideRepeater : System.Web.UI.Page { public ICollection CreateDataSource() { // Create sample data for the DataList control. DataTable dt = new DataTable(); DataRow dr; // Define the columns of the table. dt.Columns.Add(new DataColumn("ID", typeof(Int32))); dt.Columns.Add(new DataColumn("Category", typeof(string))); // Populate the table with sample values. int i; for (i = 1; i <= 5; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Category " + i.ToString(); dt.Rows.Add(dr); } DataView dv = new DataView(dt); return dv; } public DataTable CreateDGDataSource() { // Create sample data for the DataList control. DataTable dt = new DataTable(); DataRow dr; int i; int y; // Define the columns of the table. dt.Columns.Add(new DataColumn("ID", typeof(int))); dt.Columns.Add(new DataColumn("Qty", typeof(int))); dt.Columns.Add(new DataColumn("Price", typeof(int))); dt.Columns.Add(new DataColumn("Description", typeof(string))); //Make some rows and put some sample data in for (y = 1; y <= 5; y++) { for (i = 1; i <= 10; i++) { dr = dt.NewRow(); dr[0] = y; dr[1] = i * y; dr[2] = i * y; dr[3] = "Item " + y + "_" + i; //add the row to the datatable dt.Rows.Add(dr); } } return dt; } public ICollection CreateDGDataSource(int CategoryID) { DataView dv = new DataView(CreateDGDataSource(), "ID=" + CategoryID, null, DataViewRowState.CurrentRows); return dv; } public void Page_Load(object sender, EventArgs e) { // Load sample data only once, when the page is first loaded. if (!IsPostBack) { ItemsList.DataSource = CreateDataSource(); ItemsList.DataBind(); } } public void BindDG(DataGrid dg, int CategoryID) { dg.DataSource = CreateDGDataSource(CategoryID); dg.DataBind(); } public void ChangeGridPage(object sender, DataGridPageChangedEventArgs objArgs) { int CategoryID = int.Parse(((DataGrid)sender).Items[0].Cells[0].Text); ((DataGrid)sender).CurrentPageIndex = objArgs.NewPageIndex; BindDG((DataGrid)sender, CategoryID); } public void Item_Bound(object sender, RepeaterItemEventArgs e) { if (e.Item.ItemType == ListItemType.Item | e.Item.ItemType == ListItemType.AlternatingItem) { // Retrieve the Label control in the current DataListItem. DataGrid dg = (DataGrid)e.Item.FindControl("DataGrid1"); if (dg == null) { Response.Write("DataGrid not found"); } else { object CategoryID = ((DataRowView)((RepeaterItem)e.Item).DataItem)["ID"]; dg.DataSource = CreateDGDataSource((int)CategoryID); dg.DataBind(); } } } }
While for many applications, it is easier to display data-bound information using the Repeater, DataList, or DataGrid, it is possible to display database information using the Table Web server control. In the most common scenario for displaying data in a Table control, each row of the table represents a data row from the data source, and each cell of the table displays a single field from a row. Unlike the list controls ( Repeater, DataList, etc. ), the Table control does not have a mechanism for automatically iterating through the rows in a data source. Therefore, you must do this yourself. For general details about data binding in Web server controls, see Web Forms Data Binding. To display database information in a Table Web server control * Set the contents of a TableCell control to the data to be displayed. Most often you set the cell's Text property to an expression that extracts data from a data source. It is also possible to include controls in a TableCell control and bind the controls to a source of data. For details about using controls to display information, see Adding Rows and Cells Dynamically to a Table Control. The following example illustrates one way to display data in a Table control. The sample shows an event-handling method for a button. The method loops through the DefaultView object of a dataset, creating a TableRow control for each row in the dataset. The code then creates a TableCell control in each table row and sets its Text property to the "Id" field of the current data row.<%@ Page Language="C#" AutoEventWireup="true" CodeFile="DynamicTextBox.aspx.cs" Inherits="DynamicTextBox" %> <!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:Table ID="Table1" runat="server" CellPadding="2" CellSpacing="2"></asp:Table> <asp:Button ID="btnFill" runat="server" Text="Fill" OnClick="btnFill_Click" /> </div> </form> </body> </html>
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 DynamicTextBox : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public DataTable GetCustomMadeDataTable()
{
//Create a new DataTable object
System.Data.DataTable objDataTable = new System.Data.DataTable();
//Create three columns with string as their type
objDataTable.Columns.Add("Id", typeof(string));
objDataTable.Columns.Add("Column1", typeof(string));
objDataTable.Columns.Add("Column2", typeof(string));
objDataTable.Columns.Add("Column3", typeof(string));
//Adding some data in the rows of this DataTable
DataRow dr;
for (int i = 0; i <= 10; i++)
{
dr = objDataTable.NewRow();
dr[0] = "Id" + i.ToString();
dr[1] = "Item" + i.ToString();
dr[2] = "Product" + i.ToString();
dr[3] = "Description" + i.ToString();
objDataTable.Rows.Add(dr);
}
DataColumn[] dcPk = new DataColumn[1];
dcPk[0] = objDataTable.Columns["Id"];
objDataTable.PrimaryKey = dcPk;
Session["dtTemp"] = objDataTable;
return objDataTable;
}
protected void btnFill_Click(object sender, EventArgs e)
{
DataTable dt = GetCustomMadeDataTable() as DataTable;
DataView dv = dt.DefaultView;
// Create a row with one column for each row in the DataView
foreach (DataRowView dataRow in dv)
{
TableRow tRow = new TableRow();
TableCell tCell = new TableCell();
TableCell tCell2 = new TableCell();
TableCell tCell3 = new TableCell();
tCell.Text = dataRow.Row.ItemArray[0].ToString();
tCell2.Text = dataRow.Row.ItemArray[1].ToString();
tCell3.Text = dataRow.Row.ItemArray[2].ToString();
tRow.Cells.Add(tCell);
tRow.Cells.Add(tCell2);
tRow.Cells.Add(tCell3);
Table1.Rows.Add(tRow);
}
}
}
It’s common to add rows and cells to a Table Web server control at run time. Rows are objects of type TableRow. The Row property of the Table control supports a collection of TableRow objects. To add a row to the table, you add a TableRow object to this collection.
Similarly, the TableRow object has a Cells property that supports a collection of objects of type TableCell. You can add cells to a row by manipulating this collection.
<html>
<head>
<title>Generating Table Rows and Cells Dynamically</title>
<script language = "C#" runat = "server">
void Page_Load ( object src, EventArgs e ) {
if ( !IsPostBack ) {
// generate select options
for ( int i = 1; i <= 5; i++ ) {
rowsSelect.Items.Add ( i.ToString ( ) );
cellsSelect.Items.Add ( i.ToString ( ) );
}
rowsSelect.SelectedIndex = 1;
cellsSelect.SelectedIndex = 2;
}
// generate rows and cells
int numrows = int.Parse ( rowsSelect.SelectedItem.Value );
int numcells = int.Parse ( cellsSelect.SelectedItem.Value );
for ( int j = 0; j < numrows; j++ ) {
TableRow r = new TableRow ( );
for ( int i = 0; i < numcells; i++ ) {
TableCell c = new TableCell ( );
c.Controls.Add ( new LiteralControl ( "row " + j.ToString ( ) + ", cell " + i.ToString ( ) ) );
r.Cells.Add ( c );
}
myTable.Rows.Add ( r );
}
}
</script>
</head>
<body>
<div class="header"><h3>Generating Table Rows and Cells Dynamically</h3></div>
<hr size=1 width=90%>
<div align="center">
<form runat="server">
<p><asp:table id="myTable" cellpadding=5 cellspacing=0 gridlines="Both" runat="server" /></p>
<p>
Table rows <asp:dropdownlist id="rowsSelect" runat="server" />
Table cells <asp:dropdownlist id="cellsSelect" runat="server" />
<p><asp:button text="Generate Table" runat="server" />
</form>
</div>
<hr size=1 width=90%>
</body>
</html>
Just override the OnDataBound method and add the extra rows. In this example, the number of rows is always the same as the PageSize property and still keeping the footer row at the bottom.
protected override void OnDataBound(EventArgs e) { GridViewRow gvRow = null; for (int rows = this.Rows.Count; rows < this.PageSize; rows++) { gvRow = new GridViewRow(0, 0, DataControlRowType.DataRow, DataControlRowState.Normal); for (int columns = 0; columns < this.Columns.Count; columns++) { gvRow.Controls.Add(new TableCell()); } //Inserts the rows right above the footer row. //Remove the "- 1" if you are not using a footer. this.Controls[0].Controls.AddAt(this.Controls[0].Controls.Count - 1, gvRow); } }
You can use the Update method of the UpdatePanel on the server-side. Make sure to set
the UpdateMode of the UpdatePanel to Conditional to get it work.
<asp:UpdatePanel UpdateMode="Conditional" ID="UpdatePanel1"...>
UpdatePanel1.Update()
You can also use triggers that will make sure the UpdatePanel listen to a control’s event and will do an update if the event if fired and caused by a postback. Example:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:Button ID="Button1" runat="server" Text="Button" />
I have created a Web user control and have a button, when I press on that button I register a script with Page.ClientScript.RegisterStartupScript and it works. But when I put the control into my UpdatePanel the client-side script will not work, any idea?
You need to use the ScriptManager to register the script block, because the scripts registered with the Page object will not be sent back to the client after a partial update. You can for example use the ScriptManager.GetCurrent method to get the instance of the ScriptManager added to your page, and then use its RegisterClientScriptBlock method, for example:
VB.Net
Dim current As ScriptManager = ScriptManager.GetCurrent(Me.Page)
current.RegisterClientScriptBlock(Me.Page, Me.GetType(), “key”, “your script”, True)
C#
ScriptManager current = ScriptManager.GetCurrent(this.Page);
current.RegisterClientScriptBlock(this.Page, this.GetType(), “key2, “your script”, true);
This will help you to nest grdiviews inside ASP.NET. There may be many ways to do this, but this is my way( May not be the best, let me know if you have any other
1. First drop a DataList to the page, also set its DataKeyNames to your primary key
2. Add a new template column to this DataList
3. Place another DataList this template column.
<%@ Page Language="C#" %> <%@ Import Namespace="System.Data" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <script runat="server"> private DataSet CreateDS() { DataSet ds = new DataSet(); if (Session["dsEvents"] == null) { DataTable dt = new DataTable("PersonData"); DataRow dr; dt.Columns.Add(new DataColumn("Person_ID", typeof(Int32))); dt.Columns.Add(new DataColumn("PersonName", typeof(string))); dt.Columns.Add(new DataColumn("Company", typeof(string))); for (int i = 1; i < 10; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = "Person " + i; dr[2] = "Company " + i; dt.Rows.Add(dr); } DataColumn parentCol; parentCol = dt.Columns["Person_ID"]; ds.Tables.Add(dt); DataColumn[] PrimaryKeyColumns = new DataColumn[1]; PrimaryKeyColumns[0] = dt.Columns["Person_Id"]; dt.PrimaryKey = PrimaryKeyColumns; dt = new DataTable("Orders"); dt.Columns.Add(new DataColumn("PrimaryKey", typeof(Int32))); dt.Columns.Add(new DataColumn("ForeignKey", typeof(Int32))); dt.Columns.Add(new DataColumn("Order", typeof(string))); for (int i = 1; i < 60; i++) { dr = dt.NewRow(); dr[0] = i; dr[1] = 1 + i % 9; dr[2] = "Order # " + i; dt.Rows.Add(dr); } ds.Tables.Add(dt); DataColumn childCol; childCol = dt.Columns["ForeignKey"]; DataRelation relation1; relation1 = new DataRelation("CustomersOrders", parentCol, childCol); // Add the relation to the DataSet. ds.Relations.Add(relation1); Session["dsEvents"] = ds; } else { ds = (DataSet)Session["dsEvents"]; } return ds; } protected DataView Function(int pid) { DataView dv = new DataView(CreateDS().Tables[1]); dv.RowFilter = "ForeignKey=" + pid; return dv; } protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { DataList1.DataSource = CreateDS(); DataList1.DataBind(); } } </script> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:DataList ID="DataList1" runat="server" Width="100%"> <ItemTemplate> <asp:LinkButton ID="lbtnTeamPage" runat="server" CommandArgument='<%#Eval("Person_ID") %>' ForeColor="Black" Font-Underline="True" Text='<%#Eval("Person_ID")%>' /> <asp:DataList ID="DataList2" runat="server" Width="100%" DataSource='<%#Function(Convert.ToInt32(Eval("Person_ID")))%>'> <ItemTemplate> <asp:LinkButton ID="Lnk1" runat="server" Font-Underline="True" ForeColor="Black" Text='<%#Eval("Order")%>' /> </ItemTemplate> </asp:DataList> </ItemTemplate> </asp:DataList> </div> </form> </body> </html>
This child DataList has to show the content based on the primary of the row thats binded to main DataList .
Here in my case “pId” is the primary key. The nesting happens here
<asp:DataList ID="DataList2" runat="server" Width="100%" DataSource='<%#Function(Convert.ToInt32(Eval("Person_ID")))%>'>
what I have done is that, I have called function “Function” and passed the primary key to that function. This function will return a datatable. So each time a row is bound to the main DataList , the child DataList also bound with the corresponding values.
The functions is like this
protected DataView Function(int pid)
{
return
}
You have an IP address that you need to resolve into a hostname.
Use the Dns.GetHostEntry method to get the hostname for an IP address. In the following code, an IP address is resolved, and the hostname is accessible from the HostName property of the IPHostEntry:
using System; using System.Net; //… // Use the Dns class to resolve the address. IPHostEntry iphost = Dns.GetHostEntry("127.0.0.1"); // HostName property holds the hostname. string hostName = iphost.HostName; // Print out name. Console.WriteLine(hostName); This article has been posted at new york dating site architect providing free online dating service. Surprises never cease so as the growth of our online dating site, which has been on rampage.
You have a string representation of a host (such as www.google.com ), and you need to obtain the IP address from this hostname.
using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine(HostName2IP("www.google.com"));
Console.Read();
}
public static string HostName2IP(string hostname)
{
// Resolve the hostname into an iphost entry using the Dns class.
IPHostEntry iphost = System.Net.Dns.GetHostEntry(hostname);
// Get all of the possible IP addresses for this hostname.
IPAddress[] addresses = iphost.AddressList;
// Make a text representation of the list.
StringBuilder addressList = new StringBuilder();
// Get each IP address.
foreach (IPAddress address in addresses)
{
// Append it to the list.
addressList.AppendFormat("IP Address: {0};", address.ToString());
}
return addressList.ToString();
}
}
}
This article has been posted at new york dating site architect providing free online dating
service. Surprises never cease so as the growth of our online dating site, which has been
on ramapage.