Search This Blog

Wednesday, September 24, 2008

How do I catch the RowIndex in GridView RowCommand event?

The Solution for catching it is as below

Hi,

In row command if you want to get the current row or its index, It is not directly possible. There is two way, one is on row created event on the link button(suppose, it is used for row command) save row index in link button command argument attribute.

And get the argument from row command event using e.CommandArgument

But if you want to send another value as a command argument then it creates problem. To solve this just use this code

GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Here link button (or any source) that cause to enter in row command event. Now you have selected row so you don’t need any index and directly access any row variable as sample code is given below
Label lblProdId = (Label)row.FindControl(”lblproductId”);

So whole code is just two line. Below I write whole code

-------------------------------------------------------------------------------
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);

Label lblProdId = (Label)row.FindControl(”lblproductId”);

-------------------------------------------------------------------------------
Source from : http://ranafaisal.wordpress.com/2008/03/31/how-to-get-the-current-row-in-gridview-row-command-event/

Saturday, September 20, 2008

How to add a default ListItem to a DropDownList

This can be done one of 2 ways. A default ListItem can be added to a DropDownList programmatically with the following syntax after binding data to the DropDownList:


//Code here to populate DropDownList

DropDownListID.Items.Insert(0, new ListItem("Default text", "Default value")

This will add a ListItem to index 0, which will be the first ListItem.

In .NET 2.0, this can be done declaratively using the AppendDataBoundItems property. This will append all data-bound ListItems to the DropDownList, leaving those you add manually as the first selections.




Solution by : Ryan Olshan
ASPInsider | Microsoft MVP, ASP.NET
http://ryanolshan.com