Click here to Skip to main content
15,892,298 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I'm creating a button on a Sharepoint page dynamically (in C#) in my *.ascx.cs file:

Button btnSave = null;

protected void Page_Load(object sender, EventArgs e)
{
    base.OnPreRender(e);

    this.Controls.Add(new LiteralControl("<br />"));
    btnSave = new Button();
    btnSave.ID = "btnSave";
    btnSave.Text = "Save the Data";
    btnSave.Click += new EventHandler(btnSave_Click);
    btnSave.Visible = false;
    this.Controls.Add(btnSave);
}

protected void btnSave_Click(object sender, EventArgs e)
{
    btnSave.Text = "You clicked me!";
    PostTravelData ptd = new PostTravelData();
}


I set it visible at the righ time in the client-side jQuery in the *.ascx file:

$('#btnSave').show();


However, clicking the button does not reach the btnSave_Click() event - the breakpoint there is never reached, nor is the button's text changed. Why not?
Posted
Comments
ZurdoDev 15-Sep-15 22:23pm    
If you set the button's Visible = false in C# then it won't be in the html so the jquery to show it will not work. I think you must be seeing a different button.
Sergey Alexandrovich Kryukov 16-Sep-15 1:10am    
This is a right answer — input events on hidden controls are never invoked (isn't it logical?). Will you put a formal answer, to close this issue?
—SA
B. Clay Shannon 16-Sep-15 10:56am    
It is the button I expect to see, based on its text. So: is there a way to hide the button until I want it visible, and still have it work?
B. Clay Shannon 16-Sep-15 14:40pm    
I made the button visible from the git-go, and the click handler still is not reached. Is creating teh control in Page_Load too late?
Sinisa Hajnal 17-Sep-15 3:19am    
Why don't you put it on the page immediately as page element, don't create it dynamically and then when you get it to work, play with handlers.

There are two ways to resolve this. One is to create it in page load and immediately after adding it to the page, add click handler to it.

The other way is to use jQuery.on method to bind it when it shows on the page and use javascript function and ajax to send data to the server.

1 solution

Lets see:
this is the way of the server side
ASP.NET
<asp:button id="btnSave" runat="server" text="Click me" xmlns:asp="#unknown" />


On the server (or you can do it in html directly)
C#
btnSave.OnClick += btnSave_Click;



This is incorrect if you're trying to show/hide server-side control:
JavaScript
$('#btnSave').show(); 


This is due to server side naming convention that adds all containers names in front of the id like ctl_00_ctl_01_btnSave. It should be:
JavaScript
$('#<%=btnSave.ClientID%>').show(); 

where <%= %> marks document.write from server side

jQuery.on version:
http://api.jquery.com/on/[^]
JavaScript
$('document').on('click', '#<%=btnSave.ClientID%>') {
    function () {
// here goes your ajax call to a web method [WebMethod] public void btnSave_Click
    }
});



I hope this helps. Good luck.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900