Monday, June 16, 2008

Understanding the JavaScript __doPostBack Function

In this article we going to understand the the functionality of __doPostBack function of JavaScript.

_doPostBack Function
Let us take a look at the function.
Listing 1 - _The __doPostBack function


function __doPostBack(eventTarget, eventArgument) {
if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
theForm.__EVENTTARGET.value = eventTarget;
theForm.__EVENTARGUMENT.value = eventArgument;
theForm.submit();
}
}
Description
The __doPostBack function takes two arguments, eventTarget and eventArgument. The eventTarget contains the ID of the control that causes the postback and the eventArgument contains any additional data associated with the control. Note that the two hidden fields, “__EVENTTARGET” and “__EVENTARGUMENT,” are automatically declared. The value of the eventTarget and eventArgument are stored in the hidden fields. The two hidden variables can be accessed from the code behind using the forms/params collection.
Using the hidden variables you can also find the ID of the control which causes the postback. All you need to do is to retrieve the value of the __EVENTTARGET from the form parameter collection. Take a look at the code below.
Listing 2 – Getting the _EVENTTARGET hidden field
protected void Page_Load(object sender, EventArgs e)
{
string controlName = Request.Params.Get("__EVENTTARGET");
}
Description
For this code to work you need to add any web server control on the form except for Button and ImageButton control (Discuss it later). Let us add the DropDownList control and set the AutoPostBack property to true and populate the DropDownList with some dummy data. Now, run the page and view the source of the page.
You will see the following line of code.
Listing 3 – DropDownList calling __doPostBack function

The onchange event of the DropDownList calls the __doPostBack function. The ID of the control, “DropDownList1,” is also passed to the _doPostBack function and stored in the _EVENTTARGET hidden field. In the Page_Load I fetch the value of the _EVENTTARGET variable which in this case is the ID of the DropDownList. This way we can find out that which control caused the postback.
What about Buttons and ImageButtons?
You might be wondering about the POSTBACK triggered by the Buttons and the ImageButtons. Well, let us see the code generated by the Buttons.
Listing 4 – Code generated by the Button server control

As demonstrated in the code above, the Button control does not call the __doPostBack function. Because of this, the _EVENTTARGET will always be empty. However, you can find out the ID of the Button by looping through the form controls collection. Take a look at the code below.
Listing 5 – Finding the Button control in the form collection
foreach (string str in Request.Form)
{
Control c = Page.FindControl(str);
if (c is Button)
{
control = c;
break;
}
}
Description
In the code above I iterated through the controls on the page. If the control is of type Button then the loop breaks and the control is returned back to the user.
Passing Arguments
If you look closely at the __doPostBack function you will notice that the second argument is called the eventArgument. You can allow controls to pass arguments to the doPostBack function. Check out the code below.
Listing 6 – Passing arguments to the __doPostBack function


string passedArgument = Request.Params.Get("__EVENTARGUMENT");
Description
The “Button2” when clicked fires the DoPostBack function which in turn calls the __doPostBack. The __doPostBack function contains two arguments, eventTarget and eventArgument. The eventTarget is “Button2” and the eventArgument is “My Argument.” Later, in the C# code behind, I have accessed the eventArgument using the Request.Params collection. The passedArgument variable will contain the value “My Argument.”
Thanks to aspalliance.com

Tuesday, June 10, 2008

Add Grouped / Multiple row Header in the GridView

GridView the awesome control of the dot net is made so extendable that you can achive most thing you can thought about ,recently i

got the requirement that i have to show multiple row header in my application so i do some rearch and go a very simple solution.

Here we go.

What You have to do is just write some line of the code in the gridview RowDataBound event like

protected void GridViewData_RowDataBound(object sender, GridViewRowEventArgs e)
{

SortedList FormatCells = new SortedList();
FormatCells.Add("1", ",1,2");
FormatCells.Add("2", "TopGroup,4,1");


SortedList FormatCells2 = new SortedList();
FormatCells2 .Add("1", "Subgroup1,2,1");
FormatCells2 .Add("2", ""Subgroup2,1,1");
GetMultiRowHeader(e, FormatCells2 );
GetMultiRowHeader(e, FormatCells );
}

And Here is the function

public void GetMultiRowHeader(GridViewRowEventArgs e, SortedList GetCels)
{

if (e.Row.RowType == DataControlRowType.Header)
{
GridViewRow row;
IDictionaryEnumerator enumCels = GetCels.GetEnumerator();

row = new GridViewRow(-1, -1, DataControlRowType.Header, DataControlRowState.Normal);
while (enumCels.MoveNext())
{

string[] count = enumCels.Value.ToString().Split(Convert.ToChar(","));
TableCell Cell;
Cell = new TableCell();
Cell.RowSpan = Convert.ToInt16(count [2].ToString());
Cell.ColumnSpan = Convert.ToInt16(count [1].ToString());
Cell.Controls.Add(new LiteralControl(count [0].ToString()));
Cell.HorizontalAlign = HorizontalAlign.Center;
Cell.ForeColor = System.Drawing.Color.White;
row.Cells.Add(Cell);
}


e.Row.Parent.Controls.AddAt(0, row);

}
}

Tuesday, June 3, 2008

Magic Of References

"If C# manipulates objects by reference, will a swap work?" - This issue must certainly have troubled quiet a lot of beginners. C# does manipulate objects by reference, and all object variables are references. On the other hand, C# does not pass method arguments by reference; it passes them by value, (even if the method arguments are of reference type). Thus, a regular swap method will not work! Let's come from the beginning, we know that there are four kinds of formal parameters in a C# method declaration, namely Value parameters, which are declared without any modifiers. (default) Reference parameters, which are declared with the ref modifier. Output parameters, which are declared with the out modifier. Parameter arrays, which are declared with the params modifier. Thus if there is no any method parameter keyword (ref or out), the parameter can have a value associated with it. That value can be changed in the method, but that value will not be reflected when the control passes back to the calling procedure. This is true for all value types. Now, consider a reference type, an object. If we pass an object to a method and if the value of its member is changed, it will be retained even when the control is passed back to the calling procedure! This is because objects are manipulated by reference. If so, then, if we pass two objects to a regular swap method and come back to the calling procedure to see whether they have got swapped, they would have not! Look down the code below,



The out put is as follows



In the above program, we pass the instances obj1 and obj2 of class1 to badswap_noref() method and see that they are not actually getting swapped. This is what actually happens in the badswap_noref method. Not the actual references obj1 and obj2 are passed. But a copy of the references is passed. So param1 and param2 contains a copy of the actual references obj1 and obj2 respectively. We are able to modify the values of X and Y in the badswap_noref method thru param1 and param2, because param1 and param2 still points to the same address locations referred to by obj1 and obj2. But what we are swapping is only param1 and param2, the method references, which are not retained when we come back to the calling procedure, Main().





Thus, in C#, a swap method can be made possible by using the ref keyword. In such a case, we pass the object references explicitly by reference and not by value, thus able to swap them right in the called method.




thanks to