0

I have an asp button, and I want to send parameter to codebehind when it's click. so I do like this tutorial: http://www.devchix.com/2007/08/10/aspnet-passing-parameters-in-button-click-handler/

I have button:

            <asp:Button ID="button1" runat="server" CommandArgument="MyVal1"
   CommandName="ThisBtnClick" OnClick="MyBtnHandler" />

and event :

  void MyBtnHandler(Object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            switch (btn.CommandName)
            {
                case "ThisBtnClick":
                   //lala
                    break;
            }

but when I run , there is a ' Compilation Error'.

Compiler Error Message: CS1061: 'ASP.softwares_aspx' does not contain a definition for 'MyBtnHandler' and no extension method 'MyBtnHandler' accepting a first argument of type 'ASP.softwares_aspx' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 110: Line 111: Line 112:
Line 114:

why?

thanks!!

1
  • 3
    You need to make it either public or protected for it to be used as part of the OnClick handler. Functions, as standard, are private if they are not specifically stated Commented Mar 7, 2014 at 18:30

1 Answer 1

2

As per my comment - if you don't give permission to a function it becomes private as standard.

If you give it either public or protected, then the function can be seen by the OnClick handler.

For preference, I would go with protected, meaning the click handler will see it, but it won't become an available function to other classes. (Unless they're derived, which is a whole other subject)

So change...

void MyBtnHandler(Object sender, EventArgs e)

To...

protected void MyBtnHandler(Object sender, EventArgs e)
Sign up to request clarification or add additional context in comments.

5 Comments

thanks. I have another little problem..I try to transfer as parameter a value from C# object called s: CommandArgument="<%=s.SoftwareID%>" but it's transfer that like a String of : <%=s.SoftwareID%> instead of send the id. why?
This is a common mistake. Any server-side control (basically anything with runat="server" cannot have code blocks within attributes (the major exception to this is when you use <%# %> in repeaters). What you need to do is set the attribute in the code-behind
I understand..it's problem for me to do that on code behind. there is option to send a paramter with html button and OnServerClick ?
No, I don't think so @user, because in order for you to use OnServerClick you have to have runat="server" which then turns it into a server-side control
If my answer has answered your original question @user, please consider marking it as the accepted answer. This will tell the community that your problem is solved, and helps future readers who find your question

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.