Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8186059
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T02:04:02+00:00 2026-06-07T02:04:02+00:00

In the Page_Load method I create a couple of controls, based on various conditions.

  • 0

In the Page_Load method I create a couple of controls, based on various conditions. I would like to register server side code with those controls. However, for the last part I need to declare my controls as server controls. This is normally done by runat=server, but I don’t know how to set this attribute in the C# code. myControl.Attributes.Add(“runat”, “server”) does not do the trick. This one works, meaning that the “test” method is called when I click on it:

<asp:LinkButton ID="LinkButton1" runat="server" OnClick="test">testtext</asp:LinkButton>

This one does not work:

            LinkButton lb = new LinkButton();
            lb.ID = "LinkButton1";
            lb.OnClientClick = "test";
            lb.Text = "testtext";
            lb.Attributes.Add("runat", "server");

I can click on it, and the page is loaded, but the test-method is not called.

Any hints?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T02:04:03+00:00Added an answer on June 7, 2026 at 2:04 am

    You almost got it right. Just a couple things:

    • When you manually instantiate a server control, there’s no need to add the runat="server" attribute. This is a special attribute that’s used only by the ASP.NET page parser to distinguish server controls from other markup.
    • The OnClick attribute in markup corresponds to the Click server-side event, which you hook up using the += operator. (On the other hand, the OnClientClick attribute in markup corresponds to the onclick client-side attribute, which typically contains a snippet of JavaScript code. The fact that OnClick doesn’t correspond to onclick is admittedly a bit confusing.)

    Thus:

    LinkButton lb = new LinkButton();
    lb.ID = "LinkButton1";
    lb.Click += test;
    lb.Text = "testtext";
    

    And your event handler (which you can even make private if there are no references to it from markup):

    protected void test(object sender, EventArgs e)
    {
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have code in my Page_load method that can take up to a few
I am able to create controls pro grammatically using the code below without issue:
I am needing to create some dynamic controls at Page_Load in the consumer webpart.
Why I can access Session object in Page_Load method in instance of System.Web.UI.Page, but
Previously, I was passing information to a Silverlight control inside of the Page_Load method.
I'm trying to create a method that will send a PDF file directly to
I understand that you can now create MVC-specific user controls, but will my existing
How can create a unit test for the the Page_Load function in ASP.net? I
tl;dr summary : jQuery's load() method called like: $('#foo').load('similar.html #foo') results in DOM structure
Please see the code here: http://pastie.org/1092106 When I call the method createPost on Blog,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.