dynamically added buttons not firing click event c# [duplicate] - c#

This question already has answers here:
dynamically created button click event not firing
(5 answers)
Closed 8 years ago.
I have some buttons that are being dynamically added to an asp.net page. However the onclick event is not being fired. Here is the code for it being added and it is ran when the page loads. I am very new to ASP.NET so I am sure I am making some basic errors. TIA.
protected void Page_Load(object sender, EventArgs e)
{
FillTable();
string rownum = (goalstable.Rows.Count).ToString();
Button bt = new Button();
bt.Text = "View";
bt.ID = (rownum);
bt.CssClass = "button";
bt.Click += Viewbutton_Click;
goalstable.Rows[1].Cells[0].Controls.Add(bt);
}
FillTable() is a method that fills a table from an SQL DB.
The on click event for the button that has been added.
protected void Viewbutton_Click(object sender, EventArgs e)
{
getGID();
setGoalDets();
goals.Style.Add("display", "block");
darkLayer2.Style.Add("display", "block");
}
Any Ideas what I may be doing wrong.

In a nutshell, you need to add the button earlier in the Page lifecycle, before the Page_Load event.
What's happening is every server event — even simple button clicks — is a new HTTP request to your page. In turn, every HTTP request for your page results in a completely new-from-scratch C# page object. Therefore you start with a brand new Page object and a brand new ViewButton when the click event for your ViewButton is triggered.
To make things work correctly, so the new page has the same properties as the old, ASP.Net relies on a feature called ViewState. ViewState information is (typically) submitted with the http request from the client's browser, and is used to build a new Page object with the same Controls and property values as the old one.
Here's the trick: ViewState is restored for the page before the load event is processed. If the button does not exist yet at the time the ViewState is restored, that information is thrown away, and the page will not later know it needs to raise the click event (or rather, it will think there there is no button for the click event code to run in the first place).
Therefore, you need to move the code to create your button to the Pre_Init event, which runs before the ViewState is restored.
When working with "dynamic" controls in ASP.Net WebForms, I often find it easier to just add a reasonable number of controls to the page in a static manner and set them all so that their Visible property is false. Then at runtime I will set Visible back to true for just the controls that I need.

Related

Can a client get to a button click event programmatically if the button is not rendered? [duplicate]

This question already has answers here:
Is making an asp:Button control invisible enough to be sure users won't be able to click it?
(3 answers)
Closed 7 years ago.
This is in ASP.NET web forms, I have a save button on a screen. When I load the page initially, under certain conditions, the save button is not rendered.
button1.visible = false
In my button clicked event, I have this
public void button1_click(Object sender, EventArgs e)
{
SaveData();
}
The only security preventing the user being from being saved is on whether the save button is rendered.
In MVC, it would be trivial to access the save button action method just by making a HTTP POST to the server with my own modified request.
In ASP.NET Web forms, I'm a little bit confused because it relies on the encrypted ViewState being posted back. Do I still need to add this security to the button1_click event too? If so, then can you tell me how a client can fire a postback to the server that would reach the button click event without the button being visible?
That is one of the common mistakes about ViewState - it DOES NOT serve your click events and many other things.
Each click on button (or checkbox if autopostback is enabled) raises an form submitting to server. And all info about what was clicked is included in form postback data as plain text. Then server parses this data and, as your button has IPostBackDataHandler implemented, it raises appropriate events like "button id has been clicked". So, you can actually change request body:
D__EVENTTARGET=&__EVENTARGUMENT=&__VIEWSTATE=%2FwEPD...&ctl00%24_pdContent%24txtEmail=qwe%40aeqw.ry&ctl00%24_pdContent%24btnRtnUser=Login&__EVENTVALIDATION=%2FwEWDAL424aUAQLjtYbqCAKu8qTUCQLXm%2BfNAwKk2O%2B4DgK3ypStCAL6q%2BaACgKBnvb9CQLr8ey6CALxoZvDCALFt96ABgLMorjMAwoW3zW69NNlOXygWNnB6luGVWnk
Above you can see content of input with id=ctl00__pdContent_txtEmail and clicked login button with id=ctl00__pdContent_btnRtnUser.
More explanation about server events in WebForms here.
And please read more about ViewState here or even better explanation here.
https://stackoverflow.com/a/24064375/279911
According to this answer on another question, it looks like it is impossible to reach the button click event if the button is not rendered as long as you have the relevant security settings set.
Yes, setting a button's Visible property to false is enough to prevent
its Click and Command events from being raised, as long as you don't
turn off the default ASP.NET security features.

onclick event of dynamically added button

