Getting A Selected Object from GridView

Rabbiya Shahid

I am fairly new to ASP.NET. I have a term project; the issue I am facing is described as follows:

  • I have a page named Contacts.
  • I have a class called User.
  • I have an ArrayList of User which is bound as a datasource to a Gridview in Contacts.

What I want to do is that when I click on a contact from the gridview, I want a new page to open which will give details of the selected contact. For that I need the contact in a Session Variable.

My problem is that I do not know how to "extract" the selected object from the GridView. I tried the event SelectedIndexChanged but that isn't working because the response isn't redirected to the new page.

I have considered making one Column of the GV a Hyperlink Field, but that will redirect response to a new page only. It doesn't store the selected contact in a session variable.

Edit: (code)

HTML

<asp:GridView ID="GVV2" runat="server" AutoGenerateColumns="False" 
Height="25px"style="margin-right: 4px" Width="616px" 
    onselectedindexchanged="GVV2_SelectedIndexChanged"  >
    <Columns>
        <asp:TemplateField>
            <HeaderStyle CssClass="checkBoxCell" />
            <ItemStyle CssClass="checkBoxCell" />
            <ItemTemplate>
        <asp:CheckBox ID="chk" runat="server" />
        </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="FullName_" HeaderText="Name">
        <HeaderStyle CssClass="SenderCell" />
        <ItemStyle CssClass="SenderCell" />
        </asp:BoundField>
        <asp:HyperLinkField DataTextField="EmailID_" HeaderText="Email ID" 
            NavigateUrl="~/ViewContact1.aspx" >
        <HeaderStyle CssClass="emailCell" />
        <ItemStyle CssClass="emailCell a.decor" />
        </asp:HyperLinkField>
    </Columns>
</asp:GridView>

Code Behind

public partial class Contacts1 : System.Web.UI.Page
{
    Storage1 STO;
    String currentUser;
    Users1 selectedUser;
    protected void Page_Load(object sender, EventArgs e)
    {
        Initiation();
        GVV2.DataSource = Contact.getContactList(currentUser, STO);
        GVV2.DataBind();
    }
    private void Initiation()
    {
        if (Session["SignInUser"] == null)
            Session["SignInUser"] = currentUser;
        else currentUser= (String)Session["SignInUser"];`

        if (Session["Storage"] == null)
            Session["Storage"] = STO;
        else
        {
            STO= new Storage1();
            STO = (Storage1)Session["Storage"];
        }

    }

    protected void GVV2_SelectedIndexChanged(object sender, EventArgs e)
    {
        Session["SelectedContact"]=GVV2.SelectedRow;
        Response.Redirect("ViewContact1.aspx");
    }
}
tilda

Using link button

Another way to do it is using gridview OnRowCommand instead of onselectedindexchanged.

Use linkbutton to redirect you to desired page. As a command argument you have to pass users ID.

<asp:TemplateField> 
    <ItemTemplate> 
        <asp:LinkButton ID="linkBtnDetails" runat="server" Text="Details" CommandName="Details" 
           CommandArgument='<%#Eval("ID")%>' /> 
    </ItemTemplate> 
</asp:TemplateField> 

Also you have to define onrowcommand for the gridview:

<asp:GridView ID="GVV2" runat="server" AutoGenerateColumns="False" 
Height="25px"style="margin-right: 4px" Width="616px" 
    onrowcommand="UserDetails_RowCommand"  >

And finally code behind where you will get user's ID, set it to session variable and redirect to page.

void UserDetails_RowCommand(Object sender, GridViewCommandEventArgs e)
  {
  if(e.CommandName=="Details")
  {
    Session["SelectedContact"]=e.CommandArgument;
        Response.Redirect("ViewContact1.aspx");
  }
  }

Using hyperlink field

Send ID in query string.

<asp:HyperLinkField DataTextField="EmailID_" HeaderText="Email ID" 
            NavigateUrl="~/ViewContact1.aspx?id="+"<%#Eval('ID')%>" >

In the code behind on the page load get the id like this:

string getUserID = Request.QueryString["id"];
int userID = Convert.ToInt32(getUserID);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Getting A Selected Object from GridView

From Dev

Getting information from an object selected using Mouse Listeners

From Dev

Getting the object of selected element from a list of elements Angular 4

From Dev

Getting row of selected DropDownList in GridView cells

From Dev

Thymeleaf select getting selected object

From Dev

Not getting data to gridview from database

From Dev

Getting value from dropdown in gridview

From Dev

add selected row from one gridview to another

From Dev

Fire selected row from database gridview

From Dev

Getting Selected value from NSTableView

From Dev

Getting selected item from a listbox

From Dev

getting all selected from listview

From Dev

Getting the selected year from textbox

From Dev

Getting data from gridview in asp.net

From Dev

Image Not getting binded in gridview from database

From Dev

Getting checkbox value from a gridview without id

From Dev

getting data from a gridview into an access database

From Dev

Getting ExpandableListView's selected item from ContextMenu

From Java

Android getting value from selected radiobutton

From Dev

Getting the selected value from combobox in Tkinter

From Dev

Getting selected matrix columns from a list of matrices

From Dev

Getting selected Index from DropDownListFor MVC

From Dev

getting attribute values from selected options in multiselect

From Dev

Getting URL of UIImage selected from UIImagePickerController

From Dev

Getting selected cell indices from uitable

From Dev

getting value of selected checkbox from datatable in jsp

From Dev

getting month from a table according to the selected month

From Dev

Jquery not getting the selected value from dropdown

From Dev

Getting selected value from datalist element

Related Related

  1. 1

    Getting A Selected Object from GridView

  2. 2

    Getting information from an object selected using Mouse Listeners

  3. 3

    Getting the object of selected element from a list of elements Angular 4

  4. 4

    Getting row of selected DropDownList in GridView cells

  5. 5

    Thymeleaf select getting selected object

  6. 6

    Not getting data to gridview from database

  7. 7

    Getting value from dropdown in gridview

  8. 8

    add selected row from one gridview to another

  9. 9

    Fire selected row from database gridview

  10. 10

    Getting Selected value from NSTableView

  11. 11

    Getting selected item from a listbox

  12. 12

    getting all selected from listview

  13. 13

    Getting the selected year from textbox

  14. 14

    Getting data from gridview in asp.net

  15. 15

    Image Not getting binded in gridview from database

  16. 16

    Getting checkbox value from a gridview without id

  17. 17

    getting data from a gridview into an access database

  18. 18

    Getting ExpandableListView's selected item from ContextMenu

  19. 19

    Android getting value from selected radiobutton

  20. 20

    Getting the selected value from combobox in Tkinter

  21. 21

    Getting selected matrix columns from a list of matrices

  22. 22

    Getting selected Index from DropDownListFor MVC

  23. 23

    getting attribute values from selected options in multiselect

  24. 24

    Getting URL of UIImage selected from UIImagePickerController

  25. 25

    Getting selected cell indices from uitable

  26. 26

    getting value of selected checkbox from datatable in jsp

  27. 27

    getting month from a table according to the selected month

  28. 28

    Jquery not getting the selected value from dropdown

  29. 29

    Getting selected value from datalist element

HotTag

Archive