arsha
arsha

Reputation: 63

Get record id using GridView Linkbutton on one page and get on another page in ASP.NET using C#

How do I submit the id from one page to another page in ASP.NET using C#? I have a GridView control and each record contains a link button:

<asp:TemplateField>              
    <ItemTemplate>
      <asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" />
    </ItemTemplate>
</asp:TemplateField>

I've bound the id to link button:

if (e.Row.RowType == DataControlRowType.DataRow)
{
    ((LinkButton)e.Row.FindControl("lbtn_naatscat")).CommandArgument 
        = DataBinder.Eval(e.Row.DataItem, "cate_id").ToString();
    ((LinkButton)e.Row.FindControl("lbtn_naatscat")).Text 
        = DataBinder.Eval(e.Row.DataItem, "title").ToString();
}

now I want to pass this id to another page when user click to this link button.

Upvotes: 3

Views: 5155

Answers (8)

Charles Lambert
Charles Lambert

Reputation: 5132

Set the PostBackUrl and CommandArgument properties of the LinkButton in your source page like so:

 <asp:TemplateField>              
   <ItemTemplate>
     <asp:LinkButton ID="lbtn_naatscat" runat="server" Text="arshad" CommandName="view_user_naats_gv" Font-Underline="false" PostBackUrl="~/YourPage.aspx" CommandArgument='<%# Eval("submitid")%>' />
   </ItemTemplate>
 </asp:TemplateField>

Then on your page that is being submitted to you can place the following directive to the DestinationPage.aspx page:

<%@ PreviousPageType VirtualPath="~/SourcePage.aspx" %>

I don't know what type of container the <ItemTemplate> tag is from (ListView?). However you can find the control in the DestinationPage.aspx page like so:

if (Page.PreviousPage != null && Page.PreviousPage.IsCrossPagePostback) {
  // if you use the PreviousPageDirective you should be able to access the ListView
  // property directly
  // var listView = Page.PreviousPage.MyListView;
  var listView = Page.PreviousPage.FindControl("MyListView") as ListView;
  var linkButton = listView.FindControl("lbtn_naatscat") as LinkButton;
  var submitId = linkButton.CommandArgument;
}

Upvotes: 1

Kevin Kunderman
Kevin Kunderman

Reputation: 2134

I would use a HyperLinkField field like so

 <asp:HyperLinkField DataNavigateUrlFields="Database ID Field" 
                           DataNavigateUrlFormatString="~/PageName.aspx?ID={0}" 
                           DataTextField="Database ID Field" HeaderText="ID" />

And then on the page that you're redirecting to, you would check the query string for ID

   string id = Request.QueryString["ID"]

Upvotes: 1

Freelancer
Freelancer

Reputation: 9074

You can create a Session for this purpose.

Do as follows:

Session["Id"]=e.CommandArgument.ToString() //Id you want to pass to next page

In this way your Session variable will get created. And you will be able to access it on the next page.

While retrieving it on next page:

Id=Session["Id"]

Other Alternatives:

  1. View state

  2. Control state

  3. Hidden fields

  4. Cookies

  5. Query strings

  6. Application state

  7. Session state

  8. Profile Properties

State management Techniques in ASP.NET:

http://msdn.microsoft.com/en-us/library/75x4ha6s%28v=vs.100%29.aspx

Hope its helpful.

Upvotes: 2

cgalvao1993
cgalvao1993

Reputation: 148

Send the id this way

 Response.Redirect("/yourpage.aspx?" + id);

And get it this way

 String s = "";
            foreach (String item in Request.QueryString.Keys)
            {
                s = Request.QueryString[item];
            }
            if (s != "")
            {
                id = Int32.Parse(s);
            }

Upvotes: 0

Joe Ratzer
Joe Ratzer

Reputation: 18549

I think it would make more sense to use a hyperlink in your GridView. For example:

<ItemTemplate>
    <asp:HyperLink ID="link_naatscat" runat="Server"                                             
        Text="arshad"
        ToolTip="arshad"
        NavigateUrl=<%#"~/YourPage.aspx?id=" + Eval("submitid")%>>
    </asp:HyperLink> 
</ItemTemplate>

Upvotes: 0

Sachin
Sachin

Reputation: 40970

Using Row_Command event you can send it next page using query string.

protected void gvDeals_RowCommand(object sender, GridViewCommandEventArgs e)
{
   if(e.CommandName.Equals("MyCommand")
   {
      string value=calculate();
      Response.Redirect("~/MyPage.aspx?item=" + value);
   }
}

Upvotes: 0

Tim Schmelter
Tim Schmelter

Reputation: 460158

You can handle the LinkButton's Command event and use the CommandArgument:

void LinkButton_Command(Object sender, CommandEventArgs e) 
{
    if(e.CommandName == "view_user_naats_gv")
    {
        Resonse.Redirect("UserNaats.aspx?catID=" + e.CommandArgument.ToString());
    }
}

Upvotes: 3

MrB
MrB

Reputation: 424

Store it in a hidden field, and access it on the next page in the code behind file.

Here is another SO post with

Another example of passing hidden field in ASP

Best of luck!

Upvotes: 1

Related Questions