How can I create buttons and hook events from postback

I need to generate buttons initially based on a fairly intensive search for the processor and disk. Each button will display a selection and trigger a postback. My problem is that postback does not call the b_Command command. I think because the original buttons were not recreated. I cannot vouch for the original postback search to re-create the buttons, so I would like to generate the required button from the postback information.

How and where am I doing this? Should I do this before Page_Load for example? How can I rebuild CommandEventHandler from postback - if at all?

namespace CloudNavigation { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { // how can I re-generate the button and hook up the event here // without executing heavy search 1 } else { // Execute heavy search 1 to generate buttons Button b = new Button(); b.Text = "Selection 1"; b.Command += new CommandEventHandler(b_Command); Panel1.Controls.Add(b); } } void b_Command(object sender, CommandEventArgs e) { // Execute heavy search 2 to generate new buttons Button b2 = new Button(); b2.Text = "Selection 2"; b2.Command += new CommandEventHandler(b_Command); Panel1.Controls.Add(b2); } } } 
+6
c # events postback
source share
6 answers

The b_Command event handler method is not executed because the post back buttons are not recreated (because they are dynamically generated). You need to recreate them every time your page is recreated, but for this you need to explicitly cache the information somewhere in the state.

If this makes it easier to work with the page area, save it to the ViewState (in the form of strings - if you start loading the ViewState with objects, you will see that the performance decreases), so you can check it at the next load (or any other previous event) and re-create buttons when reloading the page. If the operation is limited to the session, you can easily save the object (array or something else) in the session and get it the next time you load (or initialize) to recreate the controls.

This scenario means that you just need to save some information about your button in your b_Command EventHandler, rather than creating and adding buttons, because if you do this, you will lose relative information in the next postback (as it is now).

so your code will become something like:

 namespace CloudNavigation { public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (IsPostBack) { this.recreateButtons(); } else { // Execute heavy search 1 to generate buttons Button b = new Button(); b.Text = "Selection 1"; b.Command += new CommandEventHandler(b_Command); Panel1.Controls.Add(b); //store this stuff in ViewState for the very first time } } void b_Command(object sender, CommandEventArgs e) { //Execute heavy search 2 to generate new buttons //TODO: store data into ViewState or Session //and maybe create some new buttons } void recreateButtons() { //retrieve data from ViewState or Session and create all the buttons //wiring them up to eventHandler } } } 

If you do not want to call recreateButtons on page loading, you can do it on PreLoad or on Init events, I don’t see a difference, since you can access ViewState / Session variables all over the world (in the Init window, the viewstate is not applied, but you can access it to recreate the dynamic buttons).

Someone will hate this decision, but as far as I know, the only way to save the state database is ViewState - Session - Page. Pass or client side cookies.

+5
source share

Buttons must be created before the boot event, or the state will not be connected correctly. Instead, recreate your buttons in Init ().

As for how to do this without restarting the search, I suggest you cache the results somewhere. The existence of a result set in the cache is how your button code in the Init () event will know that it should fire.

Alternatively, you can place buttons on the page statically. Just put enough to handle all the search results. If you think it might be too many items, then ask yourself: will your users really want to sort this many items? Perhaps you should consider swapping this data, in which case the static buttons are no longer so important.

+1
source share

What happens when inverse event processing tries to find a control that does not exist in the collection. Checkout Denis DynamicControlsPlaceholder @ http://www.denisbauer.com/ASPNETControls/DynamicControlsPlaceholder.aspx

Hope this helps Bruno Figueiredo http://www.brunofigueiredo.com

+1
source share

Is an event handler connected to your ASPX?

 <asp:Button id="btnCommand" runat="server" onClick="b_Command" text="Submit" /> 
0
source share

I agree with Joel about caching search results. As for the buttons, you can create them dynamically at the initialization or loading stages of the page life cycle, but keep in mind that if you delete the button and then add it programmatically, you will ruin your state.

In one of my projects, we have a dynamic form that the field son generates, and the way we work with it is an array that is stored in the cache or in the page view. The array contains buttons for display, and on each page load it re-creates buttons so that the state can be loaded correctly into them. Then, if I need more buttons or a whole new set, I mark the hide value in the array and add a new set of values ​​to the array for the new set of corresponding buttons. Thus, the state is not lost, and the buttons continue to work.

You also need to make sure that you add a handler for the on_click event for your buttons if you create them programmatically, which, in my opinion, I see in your code above.

0
source share

Here is an example with custom viewstate processing (note that the buttons have EnableViewState = false ):

  protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { // Execute heavy search 1 to generate buttons ButtonTexts = new ButtonState[] { new ButtonState() { ID = "Btn1", Text = "Selection 1" } }; } AddButtons(); } void b_Command(object sender, CommandEventArgs e) { TextBox1.Text = ((Button)sender).Text; // Execute heavy search 2 to generate new buttons ButtonTexts = new ButtonState[] { new ButtonState() { ID = "Btn1", Text = "Selection 1" }, new ButtonState() { ID = "Btn2", Text = "Selection 2" } }; AddButtons(); } private void AddButtons() { Panel1.Controls.Clear(); foreach (ButtonState buttonState in this.ButtonTexts) { Button b = new Button(); b.EnableViewState = false; b.ID = buttonState.ID; b.Text = buttonState.Text; b.Command += new CommandEventHandler(b_Command); Panel1.Controls.Add(b); } } private ButtonState[] ButtonTexts { get { ButtonState[] list = ViewState["ButtonTexts"] as ButtonState[]; if (list == null) ButtonTexts = new ButtonState[0]; return list; } set { ViewState["ButtonTexts"] = value; } } [Serializable] class ButtonState { public string ID { get; set; } public string Text { get; set; } } 
0
source share

All Articles