0
Posted on 9:46 PM by prajeesh and filed under
Some websites does not allow right click context menu option for security reasons, you can also achieve this by adding following code to your body tag of the page.

Alternatively, you can show an alert saying "Right click disabled" if you call a function from body tag's oncontextmenu event, for eg:-

Disclaimer: I have tested this functions in IE7 and Mozilla 3.0 only , it will not work in Opera.
Enjoy coding

Shout it kick it on DotNetKicks.com
0
Posted on 9:12 PM by prajeesh and filed under
In some websites you may have noticed a fade effect while browsing the pages, we may feel an ajax effect and may not feel slow page load, just add the following meta tag to your page before the body tag.


Please note that it will not work in Mozilla. Shout it kick it on DotNetKicks.com
2
Posted on 4:18 AM by prajeesh and filed under ,
Sometimes you may need to call a method in the parent page from a child user control, for eg:- we have a user control with New, Save, Delete buttons and based on the page type we may need to call appropreate methods.
Before going to actual aim of this post we must have an idea about delegates and event handlers.
What is a delegate & how should we use it?
A deligate is just like a function pointer in c/c++ , delegates can be used to call a method where the call method can be determined only at run time.
How can we declare, instantiate and call a deligate?
Suppose we have a method to add two numbers and display the result in the webpage.
Step 1 : Declaring a delegate

public delegate void SumOfTheNumbers(int a, int b);

where delegate is a keyword and void is the return type of the delegate.
Step 2: Instantiating and calling the delegate

DisplayNumbersDelegate objDisDelegate = new DisplayNumbersDelegate(AddNumbers);
objDisDelegate(5 ,10 );

where "objDisDelegate" is the delegate variable and "AddNumbers" is the function to be called, note that signature of the AddNumbers function must be similer to the delegate we declared, that is it must accept two integer parameters and returns void, when we instantiate the delegate object we are pointing the AddNumbers function to the delegate variable, we can use delegate variable to call the method by passing the values, here we are calling only one function using this delegate this is called single cast delegate, you can also use delegates to call multiple functions also this type of delegates are called multi cast delegates.
Calling a method in a parent page from a user control :
Suppose we have two text boxes and an add button in the user control and we want to call a method to add two numbers declared in the parent page and display result in the same page.
Step 1: Declare the delegate and event in user control.

public delegate void SumOfTheNumbers(int a, int b);
public event SumOfTheNumbers sumnos;

Step 2:Passing the parmeters to the delegate from click event of the Add button in the user control

protected void btnAdd_Click(object sender, EventArgs e)
{
sumnos(int.Parse(txtBoxNum1.Text), int.Parse(txtBoxNum2.Text));
}

Step 3:Declare the add method in parent page.

public void sumofthenumber(int a, int b)
{
Response.Output.Write("sum of{0} and {1} is : {3} ",a,b,a+b);
}

Step 4: Instantiate the delegate from the page load event of the parent page.

protected void Page_Load(object sender, EventArgs e)
{
delControl1.sumnos += new TestProject.UserControls.delControl.SumOfTheNumbers(sumofthenumber);
}

and you are done, hope you enjoyed this post.






Shout it kick it on DotNetKicks.com
0
Posted on 9:52 PM by prajeesh and filed under
Eventhough it is a tech blog, in this post i would like to share my happiness of being a father. We blessed with a baby girl on October 20th this year (2009) , we named her "Prarthana" meaning is Prayer.
Me and my wife Dhanya are enjoying the new life of parenthood with her :).
See her photo taken on 28th day :

Shout it kick it on DotNetKicks.com
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 :

Shout it kick it on DotNetKicks.com