Last post Jan 08, 2012 10:27 PM by b471code3
Jan 08, 2012 05:57 PM|BKirk|LINK
I have a Gridview on an ASP.NET C# page using the RowDataBound event to fill my data row by row. This is working 100%.
However i want to apply a response.redirect to another page under the following rules:
1) 1 product row of data - Needs to redirect to my Dashboard.aspx page and pass the ProductID in the Querystring from the 1 product in the row item
(i believe i need to use the Rowdatabound event to grab the productID of this rowitem to pass in the Querystring?)
2) more than 1 product row of data - stay on the page and display my Gridview of multiple products. This works just fine.
I thought i could use GridView1.Rows.Count within my RowDataBound event and simply redirect when the count equals a value of 1, but i think this Count is incremental each time the RowDataBound loops through the Gridview row items, hence
it will not know the Total Row Count until it gets to the end.
How can i condition a redirect and grab the single ProductID when there is only 1 Product row?
Jan 08, 2012 06:40 PM|Kulrom|LINK
Why don't you use the DataItem e.g.
Dim paramvalue As String = DataBinder.Eval(e.item.DataItem, "ProductID").ToString()
string paramvalue = DataBinder.Eval[e.item.DataItem, "ProductID"].ToString();
Now you can pass the paramvalue in the querystring
Jan 08, 2012 07:10 PM|BKirk|LINK
I know how to use the ProductID in the Querystring to pass to the other page. That was not my question.
I need to know what logic i can use to get the Total count of rows in the Gridview within the RowDataBound event and then decided if there is only 1 row to redirect to another page and if more than 1 row then stay on the existing page and show the grid.
How can i insert this logic and get the total row count and store the single row ProductID at the same time?
Jan 08, 2012 08:41 PM|b471code3|LINK
Jan 08, 2012 10:04 PM|BKirk|LINK
I suppose i can store the first ProductID from the first row in a variable using your suggested count logic of <=1 but then i would still need to code the logic for the redirect.
Am i correct in assuming the RowDataBound event is a routine that simply loops through however many rows are present in the gridview data? I also assume the GridView1.Rows.Count property increments each time the RowDataBound event loops through?
If my assumptions are correct then surely i need to wait for all rows to be bound before i get the total count of rows? Therefore where would i code the redirect logic? Surely i must be at the end or perhaps in the next event that fires after RowDataBound?
Jan 08, 2012 10:27 PM|b471code3|LINK
You are correct, you will not know the Row.Count until after the GridView has finished binding, which is why I suggested doing your work in the DataBound event of the gridview. Also, you don't need to go after the cells to get the product id if you don't
have it in there already like I originally suggested. Try this out instead:
protected void GridView1_DataBound(object sender, EventArgs e)
if (GridView1.Rows.Count <= 1)
int prodId = 0;
prodId = (int)GridView1.DataKeys.Value; // get the product id
Response.Redirect("~/ProductDetail.aspx?ProdId=" + prodId.ToString());
Only thing with this example is you need to specify the DataKeyNames in the gridview definition.