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
    • Member
      153 point Member
    • svibuk
    • Member since 10-16-2008, 10:22 AM
    • Posts 635

    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
    • Contributor
      2,120 point Contributor
    • SATISD9X
    • Member since 04-17-2007, 2:14 PM
    • Mumbai, India
    • Posts 355

    ' 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

    Satish Chilkury
    MCTS .Net Framework 2.0 Web Applications


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

    01-07-2009, 5:43 AM
    • Contributor
      4,508 point Contributor
    • deesh1531982
    • Member since 12-24-2007, 1:15 PM
    • Posts 766

    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
    • Member
      417 point Member
    • pvkirankumar
    • Member since 09-10-2008, 5:04 AM
    • Hyderabad
    • Posts 95

    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
    • All-Star
      75,037 point All-Star
    • NC01
    • Member since 08-26-2005, 3:33 PM
    • Posts 13,952
    • 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)