I need to add onclick event to some dynamically added button, but when i click the button, the onclick event is not fired. I saw some solutions to this, like link which says I should create the controls and attach the event on page_init or page_load every time there is a request, but, will this make the website very slow if I have a lot of controls to add?
Yes, you must recreate dynamic controls if you want to access them and have their events fire on postback.
If you have so many controls on a page that your site is slow, you have a design issue. Rethink your design so you do not have so many controls on one page (perhaps several pages/tabs?).
Allocate memory like below to button in Init Page Event
Button b = new Button();
b.Click += new EventHandler(b_Click);
void b_Click(object sender, EventArgs e)
{
}
As told by #Oded, this approach can create Design issue.
When you have a Div like control IN YOUR PAGE which is supposed to consume this Button, then you can GIVE CSS styles to give it proper alignment and proper placement.
You have two option.
Add control at Runtime
Add control at Design Time
Definitely, site will go slow in case of many controls in both cases. As both options will take memory BUT, By End of the Page Life Cycle, all the controls will get disposed.

ASP.Net and Ajax Server code behind constant re-create

I am writing a single ASP Form that dynamically changes Div boxes that are visible from mouse clicks on buttons and when text in textboxes is changed in an Ajax container. The problem i have is in the Page_Load function i create all the objects (text boxes, radio buttons, buttons, etc) dynamically from a .csv file template sitting on the asp hosting server. Everytime a postback happens, even within the Ajax window, the Page_Load function is called again and the .csv file is re-read, and all objects are re-created.
I have tried Checking for IsPostback before re-creating any objects but the objects are then nullified as they have never been created. The page is as a completely new page every single time.
Any ideas would be greatly appreciated.
Try using the Page_PreInit event rather than Page_Load to re-create/manipulate your dynamic controls:
protected void Page_PreInit(object sender, EventArgs e)
{
// create controls here
}
More info: http://msdn.microsoft.com/en-us/library/ms178472.aspx
When you create a dynamic control, you must accept the responsibility of
re-creating the control upon each postback.
You should create the control in the Page_Init event for every page
request whether it's a postback or not. If you create your controls in the Page_Init event then following that the
user entered values should automatically be filled in upon each postback so
you can acces them.

ASP .NET Button event handlers do not fire on the first click, but on the second click after a PostBack

Background: I am customizing an existing ASP .NET / C# application. It has it's own little "framework" and conventions for developers to follow when extending/customizing its functionality. I am currently extending some of it's administrative functionality, to which the framework provides a contract to enforce implementation of the GetAdministrationInterface() method, which returns System.Web.UI.Control. This method is called during the Page_Load() method of the page hosting the GUI interface.
Problem: I have three buttons in my GUI, each of which have been assigned an Event Handler. My administration GUI loads up perfectly fine, but clicking any of the buttons doesn't do what I expect them to do. However, when I click them a second time, the buttons work.
I placed breakpoints at the beginning of each event handler method and stepped through my code. On the first click, none of the event handlers were triggered. On the second click, they fired.
Any ideas?
Example of Button Definition (within GetAdministrationInterface)
public override Control GetAdministrationInterface()
{
// more code...
Button btn = new Button();
btn.Text = "Click Me!";
btn.Click += new EventHandler(Btn_Click);
// more code...
}
Example of Event Handler Method Definition
void Btn_Click(object sender, EventArgs e)
{
// Do Something
}
Page_Load Method that calls GetAdministrationInterface
protected void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsAsync)
{
List<AdministrationInterface> interfaces = <DATABASE CALL>;
foreach(AdministrationInteface ai in interfaces)
{
placeholderDiv.Controls.Add(ai.GetAdministrationInterface());
}
}
}
Good grief! I knew it was going to be something this stupid. Purely my fault of course and my lack of knowledge in ASP .NET.
After doing a multitude of Google searches and eventually being blocked by Google on suspicion of being a bot running automated scripts, I managed to squeeze in one last search in and stumbled across this article. Already at the point of giving up, I tried my best to read the article without skipping 10 lines at a time or looking for pretty pictures. In the section titled Assigning IDs to Dynamically Created Controls, I read these magical and most joyful words:
If you view the source HTML before you click the not-working button and after you have clicked it, you will notice a small difference. The buttons have different HTML IDs before and after the post-back. I got ctl04 and ctl05 before the post-back and ctl02 and ctl03 after the post-back.
ASP.NET button recognizes events by checking for a value for its ID in the Request.Form collection. (In truth it happens differently and controls do not check Request.Form collection by themselves. Page passes post data to controls by their IDs and to controls that are registered to be notified about post data). ASP.NET does not fire the Click event, because the button's ID has changed between the post-backs. The button you have clicked and the button you see after are different buttons for ASP.NET.
Sure enough, when I viewed the HTML the first time, my button had the ID ctl04$ctl36. After clicking the button, my button had the ID ctl04$ctl33.
So there you have it! All I had to do was set the ID on the buttons and presto! My event handlers are now being called!
Sample Solution:
public override Control GetAdministrationInterface()
{
// more code...
Button btn = new Button();
btn.Text = "Click Me!";
// !!THE BANE OF MY EXISTENCE!!
btn.ID = "The_Bane_of_My_Existence";
// !!THE BANE OF MY EXISTENCE!!
btn.Click += new EventHandler(Btn_Click);
// more code...
}
What a great way to spend two days...
I had the same problem, but the accepted answer here was not causing it. I had a text box and a search button, and clicking the button the first time didn't perform the search. The event handler of the button wasn't being hit. But clicking the button a second time did trigger the event on the server. Here is why:
If you have an <asp:Textbox> with its AutoPostBack set to true, after typing in the text box and then moving to click a button, the text box causes a post-back immediately the moment it loses focus. So the click even of the button doesn't count (the page is already posted-back as a result of the text box's event). That's why when you click the button a second time, it works because the text box is not involved in the second post-back.
Set the AutoPostBackproperty of the <asp:Textbox> to false to fix this issue.
A quick fix is to set an ID to the ASCX control your are loading on a page. For example, if your code is like this:
UserControl SpecsControl = (UserControl)Page.LoadControl("../name.ascx");
SpecsContainer.Controls.Add(SpecsControl);
then you need to add a line (before Controls.Add):
SpecsControl.ID = "Aribtrary_Name";
Then your handler method is fired at the first click.
I was facing the same problem. My button froze after my first click. For me this annoying problem got solved when I disabled the button's EnableViewState attribute.
For me it was the UpdatePanel , my Button and my TextBox were both inside an UpdatePanel , so when I post-back , it caused some weird behavior . It took it outside of the UpdatePanel and that fixed it .
Even i had the same problem. the cause was "localhost:1656/secure/login.aspx?ReturnUrl=%2f".
if the request contain %2f as query string, the first post will not be succeeded even though "%2f" is representing "/".
one way to avoid this by having a condition check in pageload
protected void Page_Load(object sender, EventArgs e)
{
string queryString = Request.QueryString.ToString();
if(queryString == "ReturnUrl=%2f")
{
Response.Redirect("/secure/login.aspx");
}
}
Whilst its hard to know exactly without seeing the full Page_load method it does smell a little bit like the event handlers are not hooking up until the page is reloaded.
eg:
if (IsPostBack) {
// Add handlers here ...
}
I had same problem. And I searched on internet i didnt find a solution. After that i found sample code and I used it. It worked for me. Web site link is below:
http://www.c-sharpcorner.com/UploadFile/abhikumarvatsa/calling-an-Asp-Net-C-Sharp-method-web-method-using-javascript/

