Thursday, September 4, 2008

Problem faced with xhtmlConformance mode="Legacy" Tag

I faced a typical problem in my ongoing project.. where the menu was not rendering properly..It was rendering like shown below...




The problem was that the web.config file contained a tag named as :

xhtmlConformance mode="Legacy"


This tag was supported in ASP.NET 1.0/1.1 only...
Read the solution here...
http://weblogs.asp.net/scottgu/archive/2006/12/10/gotcha-don-t-use-xhtmlconformance-mode-legacy-with-asp-net-ajax.aspx

At last the problem is solved...

Thanks,
Nitin Sharma
Software Engineer
.Net Technologies



Wednesday, September 3, 2008

Javascript window.open

To open a new window, you will need to use yet another ready-made JavaScript function. Here is what it looks like:

window.open('url to open','window name','attribute1,attribute2')

This is the function that allows you to open a new browser window for the viewer to use. Note that all the names and attributes are separated with a comma rather than spaces. Here is what all the stuff inside is:

  1. 'url to open'
    This is the web address of the page you wish to appear in the new window.

  2. 'window name'
    You can name your window whatever you like, in case you need to make a reference to the window later.

  3. 'attribute1,attribute2'
    As with alot of other things, you have a choice of attributes you can adjust.

Window Attributes

Below is a list of the attributes you can use:

  1. width=300
    Use this to define the width of the new window.

  2. height=200
    Use this to define the height of the new window.

  3. resizable=yes or no
    Use this to control whether or not you want the user to be able to resize the window.

  4. scrollbars=yes or no
    This lets you decide whether or not to have scrollbars on the window.

  5. toolbar=yes or no
    Whether or not the new window should have the browser navigation bar at the top (The back, foward, stop buttons..etc.).

  6. location=yes or no
    Whether or not you wish to show the location box with the current url (The place to type http://address).

  7. directories=yes or no
    Whether or not the window should show the extra buttons. (what's cool, personal buttons, etc...).

  8. status=yes or no
    Whether or not to show the window status bar at the bottom of the window.

  9. menubar=yes or no
    Whether or not to show the menus at the top of the window (File, Edit, etc...).

  10. copyhistory=yes or no
    Whether or not to copy the old browser window's history list to the new window.

Thanks,
Nitin Sharma

Custom Ajax Images Download





http://www.ajaxload.info/ 

Replace Strings in C#

If i have a String like this:

LDAP://CN=arth@test.com,OU=Consolidatedmessenger,OU=Hosting,DC=fabrikam,DC=com

and i want to remove a certain portion from this and want my string finally as :

LDAP://OU=test,OU=Hosting,DC=fabrikam,DC=com


This is how we will do :

string strVal = "LDAP://CN=arth@test.com,OU=Consolidatedmessenger,OU=Hosting,DC=fabrikam,DC=com";
string strToReplace = "";

int leng = strVal.IndexOf(",") - (strVal.IndexOf("/") + 1);

strToReplace = strVal.Substring(strVal.IndexOf("//") + 2, leng);

strVal = strVal.Replace(strToReplace, "");



Thanks ,
Happy Coding..!!
Nitin Sharma

Tuesday, September 2, 2008

Exporting Gridview to Excel

The Code

Listing 1: Default.aspx








Text="Export to Excel" />
Listing 2: Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
BindData();
}
}

private void BindData()
{
string query = "SELECT * FROM Categories";
SqlConnection myConnection = new SqlConnection(ConnectionString);
SqlDataAdapter ad = new SqlDataAdapter(query, myConnection);
DataSet ds = new DataSet();
ad.Fill(ds, "Categories");
GridView1.DataSource = ds;
GridView1.DataBind();
}

private string ConnectionString
{
get { return @"Server=localhost;Database=NorthWind;Trusted_Connection=true"; }

}
protected void BtnExport_Click(object sender, EventArgs e)
{
Response.Clear();
Response.AddHeader("content-disposition", "attachment;filename=FileName.xls");
Response.Charset = "";

// If you want the option to open the Excel file without saving then
// comment out the line below
// Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/vnd.xls";
System.IO.StringWriter stringWrite = new System.IO.StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
GridView1.RenderControl(htmlWrite);
Response.Write(stringWrite.ToString());
Response.End();
}
In the above listing, the GridView control will be populated with the data from the Categories table of the Northwind database. You will have to give the appropriate Server name or IP instead of localhost as server name in the above connection string.

If you use this code and try to export the GridView control, you will see an error message saying that the GridView control must be placed inside the form tags with the runat = server attribute.

This is pretty confusing, since your GridView is already inside the form tags and also contains the runat = server attribute. You can easily resolve this error by adding the following lines.

Listing 3: Overiding VerifyRenderingInServerForm Method

public override void VerifyRenderingInServerForm(Control control)
{
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET
server control at run time. */}

Thanks,
Nitin Sharma
Software Programmer
.Net Technologies

Merging Two DataTables

private void MergeDataTable()

{

DataTable dtOne = new DataTable("MyFirstTable");

dtOne.Columns.Add("Number");

DataTable dtTwo = new DataTable("MySecondTable");

dtTwo.Columns.Add("Number");

// Populate the first table

for (int i = 1; i <= 10; i++)

{

DataRow row = dtOne.NewRow();

row["Number"] = "Data" + i;

dtOne.Rows.Add(row);

}

// Populate the second table

for (int i = 11; i <= 20; i++)

{

DataRow row = dtTwo.NewRow();

row["Number"] = "Data" + i;

dtTwo.Rows.Add(row);

}

// Now merge the two tables /* ONLY ONE LINE OF CODE COOL RIGHT! */

dtOne.Merge(dtTwo);

GridView1.DataSource = dtOne;

GridView1.DataBind();

}


Thanks,
Nitin Sharma
Software Programmer
.Net Technologies

Parent page refresh after processing in the POP UP and closing the same

Hi All,
I spent 2-3 hours to search for this ....

POP UP should perform database interaction and close after the interaction and refreshing the parent page...

On Page1.aspx

butUser.Attributes.Add("onclick","window.open('AddUser.aspx',null,'Height=300,width=450,status=no,resizable=no,scrollbar=no,toolbar=no,location=no,menubar=no');");


It opens the AddUser.aspx page in a pop up..
On the AddUser.aspx page:

protected void butUser_Click(object sender, EventArgs e)
{
CreateUser();
this.butUser.Attributes.Add("onClientClick", "closePopup();");
Page.ClientScript.RegisterStartupScript(this.GetType(), "mes", "closePopup();", true);

}







Thanks,
Nitin Sharma