runat server on codebehind

  • Thread starter Thread starter Ferryandi
  • Start date Start date
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
 
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.



Juan T. Llibre
ASP.NET MVP
http://asp.net.do/foros/
Foros de ASP.NET en Español
Ven, y hablemos de ASP.NET...
======================
 
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.
 
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
 
Back
Top