FindControl () return null

I am trying to create an application that adds dynamic controllers. I have a masterpage, my asp: Content is here:

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"> <asp:ScriptManager ID="scriptManager1" runat="server"> </asp:ScriptManager> <div style="margin: 10px"> <asp:UpdatePanel ID="updatePanel1" runat="server"> <ContentTemplate> <asp:PlaceHolder runat="server" ID="myPlaceHolder" /> </ContentTemplate> <Triggers> <asp:AsyncPostBackTrigger ControlID="btnAdd" EventName="Click" /> </Triggers> </asp:UpdatePanel> </div> <asp:Button ID="btnAdd" runat="server" Text="Add" /> 

After clicking in btnAdd, I want to add two text fields. I am trying to do this as http://jagdeepmankotia.wordpress.com/2010/01/30/dynamically-add-controls-in-asp-net-c/

This is my code:

  static int myCount = 1; private TextBox[] color; private TextBox[] text; protected override void OnInit(EventArgs e) { base.OnInit(e); color = new TextBox[myCount]; text = new TextBox[myCount]; for (int i = 0; i < myCount; i++) { TextBox tbColor = new TextBox(); tbColor.ID = "colorTextBox" + i.ToString(); myPlaceHolder.Controls.Add(tbColor); color[i] = tbColor; TextBox tbText = new TextBox(); tbText.ID = "textTextBox" + i.ToString(); myPlaceHolder.Controls.Add(tbText); text[i] = tbText; LiteralControl literalBreak = new LiteralControl("<br />"); myPlaceHolder.Controls.Add(literalBreak); } } public Control GetPostBackControl(Page page) { Control control = null; string ctrlname = page.Request.Params.Get("__EVENTTARGET"); if (ctrlname != null && ctrlname != string.Empty) { control = page.FindControl(ctrlname); } else { foreach (string ctl in page.Request.Form) { Control mycontrol = page.FindControl(ctl); if (mycontrol is System.Web.UI.WebControls.Button) { control = mycontrol; // This gives you ID of which button caused postback break; } } } return control; } protected void Page_PreInit(object sender, EventArgs e) { Control myControl = GetPostBackControl(this.Page); if (myControl != null) if (myControl.ClientID.ToString() == "btnAdd") myCount = myCount + 1; } protected void btnAdd_Click(object sender, EventArgs e) { //handled in PreInit } 

When the GetPostBackControl () function in loap foreach searches for my btnAdd, for example, in the first iteration for ctr "ctl00 $ MainContent $ scriptManager1", myControl is null ... In the next iterations also ... So my function always returns zero. What could be the reason?

+4
source share
1 answer

FindControl searches only the direct children of the container. Since you are starting at page level, you will need to go through the UpdatePanel child control to get to your btnAdd control.

Look here for an example of how to do this.

Edit: I'm not sure I understand why you look at your button like this, since there is only one static button on the screen - you won’t need to use FindControl in this case.

 <asp:Button ID="btnAdd" runat="server" Text="Add" onclick="btnAdd_Click" /> 

(or in code, btnAdd.OnClick += new EventHandler(btnAdd_Click); )

Even if you had several buttons in your form added dynamically, you can connect ALL of them to the same Click Button handler, in which case the sender will contain the Button control that was clicked. Usually you use FindControl to clear data from dynamically added input controls (text box, etc.), and not to see which control called Postback (since the "sender" in the corresponding event handler will be easier)

Edit 2: You can add buttons dynamically in the same way as other controls.

  Button myButton = new Button(); myButton.Text = "Click Me"; myButton.Click += new EventHandler(btnAdd_Click); myPlaceHolder.Controls.Add(myButton); 

If you want all the controls that you have already added to stay between postbacks, then enable the viewstate on the page and on the controls, and then make sure you only add the controls once without postback to OnInit:

  base.OnInit(e); if (!IsPostBack) { // ... Add controls here 

You can save the "mycount" state in a hidden field (in the same update and with the view turned on) - you will need to analyze it each time int. Or you can use SessionState to track it.

+8
source

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


All Articles