Nico
Nico

Reputation: 237

Button.OnClientClick question

I'm gonna post some more code to show exactly what I'm trying to do, I'm adding the button using programming code and not markup but the OnClick won't work (giving the following error:

System.Web.UI.WebControls.Button.OnClick(System.EventArgs)' is inaccessible due to its protection level.

Button btnopslaan = new Button();
btnopslaan.Text = "Opslaan";
btnopslaan.ID = "btnOpslaan";
btnopslaan.CssClass = ".opslaan";
btnopslaan.Click += new EventHandler(btnopslaanClick);
btnopslaan_arr[btn_count] = btnopslaan;
add_button(btnopslaan);

protected void btnopslaanClick(object sender, EventArgs e)
{
   Debug.WriteLine("success");
}

I just can't find out why this isn't working.

Anyone who can help me out?

Upvotes: 1

Views: 2749

Answers (4)

Caspar Kleijne
Caspar Kleijne

Reputation: 21864

or you make it a postback call to the server. in your

aspx write:

<asp:Button runat="server" ID="buttonOpslaan" Text="opslaan" ></asp:Button>

codebehind write this:

protected void Page_Init(object sender, EventArgs e)
{
    buttonOpslaan.Click += new EventHandler(buttonOpslaan_Click);
}

// mind: this method can be private
void buttonOpslaan_Click(object sender, EventArgs e)
{
    //do something
}

or handle it with the AutoEventWireUp (recommended) like:

  <asp:Button runat="server" 
    ID="buttonOpslaan" 
    OnClick="buttonOpslaan_Click" 
    Text="opslaan" ></asp:Button>

// mind: this method cannot be private, but has to be protected at least.
protected void buttonOpslaan_Click(object sender, EventArgs e)
{
    //do something
}

or do it completely from code behind:

// note: buttonOpslaan must have an (autoassigned) ID.
protected void Page_Init(object sender, EventArgs e)
{
    Button buttonOpslaan = new Button();
    buttonOpslaan.Text = "opslaan!";
    buttonOpslaan.Click += new EventHandler(buttonOpslaan_Click);
    form1.Controls.Add(buttonOpslaan);
}

protected void buttonOpslaan_Click(object sender, EventArgs e)
{
    //do something
}

or handle it clientside with javascript in your ASPX (it will not reach the server)

<script type="text/javascript">
    function buttonOpslaan_Click(){
        alert("test");
        return false;
    }
</script>
<asp:Button runat="server" 
    ID="buttonOpslaan" 
    OnClientClick="buttonOpslaan_Click()"        
    Text="opslaan" ></asp:Button>

Update: (by comments)

if you add the control via an eventhandler (like the onchange event of a dropdownlist), the control is 'lost' on next postback, or even as soon as the Page is send to the client (due to the stateless (there is no mechanism to maintain the state of application) behaviour and lifecycle of .Net).

So simply adding a control once is never going to work.

That means you have to rebuild the control every time a postback occurs. My preferred way to do this is store a list/document somewhere that descrbes what controls must be created each time. Possible locations are, from worse to good (IMHO):

  • Session
  • Viewstate
  • Cache
  • XML/IO
  • Database

After all, you are posting "data" to the server (that represents a control) and you want to save that for further use.

If the controls to be created aren't that complex you could implement a Factory Pattern like a WebControlFactory that stores only a few properties in a List or Dictionary, which is read every time to recreate the controls again (and again, and again, and again)

Upvotes: 2

neeebzz
neeebzz

Reputation: 11538

You need to use OnClick for server side clicks rather than OnClientClick

Either you can use it inline >

<asp:Button id="btnopslaan" runat="server' OnClick="btnopslaanClick" />

Or in Code behind >

btnopslaan.Click+=new EventHandler(btnopslaanClick);

Upvotes: 4

Govind Malviya
Govind Malviya

Reputation: 13743

btnopslaan.Click+=new EventHandler(btnopslaanClick);

protected void btnopslaanClick(object sender, EventArgs e)
{

    Debug.WriteLine("succes");

}

Upvotes: 0

Shadow Wizard
Shadow Wizard

Reputation: 66389

btnopslaanClick should be client side, in the .aspx itself have:

<script type="text/javascript">
function btnopslaanClick() {
   alert("success");
}
</script>

Upvotes: 0

Related Questions