Page view counter

calling function on button click

Last post 01-07-2009 7:53 AM by NC01. 4 replies.

Sort Posts:

  • calling function on button click

    01-07-2009, 4:22 AM
    • Loading...
    • svibuk
    • Joined on 10-16-2008, 10:22 AM
    • Posts 339
    • Points 24

    i have a aspx page with serverside function declared as public

    i need to call this function on page2.aspx on a button click

    hwo do i achieve it.

    i hope this is the right forum

  • Re: calling function on button click

    01-07-2009, 4:51 AM
    • Loading...
    • SATISD9X
    • Joined on 04-17-2007, 2:14 PM
    • Mumbai, India
    • Posts 320
    • Points 1,850

    ' VB code

    ' from a.aspx.vb
    Namespace Web1
        Class WebForm1
            Inherits System.Web.UI.Page
            Public Sub AddSub()
                ' your codes
            End Sub
        End Class
    End NameSpace

    ' from b.aspx.vb
    Namespace Web1
        Class WebForm2
           Private theVar as WebForm1 = New WebForm1()
            Private Sub CallTheFunction()
                theVar.AddSub()
            End Sub

        End Class
    End NameSpace

    // c# code

    // from a.aspx.cs
    namespace Web1 {
        class WebForm1 : System.Web.UI.Page {
            public void AddSub() { // your codes }
        }
    }

    // from b.aspx.cs
    namespace Web1 {
        class WebForm2 {
           private WebForm1 theVar = new WebForm1();
            private void callTheFunction() {
                theVar.AddSub();
            }

        }
    }

     

    //Check this out - do it on the button click

    CSR
    ~ Please Mark as Answer if it solves your query ~
  • Re: calling function on button click

    01-07-2009, 5:43 AM
    • Loading...
    • deesh1531982
    • Joined on 12-24-2007, 1:15 PM
    • Posts 753
    • Points 4,432

    create your general functions under app_code folder so that you can access it anywhere..

    "Never underestimate the power of stupid people in large groups"
  • Re: calling function on button click

    01-07-2009, 6:10 AM
    • Loading...
    • pvkirankumar
    • Joined on 09-10-2008, 5:04 AM
    • Hyderabad
    • Posts 95
    • Points 417

    Hi Svibuk,

    Have the method in a seperate class,

    Instantiate the class and call the method using the instantiated object ....

    Hope this helps you

     

    Regards

    Kiran

     

     

     

    Regards,
    Kiran Kumar

    An ounce of practice is worth more than tons of preaching
  • Re: calling function on button click

    01-07-2009, 7:53 AM
    Answer
    • Loading...
    • NC01
    • Joined on 08-26-2005, 3:33 PM
    • Posts 13,275
    • Points 71,391
    • TrustedFriends-MVPs

    I see nothing posted so far that will actually work, at least not without a lot of effort, so try this:

    Page1.aspx.cs:

    protected void Page_Load(object sender, EventArgs e)
    {
        // Insure that the __doPostBack() JavaScript method is created...
        this.ClientScript.GetPostBackEventReference(this, string.Empty);

        if (this.IsPostBack)
        {
      string eventArgument = (this.Request["__EVENTARGUMENT"] == null) ? string.Empty : this.Request["__EVENTARGUMENT"];

      if ( eventArgument == "CallServersideFunction" )
      {
       yourFunction();
      }
        }
    }

    protected void yourFunction()
    {
     this.Response.Write("yourFunction called<br>");
    }

    Page2.aspx:

    <form id="Form1" method="post" runat="server">
     <input type="button" onclick="callServersideFunction();" value="Call Server-side Function">
    </form>

    <script type="text/javascript">
    <!--
    function callServersideFunction()
    {
     window.opener.<%= ClientScript.GetPostBackEventReference(this, "CallServersideFunction") %>;
    }
    // -->
    </script>

    NC...

Page 1 of 1 (5 items)