Second-click event handler

I create a dynamic button view that programmatically adds buttons based on the number of items in the database. As a simple workflow mechanism. On the first page, an event handler call works as expected. A new page with a new element is created and returned to the client. On the second reverse side, the event handler is NOT called, and the same elements are stored in the view. Using the same button (third postback) removes the breakpoint in the event handler.

My problem is why the event handler is not being called even if I rearrange the page (overrides OnInit ()) backward for each post?

Information: Methods LoadViewWithAlphabeticalDatasource()and LoadViewWithWorkflowItem()do the same thing, so I removed one of them in the section below. Events are added in the same way.

Code:

<pre>
   namespace EPS.Web.View.Article
   {
       public class DynamicGridView : BasePage, IDynamicGridView
       {


           protected override void OnInit(EventArgs e)
           {
               Presenter = new DynamicGridPresenter(this);
               if (IsPostBack)
               {
                   if (Presenter.Step smallerthan 2)
                   {
                       LoadViewWithAlphabeticalDatasource();
                   }
                   else
                   {
                       LoadViewWithWorkflowItem();
                   }
               }
           }

           [PageMethod]
           protected void Page_Load(object sender, EventArgs e)
           {
               if(!IsPostBack)
               {
                   SetSubmenuVisible = false;
                   Presenter.InitView();
                   PrepareView();
               }
               AddEvents();
               AddLabels();
               PageMethodsEngine.InvokeMethod(this);
           }



           private void Back_Clicked(object sender, EventArgs e)
           {
               Presenter.StepEngine(DynamicGridPresenter.BACK, string.Empty, string.Empty);
               PrepareView();
           }

           private void Cancel_Clicked(object sender, EventArgs e)
           {
               Presenter.StepEngine(DynamicGridPresenter.CANCEL, string.Empty, string.Empty);
               PrepareView();
           }

           private void ForwardString(object sender, EventArgs e)
           {
               Presenter.StepEngine(DynamicGridPresenter.FORWARD, ((LinkButton)sender).CommandArgument, string.Empty);
               PrepareView();
           }

          private void ForwardWorkflowItem(object sender, EventArgs e)
          {
              Presenter.StepEngine(DynamicGridPresenter.FORWARD, string.Empty, ((LinkButton)sender).CommandArgument);
              PrepareView();
          }

          protected void PrepareView()
          {
              phDynamicGridView.Controls.Clear();
              if (Presenter.Step smallerthan 2)
              {
                  LoadViewWithAlphabeticalDatasource();
              }
              else
              {
                  LoadViewWithWorkflowItem();
              }
          }

          private void LoadViewWithWorkflowItem()
          {
              var table = new HtmlTable();
              table.Attributes.Add("class", "tableDynamicGrid");
              int max = GetRowLength(WODatasource.Count);
              int temp = 1;
              int actualPosition = 0;
              int itr = 1;
              var tr = new HtmlTableRow();

              if (WODatasource.Count == 0)
              {
                  phDynamicGridView.Controls.Add(new HtmlTable());
                  return;
              }

              foreach (WorkflowItem s in WODatasource)
              {
                  if (actualPosition biggerOrEqual MaxLength && temp smallerOrEqual max)
                  {
                      table.Rows.Add(tr);
                      actualPosition = 0;
                      temp++;
                      tr = new HtmlTableRow();
                  }


                  actualPosition++;

                  var cell = new HtmlTableCell();
                  // cell.Attributes.Add("class", "cellDynamicGrid");
                  var btn = new LinkButton
                                {
                                    CommandArgument = s.Oid.ToString(),
                                    Text = s.SelectionItem.Name /*, CssClass = "linkButtonDynamicGrid"*/
                                };
                  btn.Click += ForwardWorkflowItem;

                  cell.Controls.Add(btn);
                  tr.Cells.Add(cell);

                  if (itr == WODatasource.Count && temp smallerOrEqual max)
                  {
                      while (itr biggerOrEqual WODatasource.Count && itr smallerThan max*MaxLength)
                      {
                          tr.Cells.Add(new HtmlTableCell());
                          itr++;
                      }
                      table.Rows.Add(tr);
                      phDynamicGridView.Controls.Add(table);
                      return;
                  }
                  itr++;
              }
          }

       }
 </pre>
+3
source share
5 answers

Thanks to everyone who helped me. I found a solution:

I did not know that a dynamically created button needs an identifier value. I assumed that Asp knows which button is created. Unfortunately, I was wrong. It seems like .NET is comparing the IDs on the buttons, not the ControlTree position.

I added

new Button {ID = itr.ToString ()}

Now that the server is rebuilding the site, event handlers are deleted.

Regards x.

+3
source

OnInit LoadPostData, . , .

  • Page_Init
  • LoadViewState
  • LoadPostData​​STRONG >
  • _Load
  • RaisePostDataChangedEvent
  • RaisePostBackEvent
  • Page_PreRender
  • SaveViewState
  • Page_Render
  • Page_Unload

Asp.net 1.1; 2.0 , , OnPreInit. .

Load . PreRender, DataBinding Load.

UPDATE

, . ( .)

a) , , . Postback/viewstate STATE (.. ), . OnInit (OnLoad , ). , ( )

b) ONCE, - [! IsPostBack], Load.

-Oisin

+4

.

+1

I had a similar problem. I tried many solutions, but in the end I noticed that one of the text fields on my web form was sent back to the server, which mixed up the situation. It was a difficult mistake, because I did not want my text file to do this.

0
source

if you use an ID variable without a declaration, this may be a problem issue. change the name of the variable name of your identifier.

0
source

Source: https://habr.com/ru/post/1707950/


All Articles