Why would my child controls be uninitialized at the time of event attachment?

I have a page and a user control — we'll call them Detail.aspx and Selector.ascx.
Let's say the page shows the details of individual records in a database. The user control basically consists of a DropDownList control and some associated HTML. The DropDownList displays a list of other records to switch to at any time.
When the DropDownList fires its SelectedIndexChanged event, I'd like the parent page, Detail.aspx in this case, to handle it. After all, he'll need to know what was selected so that he can appropriately change the URL and the details shown, etc.
To do that, I've done what I usually do, which is also what the top answer says to do in this StackOverflow question:
public event EventHandler DropDownSelectedIndexChanged
{
add
{
MyDropDownList.SelectedIndexChanged += value;
}
remove
{
MyDropDownList.SelectedIndexChanged -= value;
}
}
The above code appears in the Selector.ascx.cs codebehind file.
As a result, on Detail.aspx, I can use it like so:
<cc1:RecordSelector ID="RecordSelector1" runat="server"
OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />
So far nothing fancy or surprising.
Here is my problem:
This causes a NullReferenceException when the browser hits Detail.aspx.
Debugging the problem shows that when the page is first hit, the public event I've shown above tries to add the event, but MyDropDownList is null, thus throwing the exception. From what I can tell, the events are added (or attempted to be added) before the Selector user control's Load event fires and thus also before the DropDownList's Load event fires.
Curiously, if I omit the OnDropDownSelectedIndexChanged attribute from Detail.aspx and instead put the following in the Page_Load event in Detail.aspx.cs:
protected void Page_Load(object sender, EventArgs e)
{
RecordSelector1.DropDownSelectedIndexChanged += new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
}
It works exactly as expected. The events are attached and handled just fine. No problems.
But this means several bad things:
I have to remember not to use the designer to add said event onto my user control
I have to remember not to add the event via attributes when working in source view
Worst of all, as the control's author I need to make sure everybody else using my control knows 1 and 2
So what am I doing wrong? Every example I've seen thus far shows similar usage of exposing child controls' events through a user control.
The reason this works:
protected void Page_Load(object sender, EventArgs e)
{
RecordSelector1.DropDownSelectedIndexChanged
+= new EventHandler(RecordSelector1_DropDownSelectedIndexChanged);
}
and this does not:
<cc1:RecordSelector ID="RecordSelector1" runat="server"
OnDropDownSelectedIndexChanged="RecordSelector1_DropDownSelectedIndexChanged" />
is because the first one adds the handler after the control has been initialized (via the page's Init). The second example gets parsed much earlier and as such the page is attempting to add the handler before the control has initialized.
Due to the nature of the page's life cycle I think you may have to live with adding the event handler in the code-behind. There will be no way to add the handler before the control is initialized because that control will always be null prior to initialization.

Categories