Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: <asp:button How do I make an OnClick Work?


Message #1 by marl@s... on Sun, 23 Feb 2003 19:02:16
Hi:

I'm trying to perform what I thought would be a very simple task.

I want the user to click a button, pass 2 arguments to an event handler 
and have it do a job:

<asp:button onclick="Buy(TickerID, CurrentPrice)" Text="Buy">

-- server side .cs file --

protected void Buy(int nTickerID, Decimal nCurPrice)
{
    . . . buy the dang stock . . . 

}

Of course this doesn't work at all.
How am I supposed to do this in ASP.NET?
Message #2 by "Mike Amundsen" <mike@a...> on Sun, 23 Feb 2003 15:18:17 -0500
Actually, it's a lot easier in ASP.NET than you'd first expect:

First, any variable (including controls on the page) already existing in 
the
server-side code upon postback.  So, given a page that looks link this:
*************************************************************
    <form runat=3D"server">
        <p>
            TickerID:
            <asp:TextBox id=3D"txTickerID" runat=3D"server"/>
            <br />
            Current Price:
            <asp:TextBox id=3D"txPrice" runat=3D"server"/>
        </p>
        <p>
            <asp:Button id=3D"btnBuyStock"
              onclick=3D"btnBuyStock_Click"
              runat=3D"server"
              Text=3D"Buy Stock"/>
        </p>
        <p>
            <asp:Button id=3D"btnBuyBlind"
               onclick=3D"btnBuyBlind_Click"
               runat=3D"server"
               Text=3D"Buy Blind"/>
        </p>
        <p>
            <asp:Label id=3D"lbResults" runat=3D"server"/>
        </p>
    </form>
**************************************

you can write code that looks like this:
****************************************************
    // used for blind purchases
    int BlindTickerID=3D1;
    decimal BlindPrice=3D29;
   
    void btnBuyStock_Click(Object sender, EventArgs e) {
   
        // buy based on inputs from user
        lbResults.Text =3D
            Buy(Int32.Parse(txTickerID.Text),
            Decimal.Parse(txPrice.Text));
    }
   
    void btnBuyBlind_Click(Object sender, EventArgs e) {
        // buy using local vars
        lbResults.Text =3D Buy(BlindTickerID,BlindPrice);
    }
   
    string Buy(int nTickerID, decimal nCurPrice)
    {
      return String.Format(
	  "stock {0} was purchased at {1} as requested.",
        nTickerID,nCurPrice);
    }
**************************************

notice that there is no need to pass the variables in the click event. 
They
are already there for you to use.

Hope this helps.

MCA

> -----Original Message-----
> From: marl@s... [mailto:marl@s...]
> Sent: Sunday, February 23, 2003 7:02 PM
> To: ASP.NET
> Subject: [aspx] <asp:button How do I make an OnClick Work?
>
> Hi:
>
> I'm trying to perform what I thought would be a very simple task.
>
> I want the user to click a button, pass 2 arguments to an event 
handler
> and have it do a job:
>
> <asp:button onclick=3D"Buy(TickerID, CurrentPrice)" Text=3D"Buy">
>
> -- server side .cs file --
>
> protected void Buy(int nTickerID, Decimal nCurPrice)
> {
>     . . . buy the dang stock . . .
>
> }
>
> Of course this doesn't work at all.
> How am I supposed to do this in ASP.NET?


  Return to Index