none
Calling OnClick event with parameters

    Question

  • All sorry if this is not the correct place but...

    I can't believe that I can't do this so can you please help me out.  I want to call an event from the OnClick event of a control.  Sounds simple right!  Well I can't for the life of me figure it out.

    I want:

    <asp:LinkButton ID="lnk1998Tot" runat="server" Text="1998 YTD" OnClick="SetGridContext("1998", "Pledge")"></asp:LinkButton>

    to call:

            private void SetGridContext( string year, string type )  
            {  
                Session["Year"] = year;  
                Session["Type"] = type;  
                Session["Donor"] = txtDonorID.Text;  
                Response.Redirect( "~/TransDetail.aspx" );  
            } 


    the only way I can get it to work is with:
    OnClick="lnk1998Tot_Click"

    then

            protected void lnk1998Tot_Click( object sender, EventArgs e )  
            {  
                SetGridContext( "1998", "Pledge" );  
            } 

    Please say I am missing something. :)

    Sunday, October 05, 2008 6:56 PM

Answers

  • Hi John,

    What you are trying to do, the way you are trying to do simply won't work. On the OnClick you are defining a delegate by saying that the OnClick event will be handled by a method that you define. However, you can only give it a method name, you can't give it the whole parameters you want to pass as that is reserved by C#. The parameters will be an object (the sender) and Event Arguments related to the event.

    So in a nutshell, yeah, you need the lnk1998Tot_Click method (which is a proper event handler for OnClick) and only in it you can call the SetGridContext().

    Do note, LinkButtons have a CommandArgument that you can use when you want to pass a value to the event "Command" but here you can only pass a simple string I believe. (Check lnk1998.CommandArgument)

    Tiago

    Microsoft Student Partner '09 - Portugal www.tiagoespinha.net
    Sunday, October 05, 2008 7:18 PM