• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 415
  • Last Modified:

How can I figure out which linkbutton a user has clicked?

I have the attached code which is attached to the onClick event of a linkbutton.

Each linkbutton is beside a label.  That label holds some text that is put into the session variable.  

<asp:LinkButton ID="lnkEmailLink" runat="server" OnClick="lnkEmailLink_Click" Text="Email" />
<asp:Label ID="lblEmailUser" runat="server" Text="jcoulson" Visible="false" />

This works fine if the page only has one of these linkbutton/label combinations.  If there are more, I will need to have a different click function for each label.  What I need is one function that can tell which label is being used.  This is where it gets tricky for me.  The script needs to know which label to get its session variable data from.

Ideas?  I keep trying things, but they don't work out.  I'm having a hard time getting this one rolling.

Jeremy
protected void lnkEmailLink_Click(object sender, EventArgs e)
        {
            // Set a session variable equal to the text of the hidden label.
            Session["emailUser"] = Server.HtmlEncode(lblEmailUser.Text);
            // Go to the form page.
            Response.Redirect("email_form.aspx");
        }

Open in new window

0
mrcoulson
Asked:
mrcoulson
  • 5
  • 3
  • 2
  • +2
2 Solutions
 
David RobitailleAnalyst ProgrammerCommented:
are those link buttons in a repeating structure?
if yes, you could cast sender as a control (or a linkbutton) and use findcontrol on it s parent property.
 
 
0
 
mrcoulsonAuthor Commented:
I think the answer is no.  
0
 
David RobitailleAnalyst ProgrammerCommented:
then, does all your LinkButton cannot have the same id???
plese post some of your aspx code...
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tillgeffkenCommented:
I'm unsure if you really need those hidden labels. Try this:

<asp:LinkButton ID="lnkEmailLink" runat="server" Text="Email" OnClick="lnkEmailLink_Click" CommandArgument="jcoulsen" CommandName="SetSessionVariable" OnCommand="SetSessionVariable" />

Maybe you want to generate this control programmatically.

protected void SetSessionVariable(object sender, EventArgs e)
        {
               Session["emailUser"] = Server.HtmlEncode(e.CommandArgument);
        }

Open in new window

0
 
mrcoulsonAuthor Commented:
davrob: Right, that's the problem.  This works on the id of one label, but there may be many email links on one page.  I'm attaching my ASPX.

tillgeffken: I'll try that now.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test_link.aspx.cs" Inherits="email_form.test_link" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="lnkEmailLink" runat="server" OnClick="lnkEmailLink_Click" Text="Email" />
        <asp:Label ID="lblEmailUser" runat="server" Text="jcoulson" Visible="false" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
GiftsonDJohnCommented:
Hi,

lillgeffken is correct. You can use CommandName and CommandArgs to differentiate which linkbutton is pressed if you are using same event handler for multiple controls.

The code needs a minor change.

        protected void SetSessionVariable(object sender, CommandEventArgs e)
        {
               Session["emailUser"] = Server.HtmlEncode(e.CommandArgument); // e.CommandName can also be used.
        }
0
 
David RobitailleAnalyst ProgrammerCommented:
in that case, tillgeffken solution will work.
0
 
mrcoulsonAuthor Commented:
tillgefken: On e.CommandArgument, I get this error:

System.EventArgs' does not contain a definition for 'CommandArgument' and no extension method 'CommandArgument' accepting a first argument of type 'System.EventArgs' could be found (are you missing a using directive or an assembly reference?)

Jeremy
0
 
tillgeffkenCommented:
Sorry, typo, need more coffee...

protected void SetSessionVariable(object sender, CommandEventArgs e)
        {
               Session["emailUser"] = Server.HtmlEncode(e.CommandArgument);
        }

Open in new window

0
 
mrcoulsonAuthor Commented:
"The best overloaded method match for 'System.Web.HttpServerUtility.HtmlEncode(string)' has some invalid arguments."

Am I totally putting this in the wrong way now?  I'm attaching my ASPX and my CS.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
namespace email_form
{
    public partial class test_link_2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
 
        }
 
        protected void lnkEmailLink_Click(object sender, EventArgs e)
        {
 
        }
 
        protected void SetSessionVariable(object sender, CommandEventArgs e)
        {
            Session["emailUser"] = Server.HtmlEncode(e.CommandArgument);
        }
    }
}
 
 
 
 
 
 
 
 
 
 
 
 
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="test_link_2.aspx.cs" Inherits="email_form.test_link_2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:LinkButton ID="lnkEmailLink" runat="server" Text="Email" OnClick="lnkEmailLink_Click" CommandArgument="jcoulson" CommandName="SetSessionVariable" OnCommand="SetSessionVariable" />
    </div>
    </form>
</body>
</html>

Open in new window

0
 
GiftsonDJohnCommented:
CommandArgument is object DataType.

Use

Session["emailUser"] = Server.HtmlEncode(e.CommandArgument.ToString());

0
 
mrcoulsonAuthor Commented:
BINGO!

Thanks a ton, guys!!
0
 
MrJesse34Commented:
The way to go is to cast the "sender" to a linkbutton.

LinkButton lb = (LinkButton)sender;
sender.ID will work now.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 5
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now