In my Sharepoint 2010 app, I'm handling most events client-side with jQuery. However, for the saving of data to the Sharepoint list, and the generation of a PDF file with that data, I want to handle that server-side, with C#.
I tried to invoke a C# event this way:
0) Aded an HTML button in my project's *.ascx file:
<button type="button" name="saveData" id="saveData">Save Data</button>
1) Added jQuery to respond to that button being clicked (in the same *.ascx file):
$("#saveData").click(function () {
$('#hiddenSave').trigger('valuechanged');
});
2) Created a hidden element in the server-side C# (in the *.ascx.cs file):
HiddenField hiddenSave = null;
protected void Page_Load(object sender, EventArgs e)
{
base.OnPreRender(e);
hiddenSave = new HiddenField();
hiddenSave.ID = "hiddenSave";
hiddenSave.ValueChanged += new EventHandler(hiddenSave_ValueChanged);
this.Controls.Add(hiddenSave);
}
protected void hiddenSave_ValueChanged(object sender, EventArgs e)
{
GeneratePDF();
}
private void GeneratePDF()
{
;//bla
}
But I never reach the "ValueChanged" event handler; $("#saveData").click() fires, but not hiddenSave_ValueChanged().
So do I need a tweak to this, or a completely different approach? How can I do as much as possible client-side with jQuery, but also run server-side/C# code where necessary, in a Sharepoint 2010 app?
UPDATE
A little more detail, and about additional things I've tried: 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 right 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?
Even when I don't set the button invisible (comment out the "btnSave.Visible = false;" line), the click handler isn't reached...is Page_Load() too late? Is there an earlier page event I can use that would work?
I tried moving it from Page_Load() to OnPreRender(), too, like this:
protected void OnPreRender(EventArgs 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);
}
...(and OnRender()) but the button doesn't even display...
And, trying a different tack, I commented out the dynamic creation server-side code and tried to attach to a button created in the HTML (*.ascx file):
<button type="button" name="saveData" id="saveData" runat="server" onclick="saveData_Click">Save Data</button>
(by adding the "runat server" and the onclick handler), and then adding this "code-behind" (*.ascx.cs)):
protected void saveData_Click(object sender, EventArgs e)
{
PostTravelData ptd = new PostTravelData();
SaveToList(ptd);
GeneratePDF(ptd);
}
...but there was still no joy in Mudville -- the breakpoint in the handler is not reached.
Yet another attempt was:
In the *.ascx:
<asp:Button runat="server" id="saveData" name="saveData" onclick="saveData_Click" Text="Bla" />
In the code-behind:
saveData.Click += saveData_Click;
The "Bla" button is created, but clicking on it reaches not the breakpoint in the "saveData_Click" handler.
I even adapted some code from here like so:
Button btnSave = null;
. . .
protected override void CreateChildControls()
{
btnSave = new Button();
btnSave.Width = new Unit(150, UnitType.Pixel);
btnSave.Text = "Can you see me?";
btnSave.Click += new EventHandler(btnSave_Click);
Controls.Add(btnSave);
}
...but I still do not reach the "protected void btnSave_Click(object sender, EventArgs e)" handler when I click the button.
Surely there's a way to get a handle on the button server-side and manipulate it (specifically, respond to its click event)...?!?
First of all, as far as I know, there is no such event for input type hidden in a ascx page. If you create the input type hidden with runat server in the ascx code then you'll see that when you try to add this event it's not available. However there are other events like OnClick that you can simulate to get the desired result.
Related
I have created button manually from the behind code page using asp.net WebForms.
That is the creation code:
Button myButton = new Button();
myButton.ID = "b" + arrcodes[i];
myButton.Text = "Buy Now!";
myButton.CssClass = "btn-primary";
myButton.Click += new EventHandler(myButton_Click);
myPlaceHolder.Controls.Add(myButton);
I googled how to add onClick function and used method which founded here in stackoverflow. But this method not working.. the function "myButton_Click" isnt activated when i press the button.
protected void myButton_Click(object sender, EventArgs e)
{ // my code }
Have i did something wrong?
You will need to reload that dynamically created button on postback with same Id inside Page_Init or Page_Load event.
Otherwise, it won't be in the Control Tree, and cannot trigger the Click event.
For example,
public partial class Default : System.Web.UI.Page
{
protected void Page_Init()
{
CreateButton();
}
protected void myButton_Click(object sender, EventArgs e)
{
}
private void CreateButton()
{
Button myButton = new Button();
myButton.ID = "b1";
myButton.Text = "Buy Now!";
myButton.CssClass = "btn-primary";
myButton.Click += new EventHandler(myButton_Click);
myPlaceHolder.Controls.Add(myButton);
}
}
Usually, the better option is to create the button and toggle it's visibility.
If you really need to create it dynamically, make sure you're creating the button in OnInit method for event handler to work.
I am trying to call dynamically created button click event. I this event I want to show one message on clicking dynamically created button.
my Code
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnMain_Click(object sender, EventArgs e)
{
Button btnNew = new Button();
btnNew.ID = "btnClick";
btnNew.Text = "Click";
btnNew.Click += new System.EventHandler(btnNew_Click);
this.form1.Controls.Add(btnNew);
}
protected void btnNew_Click(object sender, EventArgs e)
{
Label lblMeaaseg = new Label();
lblMeaaseg.ID = "txtMessage";
lblMeaaseg.Text = "Hello Shree";
this.form1.Controls.Add(lblMeaaseg);
}
You create the dynamic button in the click event handler of btnMain during the postback caused by btnMain click. After that you see the new button in the browser page, click it and expect its click event handler (btnNew_Click) to fire. Pressing the new dynamic button causes a new postback that is processed by a new instance of the page created on the server by ASP.NET. This new page does not have the dynamic button - there is nothing there connected to btnNew_Click. You have to write code that persists the fact that the dynamic button has been created and recreates this button every time the page is instantiated. So that this button has a chance to feel and respond to its client-side click.
I have a button that is created after a user selects a certain value from a dropdown menu, but it is not firing its' EventHandler. Is there something with the Lifecycle, OnInit possibly, that I have to refresh for the handler to fire correctly?
Event fired from DropDownList's OnSelectedIndexChanged
protected void Selected_floor_first(object sender, EventArgs e)
{
Button btn = new Button();
btn.ID = "room_button_1";
btn.Text = "Select";
btn.Click += new EventHandler(room_1_Click);
floor_1_room_overlay.Controls.Add(btn);
}
Handler: (Not Firing)
protected void room_1_Click(object sender, EventArgs e)
{
validation.Text = "You selected a Room";
}
If you must create your button dynamically, create it inside the OnInit() method of the page.
Event handling happens after Page Init. So, the button will have to be created before Page Init, for the events to be handled.
As it is dynamically added, you have to take that code in Page_Init() event that occurs after every postback. otherwise when the postback occurs, there is no room_button_1 in the forms.controls collection and the event is missed. So
add it as it is being added.
after adding set a variable in session to identify that dynamic control has been added
on page_init() check the session variable of step2. if it says yes then create the control you created in step 1.
Instead of repeating the code, it's better if you create a function for button creation and call it from your Select_floor_first() and Page_Init().
The button goes out of scope mate. define it as a private variable otherwise the event wont fire as the button disposed after Selected_floor_first method finishes
private Button btn = new Button();
protected void Selected_floor_first(object sender, EventArgs e)
{
btn.ID = "room_button_1";
btn.Text = "Select";
btn.Click += new EventHandler(room_1_Click);
floor_1_room_overlay.Controls.Add(btn);
}
protected void room_1_Click(object sender, EventArgs e)
{
validation.Text = "You selected a Room";
}
HTML
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Button runat="server" ID="show" OnClick="show_Click" Text="show"/>
<asp:Button runat="server" ID="add" OnClick="add_Click" Text="add new "/>
<div id="content" runat="server"></div>
</asp:Content>
code
protected void show_Click(object sender, EventArgs e)
{
Response.Write(((CheckBox) content.FindControl("chb")).Checked);
}
protected void add_Click(object sender, EventArgs e)
{
CheckBox chb = new CheckBox();
chb.ID = "chb";
chb.Text = "chb";
content.Controls.Add(chb);
}
by button add added a new checkbox on runtime.
then i want get checkbox chb by button show
but ((CheckBox) content.FindControl("chb")).Checked return Null.
i want add checkbox dynamically and then checked that which of them checked is true.
This happens because dynamically added controls are not preserved after postbacks. You can easily demonstrate this by adding another button (without a click event handler) to the page. Run the application and click the "add" button to create the checkbox, then click the newly added button and the checkbox will be gone after the postback.
Well, I can not understand what you are trying to achieve but;
protected void show_Click(object sender, EventArgs e)
{
Response.Write((Session["chb"] as CheckBox).Text);
}
protected void add_Click(object sender, EventArgs e)
{
CheckBox chb = new CheckBox();
chb.ID = "chb";
chb.Text = "chb";
content.Controls.Add(chb);
Session["chb"] = chb;
}
Your events don't happen in the same postback of your page - when you click add, it adds the checkbox but then the page execution finishes, the page is sent to the client and it's done with handling that Click event.
When you then click the show button, it's another postback, in which your checkbox has not been created, so it doesn't exist.
To handle this, you have a few options:
1.
Add the checkbox to the page in the designer and set its Visible property to false inially. You can keep the add button, but it won't actually add a checkbox to the page, it'll just make it visible by setting Visible to true.
2.
If you really want to dynamically add the checkbox, then you need to add it every time the page is executed, in one of the page event handlers (for example Load). The way to do that is to save a value in the viewstate or in a hidden field when you click add and based on the value, you'd create the checkbox on subsequent postbacks.
protected void Page_Load (object sender, EventArgs e)
{
if ( IsPostBack )
{
if ( Session["chb"] != null )
CreateChb ();
}
}
protected void show_Click(object sender, EventArgs e)
{
Response.Write(((CheckBox) content.FindControl("chb")).Text);
}
protected void add_Click(object sender, EventArgs e)
{
Session["chk"] = true;
CreateChb ();
}
private void CreateChb ()
{
CheckBox chb = new CheckBox();
chb.ID = "chb";
chb.Text = "chb";
content.Controls.Add(chb);
}
Good morning everybody.
I have a question connected with controls and event handling. Lets say I want to create a LinkButton.
protected void loadLinkButton()
{
ContentPlaceHolder content = (ContentPlaceHolder)this.Master.FindControl("MainContent");
LinkButton lnk = new LinkButton();
lnk.ID = "lnikBtn";
lnk.Text = "LinkButton";
lnk.Click += new System.EventHandler(lnk_Click);
content.Controls.Add(lnk);
}
Here is the event handler:
protected void lnk_Click(object sender, EventArgs e)
{
Label1.Text = "ok!";
}
If I run the loadLinkButton function inside Page_Load everything is ok. But when I try to run the loadLinkButton by clicking simple button, link button is created but event is not handled.
protected void Button1_Click(object sender, EventArgs e)
{
loadLinkButton();
}
I there any way to solve it? Or loadLinkButton must always regenerated on Page_Load, Page_init etc.
When working with dynamic controls, I always add the control in Page_Init, because viewstate loading will happen right after Init. If you add it to Page_Load, there is a chance that you will lose viewstate. Just make sure you provide a unique control ID.
It is important to know how ASP.Net determines which events to invoke. The source of each event is passed using a hidden field:
<input type="hidden" name="__EVENTTARGET" value="" />
Whenever the page loads, it pulls in the source of the event from that field and then determines which event to invoke. Now this all works great for controls added through markup because the entire control tree is regenerated on every request.
However, your control was only added once. When a Postback occurs, your control no longer exists as a Server control in the tree, and therefore the event never fires.
The simply way to avoid this is to make sure your Dynamic Controls are added every time the page loads, either through the Page_Init event, or the Page_Load event.
You are right. This is the expected behavior. Page_Load and Page_Init would be the events where you should be adding it.
That would be because when you click your dynamically generated linkbutton, you do a postback to the server. There you do an entirely new pageload, but your original buttonclick (that generates the link) never happened now, so the linkbutton is never made, and the event can not be thrown.
An alternative is to add the linkbutton you add dynamically, to your page statically, with Visible = false. And when you click the other button, make it visible.
I am not exactly sure what problem you are facing but you should put the dynamic controls code in Page_Init as suggested by #johnofcross:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Page_Init(object sender, EventArgs e)
{
CreateControls();
}
private void CreateControls()
{
var lb = new LinkButton();
lb.Text = "Click Me";
lb.Click += lb_Click;
ph.Controls.Add(lb);
ph.DataBind();
}
void lb_Click(object sender, EventArgs e)
{
lblMessage.Text = "Button is clicked!";
}
}