How to redirect from gridview based on the selected row details. asp.net,c#,sql server 2008

Hi friends, in our businness solutions searching the details from database and listing it into a gridview is most common thing in native asp.net ,c# applications. based on the selected row you may need to update the details.here we are going to explain how we can implement it.

Here is the aspx code for the gridview


so here for editing the items in the grid we have an edit button in each row.
we are using OnRowCommand="grdCategory_RowCommand"   so in code behind we have an RowCommand event handler class. it will check the id's which we have passed and redirect to the corresponding page. here we need to pass some values from the  parent page to the redirecting page. for that we have used   DataKeyNames="Id"   in our grid here we have to pass only one value so we have kept only Id in DataKeyNames if you have more values you can put it as  comma separated.

So next step is binding the details to grid.
that details you can check over here.
http://androidsharp.blogspot.in/2013/10/binding-grid-view-on-page-load-with.html

So now we will check what all the things we need to do in RowCommand class

Here firs we will check the command name if its correct then we will retrieve  the data key name value based on the row we have selected.
then for redirecting to next page you can either use Response.Redirect(url) or Server.Transfer(url)
server.transfer directly sends the http request to the server but in case of response.redirect first it will send request to browser then browser will send a request to the server so depending on your requirement you can use which ever you want.

we are using GET method to pass the values from one page to other . so here you can see the values are attached along with the url.

you need to pass Resposne.Redirect(url,false) . if you wont give false over there you may get some exception you can catch it and throw it using ThreadAbortException

so in the second page page load you can retrieve the value and you can make whatever functionality you want with that values.

you need to use Request.QueryString["PassedValueName"] 
for retrieving the value in the second page.
you can store your values to ViewState, If you want that value only in that page. if you want it through the application you can go with session

0 comments: