Share via


Pass value of label in textbox to another page in asp.net

Question

Wednesday, August 12, 2020 4:38 PM

<asp:TemplateField HeaderText="CategoryName" ItemStyle-Width="150">
<ItemTemplate>

<asp:Label ID="lblCategoryName" runat="server" Text='<%# Eval("CategoryName") %>' ></asp:Label>

<asp:LinkButton ID="LinkButton" runat="server"
PostBackUrl='<%# "WallPaperCategory.aspx?id="+ Eval("Id") %>' Text="Edit" OnCommand="LinkButton_Command"></asp:LinkButton>

protected void LinkButton_Command(object sender, CommandEventArgs e)
{
Session["lblCategoryName"] = lblCategoryName.ToString();
}

(Destination Page where i want display value)

protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
{
using (WallpaperEntities7 ctx = new WallpaperEntities7())
{
string Id = Request.QueryString["Id"];
Session["lblCategoryName"].ToString();

}

All replies (2)

Thursday, August 13, 2020 2:15 AM âś…Answered

Hi Nirvriti Sharma,

 

There are two places you might need to change in your codes.

  • Change "Session["lblCategoryName"] = lblCategoryName.ToString();" to "Session["lblCategoryName"] = lblCategoryName.Text;". 
Session["lblCategoryName"] = lblCategoryName.ToString(); // You will get "System.Web.UI.WebControls.Label"
  • Remove the post back url for the link button. Instead, you should call "Response.Redirect" method in command event handler to move to next page after setting the Session value.

More details, you could refer to below codes:

Pass Value ASPX:

<form id="form1" runat="server">
        <h4>Pass Value Page</h4>
        <div>
            <asp:Label ID="lblCategoryName" runat="server" Text="This Text Will Be Passed To Another Page"></asp:Label>

            <asp:LinkButton ID="LinkButton" runat="server"
              Text="Edit" OnCommand="LinkButton_Command"></asp:LinkButton>
        </div>
    </form>

Pass Value Code behind:

protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void LinkButton_Command(object sender, CommandEventArgs e)
        {
            Session["lblCategoryName"] = lblCategoryName.Text;
            Response.Redirect("SessionShowValue.aspx");
        }

Show Value ASPX:

 <form id="form1" runat="server">
        <h4>Show Value Page</h4>
        <div>
            
            <asp:Label ID="displayValue" runat="server"></asp:Label>
         
        </div>
    </form>

Show Value Code behind:

protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                displayValue.Text = Session["lblCategoryName"].ToString();
            }
        }

Demo:

** **

** **

Hope this can help you.

Best regards,

Sean


Thursday, August 13, 2020 1:37 AM

You didn't ask a question or detail a problem that you have but one error in you code is that you probably want 

lblCategoryName.Text;

rather than lblCategoryName.ToString();

lblCategoryName is the entire control but it seems that you just want its Text property.