I am fairly new to ASP.NET. I have a term project; the issue I am facing is described as follows:
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");
}
}
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.
Comments