user1125911
user1125911

Reputation: 129

Passing data from gridview as session

I've a gridview with client ID, client name, client contact number and a hyperlink to client detail. I would like to pass selected clint ID to another asp page (clientDetail.aspx & ClientContact.aspx). I'm thinking of passing the clientID as session. But how do i go about doing this? Can someone guild me on this as im quite new to this.

& how do i use the passed data in clientDetail.aspx & ClientContact.aspx?

Thanks in advance for your help.

Upvotes: 1

Views: 5551

Answers (3)

Rich
Rich

Reputation: 1915

You should be able to evaluate the clientid field of the asp.net gridview in the navgiate url of the hyperlink and pass it as a query string like so:

<asp:gridview id="OrdersGridView" 
        datasourceid="OrdersSqlDataSource" 
        autogeneratecolumns="false"
        runat="server">

        <columns>

          <asp:boundfield datafield="ClientID" 
            headertext="Client ID"/>
          <asp:boundfield datafield="ClientName" 
            headertext="Client Name"/>
          <asp:boundfield datafield="ClientContact" 
            headertext="Client Contact"/>
          <asp:hyperlinkfield text="Details..."
            navigateurl="~\details.aspx?clientid='<%# Eval("ClientID") %>'"            
            headertext="Order Details"
            target="_blank" />

        </columns>

      </asp:gridview>

Upvotes: 1

Mubarek
Mubarek

Reputation: 2689

Add two new columns of type HyperLinkField to your gridView as follows. Now clientID is passed as QueryString. One link is here

 <asp:HyperLinkField DataNavigateUrlFields="ClientID" 
                                DataNavigateUrlFormatString="~/ClientDetails.aspx?id={0}" 
                                Text="Client Details" />

Upvotes: 2

Crab Bucket
Crab Bucket

Reputation: 6277

Assuming that you are selected the grid row with a button or link button you could use the OnRowCommand event

When you wire into this event you can pick out the value you want from the selected item then save it into the Session which will then be available for subsequent pages. In the below exampel I've assumed the value is in a label field so you can pick it out of that control.

void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
{
     if (e.Row.RowType == DataControlRowType.DataRow)
     {
           Label lblMyValue = (Label)e.Row.FindControl("lblMyValue");
           Session["myValue"] = lblMyValue .Text;
     }
}

There are other variants of this for instance you could store the value you are interested in in the CommandArgument property of the button that you use to select the row. The command argument will then be available in the RowCommand event

 void ContactsGridView_RowCommand(Object sender, GridViewCommandEventArgs e)
 {
      string arg = e.CommandArgument;
      //.. put into session here
   }

And there are alternatives using different events for instance you could use the DataKeys collection of the GridView to store the value you are interested in and pick out the value from there

Markup fragment

<asp:gridview id="CustomersGridView" 
        //.. more properties   
        datakeynames="myID"
        onselectedindexchanged="MyGridView_SelectedIndexChanged"
        runat="server">

Code behind

 void MyGridView_SelectedIndexChanged(Object sender, EventArgs e)
  {

    int index = MyGridView.SelectedIndex;
    Session["myValue"] = CustomersGridView.DataKeys[index].Value.ToString();
  }

There are a number of alternatives to get this working. I would use the first one detailed if it were me - I've always found it easiest to get to work. You can make the label hidden with Css if you want - if it isn't suitable for the UI. Or use a hidden field (with runat="server"). I'm going to stop - I'm risking confusuing by just typing on.

Upvotes: 1

Related Questions