runat server on codebehind

F

Ferryandi

hi, i have created a button link like this and attach it
to placeholder in code behind

linkbutton1 = New LinkButton
linkbutton1.Text = "Link Button
linkbutton1.ID = "Link1"
PlaceHolderLink.Controls.Add(linkbutton1)

but how do i add runat="server" as in html code in code
behind, im using vb.net
thax
 
J

Juan T. Llibre

If it's in the code-behind, it automatically runs at the server.

Instancing any server control in
code-behind makes it run server-side.
 
P

Peter Rilling

Runat is only used by ASP.NET to indicate static tags that will be mapped to
variables in the code-behind. If you dynamically create those controls,
then you have to initialize the controls on each postback. You have to do
this manually when the form initializes.
 
D

Derek Harmon

linkbutton1 = New LinkButton
: :
PlaceHolderLink.Controls.Add(linkbutton1)

but how do i add runat="server" as in html code in code behind

You don't need to add runat="server" to a dynamically-created control. LinkButton
extends System.Web.UI.Control, so it's already understood by ASP.NET as being
a server-side control (as opposed to client-side controls like <INPUT type="submit">
buttons that have no existence on the server.)

runat="server" is something written in the .ASPX file, if you had created this LinkButton
statically in that file (in what perhaps is confusingly called the "HTML View" although it
is not) you would've written,

<asp:LinkButton ID="Link1" Text="Link Button" runat="server"></asp:LinkButton>

Go and do that and then run the web application in your browser, and do a View Source.
You should never see runat="server" in the HTML.


Derek Harmon
 

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

Forum statistics

Threads
474,263
Messages
2,571,062
Members
48,769
Latest member
Clifft

Latest Threads

Top