0

I have a repeater listing out my custom data, now what I want to do is pass on a variable from one page to another using a asp:linkbutton. The problem I am having is that when I go to call my link button in the subroutine its not being recognized. I looked at Can't find a link button in a repeater but I do not think it will work in this case. Here is my code:

    <asp:Repeater ID="rptList" runat="server" EnableViewState="false" DataSourceID="SqlDataSource3">
<ItemTemplate>
    <div>
    <p>
    <b>Title: </b> <asp:LinkButton ID="lbnCookieVar" OnClick="lbnCookieVar_Click" Text='<%# Eval("Job_Title")%>' runat="server" /> <br />
    <b>Status: </b><%# Eval("Status")%><br />
    <b>Department: </b><%# Eval("Department")%><br />
    <b>Date Position Available: </b><%# Eval("Date_Position_Available")%><br />
    </p>
    </div>
</ItemTemplate>
</asp:Repeater>

here is the code behind

     protected void lbnCookieVar_Click(object sender, EventArgs e)
{
    Session["Data"] = lbnCookieVar.Text;
    Response.Redirect("Application.aspx");
}
Community
  • 1
  • 1

6 Answers6

2

You will need to access the linkbutton via the "sender" argument of the function:

protected void lbnCookieVar_Click(object sender, EventArgs e)
{
    Session["Data"] = (sender as LinkButton).Text;
    Response.Redirect("Application.aspx");
}
mreyeros
  • 4,359
  • 20
  • 24
1

try this, should work:

Session["Data"] = (sender as LinkButton).Text;

see here for the full story: Change text of a linkbutton in a repeater

Community
  • 1
  • 1
Davide Piras
  • 43,984
  • 10
  • 98
  • 147
1

Ugly and you are making this way more difficult than you need to.

Why aren't you using the Repeater's ItemCommand routine to pick this up? Then you just pass the CommandArgument of what you want to do.

<asp:Repeater ID="rptList" runat="server" EnableViewState="false" 
    DataSourceID="SqlDataSource3" OnItemCommand="rptList_ItemCommand">
<ItemTemplate>
    <div>
    <p>
    <b>Title: </b> <asp:LinkButton ID="lbnCookieVar" CommandText="click" CommandArgument='<%# Eval("Job_Title")%>' Text='<%# Eval("Job_Title")%>' runat="server" /> <br />
    <b>Status: </b><%# Eval("Status")%><br />
    <b>Department: </b><%# Eval("Department")%><br />
    <b>Date Position Available: </b><%# Eval("Date_Position_Available")%><br />
    </p>
    </div>
</ItemTemplate>
</asp:Repeater>

Code Behind:

I don't know who changed my code but you are mistaken. If you do this exactly as it is this is the way you do it. I had mistakenly put single quotes around "click" when it should have been double quotes.

protected void rptList_ItemCommand(object sender, CommandEventArgs e)
{
   if (e.CommandName == "click")
   {
       Session["Data"] = e.CommandArgument.ToString();
       Response.Redirect("Application.aspx");
   }
}
Steve
  • 399
  • 2
  • 7
0

Working with controls inside of a repeater/listview is usually gets a little weird. I think in your case you can just do the following:

Session["Data"] = ((LinkButton)sender).Text;
Abe Miessler
  • 82,532
  • 99
  • 305
  • 486
  • I tried that but the stupid browser wont redirect to application.aspx like its supposed to for me to test it. –  Aug 07 '12 at 17:24
0
SessionStorage.LinkButtonText = Session["Data"] = ((LinkButton)sender).Text;

public class SessionStorage
{
    public Static String LinkButtonText
    {
        get
        {
            if (System.Web.HttpContext.Current.Session["LinkButtonText"] == null)
                return String.Empty;
            return Convert.ToString(
                       System.Web.HttpContext.Current.Session["LinkButtonText"]);
        }
        set
        {
            System.Web.HttpContext.Current.Session["LinkButtonText"] = value;
        }
    }
}
0

Hi all this is how I fixed it!

<asp:Repeater ID="rptList" runat="server" EnableViewState="false" DataSourceID="SqlDataSource3">
<ItemTemplate>
    <div>
    <p>
    <b>Title: </b> <a href="CareerSummary.aspx?Job_Title=<%# Eval("Job_Title") %>&num=<%# Eval("num") %>"><%# Eval("Job_Title") %></a><br />
    <b>Status: </b><%# Eval("Status")%><br />
    <b>Department: </b><%# Eval("Department")%><br />
    <b>Date Position Available: </b><%# Eval("Date_Position_Available")%><br />
    </p>
    </div>
</ItemTemplate>
</asp:Repeater>

Code behind of page you want to get sent variable:

protected void Page_Load(object sender, EventArgs e)
{
    if (!Page.IsPostBack)
    {

        lblRow.Text = Request.QueryString["num"];
    }


}

It has to be in the load for this method to work