Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

linkbutton

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.

like image 690
arsha Avatar asked Dec 01 '25 00:12

arsha


2 Answers

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());
    }
}
like image 182
Tim Schmelter Avatar answered Dec 02 '25 14:12

Tim Schmelter


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.

like image 32
Freelancer Avatar answered Dec 02 '25 14:12

Freelancer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!