0
Posted on 3:02 AM by prajeesh and filed under ,
Working with Gridview inside Gridview.

Grid view is a very useful and easier to use data presentation control in asp.net it is having lots of default features that we can set very easily, but in some of the projects you may need to show a master client relationship to the users For eg:- List of students who are studying in different departments.
We can handle this situation by using nested grid views, ie a Gridview inside a Gridview.
Microsoft is providing a solution for this situation msdn website , see the link : http://msdn.microsoft.com/en-us/library/aa992038(VS.80).aspx

I think Microsoft's solution contains lot of steps to complete the process, I done a workaround on this and come up with a solution, let me explain the tasks in step by step with an example.
Step 1:
Create a gridview named gvDepartments and add a Template Field in it.
Step 2:
Inside the Template Field'd Item Template add another gridview called gvStudents.
Step 3:
Add following code in gvStudents grid view
DataSource ='<%# GetStudentInfo( Convert.ToInt16(Eval("Department_Id")) ) %>'
Where GetStudentInfo is a server side function that returns a datatable containing the list of students based on department id.
Source of the Grid views will be like below code :

DataKeyNames="DepartMent_Id" CellPadding="4" ForeColor="Black"
GridLines="Vertical" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px">




<%# Container.DataItemIndex+1 %>





DataSource ='<%# GetStudentInfo( Convert.ToInt16(Eval("Department_Id")) ) %>'
CellPadding="4" ForeColor="#333333" GridLines="None" ShowHeader="False"
AutoGenerateColumns="False">




<%# Container.DataItemIndex +1 %>






















Step 4
Create Method to bind gvDepartments (Must contain a column named “Department_Id” as we are passing this parameter to bind gvStudents).
Step 5
Create a Method named GetStudentInfo(int department_Id) , It accepts Department_Id as parameter and returns a
datatable contains students list,(example given below) and you are done.

public DataTable StudentsByDepartment(int DepartmentId)
{
SqlConnection dbConnection = new SqlConnection(ConnectionString);
DataTable dtStudentList = new DataTable();
try
{
dbConnection.Open();
SqlDataAdapter daStudents = new SqlDataAdapter();
SqlCommand cmdSelect = new SqlCommand("SelectStudentByDep",dbConnection);
cmdSelect.CommandType = CommandType.StoredProcedure;
cmdSelect.Parameters.AddWithValue("@Dep", DepartmentId );
daStudents.SelectCommand = cmdSelect;
daStudents.Fill(dtStudentList);
}
catch (Exception objException)
{
HttpContext.Current.Response.Write(objException.Message);
}
finally
{
if (dbConnection != null && dbConnection.State == ConnectionState.Open)
{
dbConnection.Close();
}
}
return dtStudentList;
}


Figure: Sample output of a nested Gridview :

1
Posted on 5:43 AM by prajeesh and filed under
In some situations we may need to maintain the scroll bar position when we are dealing with large pages with a button causes post back, you can use achieve this by adding MaintainScrollPositionOnPostback=”true” in @Page directive.

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" MaintainScrollPositionOnPostback ="true" %>
0
Posted on 10:29 PM by prajeesh and filed under ,
Here is the list of some commonly used reguler expressions for validating your forms.
E-mail
^([0-9a-zA-Z]([-\.\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\w]*[0-9a-zA-Z]\.)+[a-zA-Z]{2,9})$
URL
^(htf)tp(s?)\:\/\/[0-9a-zA-Z]([-.\w]*[0-9a-zA-Z])*(:(0-9)*)*(\/?)([a-zA-Z0-9\-\.\?\,\'\/\\\+&%\$#_]*)?$
Social Security Number
^\d{3}-\d{2}-\d{4}$
Phone number(Validates US Phone number)
^[01]?[- .]?(\([2-9]\d{2}\)[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$
Zip Code(Validates US Zip code)
^(\d{5}-\d{4}\d{5}\d{9})$^([a-zA-Z]\d[a-zA-Z] \d[a-zA-Z]\d)$
Currency(Non Negative)
^\d+(\.\d\d)?$
Currency(+ve or -ve)
^(-)?\d+(\.\d\d)?$
Non Negative Integer
^\d+$
For a detailed article on reguler expressions, refer : http://msdn.microsoft.com/en-us/library/ms972966.aspx
2
Posted on 4:17 AM by prajeesh and filed under ,
Sometimes you may be want to show your latest twitter tweets in your website or blog, most of the cases you are doing this by using widgets with limited customization facilities and showing ads or links to other websites, here is an easier way to achieve this using twitter API and javascript.
Step 1:
First, decide where about on your page you want your last tweet to display. Then paste following html code there.
Step 2:
Next you need to put these 2 lines of JavaScript below the code in step 1. On the 2nd line of code where it says prajeeshkk.json, you need to replace prajeeshkk with your twitter username.

Step 3:(Optional)
You can apply css and make the div displaying the tweet stylish.


See how my tweet design looks :

0
Posted on 4:06 AM by prajeesh and filed under ,
In some applications we may need to reset all controls in using a single "Reset" button click, here is the c# code to achieve this.

public static void ResetControls(ControlCollection pagecontrols, bool txtbox, bool dropdownlist, bool label)
{
foreach (Control cntrl in pagecontrols)
{
foreach (Control mycontrols in cntrl.Controls)
{
if (txtbox)
{
if (mycontrols is TextBox)
{
(mycontrols as TextBox).Text = string.Empty;
}
}
if (dropdownlist)
{
if (mycontrols is DropDownList)
{
(mycontrols as DropDownList).SelectedIndex = 0;
}
}
if (label)
{
if (mycontrols is Label)
{
(mycontrols as Label).Text = string.Empty;
}
}
}
}
}

We can call this function using following format if you want to clear all controls except label

FormControl.ResetControls(this.Controls, true, true, false);