Archive for April 12th, 2008

Passing Data one page to another page in ASP.NET?

Saturday, April 12th, 2008

we can only retrieve the values of the web form objects using the Request object. But what if we need to retreive the entire object from a different aspx page. In Classic ASP, passing an entire object from one page to another was possible with the help of either Session Objects or Application Object or we need to implement a custom way to transfer objects between ASP
pages.

Assume that, we have a page called source.aspx with one textbox and a button. When we click on the button, we are going to invoke a different page called destination.aspx. Our aim is to retrieve the entire texbox object from the destination.aspx.

Content of source.aspx
<%@ Page Language=”vb” Classname=”Source” %>
<script language=”VB” runat=”server”>
Sub Transfer(sender As Object, e As EventArgs)
Server.Transfer(”destination.aspx”)
End Sub

Public ReadOnly Property GetFirstname() As String
Get
Return txtFirstName.Text
End Get
End Property

Public ReadOnly Property GetFirstnameObj() As System.Object
Get
Return txtFirstName
End Get
End Property
</script>

Content of Destination.aspx
<script language=”VB” runat=”server”>
Sub Page_Load(sender As Object, e As EventArgs)
Dim objSource As source
objSource = CType(context.Handler, source)
Response.Write(”First Name is : ” & objSource.GetFirstnameObj.text & ”
“)
Response.Write(”ForeColor is : “)
Response.Write(objSource.GetFirstnameObj.ForeColor)
Response.Write(”
BackColor is : “)
Response.Write(objSource.GetFirstnameObj.BackColor)
End Sub
</script>

Posted by Mahesh ( Tryangled )

Merging two Datasets into a single Datagrid in ASP.NET?

Saturday, April 12th, 2008

Merge is one of the method of Dataset. The merge feature is basically used in applications where the concept of Master and
Transaction table exists.we need to have the following pre-conditions.
Pre-conditions for displaying two datasets in a single datagrid.
1) The primary thing is that, both all the columns specified in the datagrid must be present in both datasets.
2) The data type of all columns in the datasets must be same.
3) Also the column name should match.
Sub BindGrid()
Dim myConnection as New SqlConnection (strConn)
Dim DS1 As DataSet
Dim DS1 As DataSet
Dim MyCommand As SqlDataAdapter
MyCommand = new SqlDataAdapter(”exec s_get_table1″, MyConnection)
DS1 = new DataSet()
MyCommand.Fill(DS1, “MyTable”)
MyCommand = new SqlDataAdapter(”exec s_get_table2″, MyConnection)
DS2 = new DataSet()
MyCommand.Fill(DS2, “MyTable”)
ds1.merge(ds2)
MyDataGrid.DataSource=DS1.tables(0).DefaultView
MyDataGrid.DataBind()
End Sub

Posted by Mahesh ( Tryangled )

How to export data in Datagrid on a webform to Microsoft Excel?

Saturday, April 12th, 2008

Two techniques for exporting the data in the DataGrid:

* Using the Excel MIME Type (or Content Type)

With server-side code, you can bind the DataGrid to your data and have the data open in Excel on a client computer. To do this, set the ContentType to application/vnd.ms-excel. After the client receives the new stream, the data appears in Excel as if the content was opened as a new page in the Web browser.

* Using Excel Automation

With client-side code, you can extract the HTML from the DataGrid and then Automate Excel to display the HTML in a new workbook. With Excel Automation, the data always appears outside the browser in an Excel application window. One advantage to Automation is that you can programmatically control Excel if you want to modify the workbook after the data is exported. However, because Excel is not marked as safe for scripting, your clients must apply security settings in the Web browser that allow Automation.

Posted by Mahesh ( Tryangled )

How to display a Tooltip when hovering over the Header sort link of the DataGrid?

Saturday, April 12th, 2008

protected void ItemDB (object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
for(int i=0 ;i<= e.Item.Cells.Count -1;i++)
{
e.Item.Cells[i].ToolTip = “This is Column ” + i.ToString();
}
}

Posted by Mahesh ( Tryangled )

How to display multiple dates selected in Calendar Control in asp.net?

Saturday, April 12th, 2008

Set the SelectionMode property of Calendar Control to DayWeek/ DayWeekMonth.
Code for the OnSelectionChanged as follows:

string strval=”" ;
foreach (DateTime day in Calendar1.SelectedDates)
{
strval += (day.Date.ToShortDateString() + “<br>”);
}
Response.Write(strval);
}

Posted by Mahesh ( Tryangled )