Sunday, April 25, 2010

Gridview - Highlight selected row after Postback

Problem:

I have a gridview to display employee information. When user clicks on employee no /name the particular row should be selected and selected row color should be changed. This has to be done after the postback.














Solution:


Since we are doing it after postback, the control losts the data as well selected row. We can do this with linkbutton control to change the color of the selected row very easily.


code snippet:

If you look at the snippet in the first image, I have used the linkbutton inside the itemtemplate. The important thing here is the property CommandName as Select. This property will take care of the selected row color changes. The row selection color will be taken from the SelectedRowStyle styles from the gridview.

Conclusion:


Hope this will be easy for everyone rather than doing it after the binding the grid with some extra code.

Happy coding...

Thursday, April 15, 2010

Open a page in the same browser using JavaScript without Postback

Problem:

Had a requirement to open up a page in the same browser window without postback to the existing page.

Solution:

In JavaScript, window.Open(...) is there to open a page in a new window. With the value of _self as a second parameter to the Window.Open, we could open the page in the same browser window.

Code Snippet:

#1 Using Image Button


OnClientClick="Javascript:window.open('Format.aspx','_self',null,false);return false;" />

#2 Using href anchor tag


href="javascript:window.open('Format.aspx','_self');" style="text-decoration:underline" id="A1" >Click...Here

I have opened the page called "Format.aspx" using window.open with the second parameter as "_self" on the OnClientclick of ImageButton. Finally give the return false to not posted back to the existing page.

Reference:
Refer the below Microsoft knowledge base library for more info.