How to call a function on run time button click

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sachin2320
    New Member
    • Mar 2010
    • 7

    How to call a function on run time button click

    How to call a function on run time button click.

    As I have created a table and a button on run time and want to call a function on its click on
    code behind page(.cs page).

    It is in Asp.net with c#

    my code full details is like
    Code:
    strhtml = strhtml.append(<button id=btnclick, runat='server',onclick=testfunction''></button>);
    2) my function is on same page like
    Code:
    testfunct()
    {
    xyz;
    }
    now i want to call this function on click of button
  • semomaniz
    Recognized Expert New Member
    • Oct 2007
    • 210

    #2
    Code:
      Button btn = new Button();
      btn.ID = "btntest";
      btn.Click += new EventHandler(btn_Click);

    Comment

    • semomaniz
      Recognized Expert New Member
      • Oct 2007
      • 210

      #3
      When are you creating the control ? On page load or a a click event ?

      Comment

      • sachin2320
        New Member
        • Mar 2010
        • 7

        #4
        on page load,
        but have to call function on button click which is creating at run time

        Comment

        • semomaniz
          Recognized Expert New Member
          • Oct 2007
          • 210

          #5
          Why dont you create a button as i mentioned before?

          Comment

          • sachin2320
            New Member
            • Mar 2010
            • 7

            #6
            ok I wil give it try and
            get u touch soon

            thanks

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              When developing in ASP.NET it is a good idea to use ASP.NET controls instead of HTML controls. The reason is because you will be able to access ASP.NET controls much more easily in your C#/VB.NET server-side code that you will be able to access HTML controls.


              A good place to learn about any .NET controls (ASP.NET included) is the MSDN Library. I suggest that you go through your Toolbox to discover what ASP.NET controls are available to you before you jump straight to the MSDN Library or else you won't know what to search for.

              Happy Coding,

              -Frinny

              Comment

              • sachin2320
                New Member
                • Mar 2010
                • 7

                #8
                hi

                its ok to use asp.net controls instead of html
                but what if we have to create report at run time.thats why we used html control
                and dint find solution
                how to call function which is on same page
                ( code behind page)
                problem is that Im not getting click event of this button (html button )on this
                page ( code behind page )


                thanks

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  Sachin2320,

                  It would be best if you described exactly what you are trying to do because right now I have no idea what's going on. No where did you mention a report...and no where did you mention why you need to dynamically create this button.

                  I still think that you should use an ASP.NET Button control because then you can handle it's OnClick event (server-side)...which is exactly what you are looking to do.

                  There is no such thing as a server-side OnClick event for an HTML button. I believe if you use the Request object you may be able to determine if the button was the thing that submitted the page...but this is overly complicated especially since the ASP.NET Button already does this for you (and creates the server-side OnClick event which you can handle).

                  It is a LOT easier to use an ASP.NET Button in an ASP.NET application than it is to use an HTML button....espec ially when you want to do something in your server code in response to clicking the button. All you have to do is Drag the Button from the Toolbox onto your ASPX page (not your C# code...your page design)...and then implement a method that handles the OnClick event for this button.

                  ASP.NET Buttons have a property called "Visible". If you set this property to False then this button will not be rendered as HTML and so it will not appear in the browser.

                  This means that you can have a button declared in your ASP.NET page, set it to Visible and it won't show up in the browser.

                  You can set the Button's Visible property to True and the button will be visible...you should only set this property to true when you need the button.

                  Dynamically creating ASP.NET buttons is not trivial and requires some thinking so this is why I'm recommending that you use the Visible property.

                  Now, I'm guessing that you only want to use 1 button on your page...if your report has multiple buttons in it (ie: a column of buttons) then you have to take a completely different approach and you should be using something like a GridView or a Repeater control...which will automatically, dynamically create buttons for you (with unique IDs and everything)...y ou can handle the dynamic button click in your server-code.

                  I cannot help you any further because you haven't shared with us what you are trying to do, why you need the button, where the button exists, or how many buttons need to be created.

                  Once you describe your project and describe why you need the button (and where it should be used) then we can help you more.

                  -Frinny

                  Comment

                  Working...