Linkbutton in User Control Not Working

A

Anthony Hunter

Hello,

I am loading a user control into a panel on my default page "Default.aspx" :

void LoadControlData()
{
this.pnlCONTENT.Controls.Add(LoadControl("Default_Content.ascx"));
}

The user control "Default_Content.ascx" has a linkbutton that is tied to a
click event:

void lnkMainPage_Click(object sender, EventArgs e)
{
Response.Redirect(User_Info.Application_Path + "\\Main.aspx");
}

When I click the link button, the event never gets executed (tested in CLR
Debugger). I am only redirected back to default.aspx with nothing on the
page. Is there something I may be missing?

Please help... Thanks in advance,

Tony
 
K

Ken Cox [Microsoft MVP]

Hi Tony,

You may already have checked, but be sure there's an event handler for your
linkbutton. Here's how mine user control looked:

namespace p4320workcs
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Default_Content.
/// </summary>
public class Default_Content : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.LinkButton lnkMainPage;

private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.lnkMainPage.Click += new
System.EventHandler(this.lnkMainPage_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void lnkMainPage_Click(object sender, System.EventArgs e)
{
Response.Redirect("Main.aspx");
}
}
}

Ken
Microsoft MVP [ASP.NET]
 
R

Robert Koritnik

Are you creating that LinkButton staticly inside the ASPX page or dynamicly
inside the CS/VB file (is any part dynamic about this linkbutton)? If the
second one is in question, the answer is clear: You are recreating that link
button too late or with different state.

If the first one is the question, I would check the RUNAT=SERVER attribute
and subscription to event statement inside your code.
 
A

Anthony Hunter

The link button is added as a static control on the ascx web form. Here is
the html:

<asp:LinkButton id="lnkMainPage" onclick="lnkMainPage_Click"
Font-Underline="True" Runat="server" CssClass="clsASPLink">Main
Page</asp:LinkButton>
 
A

Anthony Hunter

This appears to be a big problem.

I finally decided to add my user control to the page by registering it and
adding an html tag for it. Doing it this way, the link button works fine.
I then decided to add the user control programmatically using a placeholder
and loadcontrol(), this too will add the control to the page; but I have the
same problem as before when I added the user control to a panel...the link
does not work. I am using web matrix 0.6.812.0 as my development tool -
what is up with this?

Any help will be greatly appreciated.

Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,264
Messages
2,571,065
Members
48,770
Latest member
ElysaD

Latest Threads

Top