Search code examples
.netuser-controlslinkbutton

.Net Add LinkButton to a Placeholder in User Control


I want to create a common header user control for various master pages in a web application. The header is shared by masters and has common items such as the logo, navigation, and dynamic account buttons such as My Account and Sign Out.

I am adding dynamic link buttons to the form just fine, but I then want to move them to the Placeholder position.

The problem is that the user control gets the error, "'LinkButton' must be placed inside a form tag with runat=server" when I add it to a placeholder.

uc_header.ascx.cs

LinkButton signout = new LinkButton();
signout.Text = "Sign-Out";
signout.ID = "SignOutLink";
signout.Click += new EventHandler(SignOutLink_Click);
//adding it to the form works but positions it at the bottom
this.Page.Form.Controls.Add(signout);
//I get an error when I try to add it to the placeholder 
SignOutHolder.Controls.Add(signout);

uc_header.ascx (simplified for example)

<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="uc_header.ascx.cs" 
Inherits="Site.Masters.uc_header" %>

<asp:PlaceHolder ID="SignOutHolder" runat="server"></asp:PlaceHolder>

How can I add or position the Sign-Out button to the placeholder using .Net?

My goal is to create a way to log out on all the pages and manage only one common header file.


Solution

  • It sounds like the user control is placed outside of the form. As the error message indicated, check the master page and make sure that the user control is inside the form.