Missing page controls when calling SelectedIndexChanged of DropDownList (asp.NET) - c#

Here is my problem :
I have an asp:Panel in which I dynamically create a list of .ascx controls.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Panel runat="server" ID="myPanel">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
The controls are correctly created. I also have a button calling an "onserverclick" event :
<button runat="server" id="myButton" onserverclick="myButton_Click" type="button" class="button"> My Button </button>
And finally, I created an asp:DropDownList like this one, with a server event triggered on SelectedIndexChanged :
<asp:DropDownList runat="server" ID="myDdl" AutoPostBack="true" OnSelectedIndexChanged="myDdl_SelectedIndexChanged" OnDataBound="myDdl_DataBound"></asp:DropDownList>
In response to the two events (myButton_Click and myDdl_SelectedIndexChanged), I'm trying to parse the child controls of myPanel, like this :
ControlCollection controls = myPanel.Controls;
This is the very first line of code within the two response methods.
When clicking on myButton, all the controls of myPanel are correctly retrieved.
BUT
When calling the SelectedIndexChanged event of myDdl, the only child control retrieved in the ControlCollection is the first one, although I can clearly see them on the webpage.
I would appreciate some help, I'm stuck on this since yesterday :(
Thank you !
EDIT
Here is how I'm creating my controls during the page creation:
while(condition)
{
MyControl newControl = (MyControl)Page.LoadControl("~/Controls/MyControl.ascx");
// Setting MyControl attributes
....
myPanel.Controls.Add(newControl);
}

Related

UpdatePanel Questions

I have an update panel that lives in a control that lives on a masterpage. Is it possible to access the updatepanel and cause it to fire in the code-behind of another aspx page that this control is added to at run-time?
There is one case where a button is clicked on page X, and when that button is clicked, I need the update-panel to run. I have tried this so far with no luck:
Code-Behind
udp = FindControl("udpWishlist") as UpdatePanel;
if (udp != null){
udp.Update();
}
Snippet from control of the UpdatePanel I'm trying to use
<!--update wishlist on cartadd-->
<asp:UpdatePanel ID="udpWishlist" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:LinkButton ID="lbwishlist" runat="server" href="/wishlist.aspx"></asp:LinkButton>
</ContentTemplate>
<Triggers>
</Triggers>
</asp:UpdatePanel>
Well, you can create a public Method inside the user control, like this:
public void Update()
{
udpWishlist.Update();
}
Inside the page that contains the UserControl:
YourUserControlType uc = (YourUserControlType)Page.FindControl("YourUserControlID");
uc.Update();
Since the update panel lives in another .aspx, it's out of scope for FindControl(). You may be able to do something like:
udp = this.Page.Master.FindControl("udpWishlist") as UpdatePanel;

Gridview outside UpdatePanel not showing

I have an ItemCommand event in a button found in a listview.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:ListView ID="systemsView" runat="server"
onitemcommand="systemsView_ItemCommand">
<ItemTemplate>
<asp:Button ID="btnView" runat="server" Text='<%#Eval("SYSTEM_DESC") %>' CommandArgument='<%# Eval("ROW_ID")%>'
class="systemButtonStyle" />
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
When the itemCommand is fired I want to bind another gridview which is not inside the update panel. The gridview is not showing when the event is fired. Any ideas how I could solve this problem please?
Since UpdatePanels only perform a PostBack on controls within their ContentTemplate, all controls that are expected to be updated by a Partial PostBack must be housed within the same UpdatePanel's ContentTemplate.
If you need to update something outside of an UpdatePanel with an action that occurs within it's Partial PostBack, you will have to rethink your use of the UpdatePanel.
Two solutions:
Add the other GridView to the UpdatePanel's ContentTemplate.
Remove the UpdatePanel entirely and use the normal PostBack.

Fail to continously add new control into Panel/Placeholder during partial postback

I want to create a dynamic form where user has a button to click on it.
When the button clicked, it will trigger a partial postback and add a new control into a placeholder.
My problem is, when the button is clicked for the first time, it able to create a new control in the placeholder. But it will not create more than that even though i have clicked the button for few times.
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Add images" OnClick="Button1_Click" />
<asp:HiddenField ID="HiddenField1" runat="server" Value="1" />
<br />
<asp:Panel ID="PlaceHolder1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Code behind:
protected void Button1_Click(object sender, EventArgs e)
{
PlaceHolder1.Controls.Add(new FileUpload());
}
Unless a dynamically added control is added at the preinit or init stage, it does not persist beyond a post back. So when the button is clicked for the second time, the first added control is lost and then the button logic adds a control again, leaving you with one control every time. For a control added after the init stage, you would need to store its state and recreate it on each postback. This is described int this article:
http://www.codeproject.com/Articles/3684/Retaining-State-for-Dynamically-Created-Controls-i
You may also be able to achieve your goals without using dynamically added controls. One possible approach is to use a ListView control, add a FileUpload control to its ItemTemplate and add a new record to the list view data source every time the button is clicked.

Dynamically loading usercontrol in updatepanel

So I'm trying to dynamically load a usercontrol into a placeholder in another usercontrol inside a updatepanel.
basically I've got this markup:
<asp:UpdatePanel ID="upPopup" runat="server">
<ContentTemplate>
<UC:Popup runat="server" ID="UC_Popup" />
</ContentTemplate>
</asp:UpdatePanel>
And then this markup inside that usercontrol (Popup):
<div id="modal">
<asp:PlaceHolder ID="phPopupPlaceholder" runat="server"></asp:PlaceHolder>
<asp:Label ID="lblModal" runat="server"></asp:Label>
</div>
In the codebehind of the popup usercontrol I have:
public void Show(UserControl control) {
this.phPopupPlaceholder.Controls.Add(control);
this.lblModal.Text = "Loaded";
}
And I call this show method elsewhere with:
Popup.show(new MyUserControl());
Nothing loads into the placeholder though.
But in the show method I can load regular server-controls fine like this:
this.phPopupPlaceholder.Controls.Add(new Label(){ Text = "Label!" });
Can anyone explain to me why regular controls are loaded fine, but my usercontrol isn't loaded?
Okay, I learned that I HAVE to use LoadControl method apparently, but I don't quite understand why this is necessary :/

ASP.NET: Custom Control Reloads Whole Page

I'm working on a ASP.NET site with C# code.
Now the trouble starts when I create a custom control programmatically. The control displays in a panel, but when I click one of the buttons of the control it does nothing. If I click them twice, the user control disappears.
Using the debugger, I found that it's doing a postback, which is strange because I tried using buttons and setting the usesubmitbehavior to false; it's still sending postbacks.
Here is where the control is inserted on the default.aspx file
<asp:UpdatePanel runat="server" ID="contentHolderUpdatePanel"
UpdateMode="Conditional">
<ContentTemplate>
<asp:Panel runat="server" ID="contentPanel">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
Here is the ASPX from ListadoAuditoria of the control.
<asp:UpdatePanel runat="server" ID="auditorTableUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Table runat="server" ID="auditorTable" BorderWidth="0" Width="100%">
<asp:TableHeaderRow HorizontalAlign="Center">
<asp:TableHeaderCell>Button
</asp:TableHeaderCell>
</asp:TableHeaderRow>
</asp:Table>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel runat="server" ID="formHolderUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label runat="server" ID="testLabel" Text="bbbbbbbbbbbbb" ></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
The method that the button invokes should change the text of the label testLabel from "bbbbbbbbbbbbb" to "aaaaaaaaaaa". Obviously I'm doing an auditorTableUpdatePanel.Update() after I modify the text.
The control CS
protected void Page_Load(object sender, EventArgs e)
{
loadAudits();
}
public void loadAudits()
{
for(int i=0;i<10;++i)
{
TableRow row = new TableRow();
TableCell cell1 = new TableCell();
ImageButton deleteButton = new ImageButton();
deleteButton.ImageUrl = "~/image.gif";
deleteButton.Click += generateNewPart;
deleteButton.EnableViewState = true;
deleteButton.ID = i.ToString();
cell1.Controls.Add(deleteButton);
row.Cells.Add(cell1);
auditorTable.Rows.Add(row);
}
}
public void generateNewPart(object sender, EventArgs e)
{
tumadre.Text = "aaaaaaaaaaaa";
formHolderUpdatePanel.Update();
}
And here is the code when I generate the control and insert it into the panel:
Panel panel = (Panel)Page.FindControl("contentPanel");
UpdatePanel updatePanel = (UpdatePanel)Page.FindControl("contentHolderUpdatePanel");
ListadoAuditorias listadoAuditorias = (ListadoAuditorias)LoadControl("~/CargaDeAuditoria/ListadoAuditorias.ascx");
panel.Controls.Add(listadoAuditorias);
updatePanel.Update();
I looked over the Internet and didn't found anything.
I'm not sure where exactly the code to dynamically add the controls is, but it must be called on EVERY postback to re-add the controls. You can't just add it once and forget about it. When you postback, the page will re-render with the markup in your aspx page (which does not have your dynamic controls, obviously). The values from the dynamically added controls will still be in ViewState, but the controls will not be re-rendered.
I'm not entirely sure what I'm looking at; I don't know if the "code of the control" is the code for the ListadoAuditorias control that you're loading. If so, I didn't notice any buttons.
So I could be wrong here, but the first thing that pops out at me is that it looks like you're loading the ListadoAuditorias control and then adding it to a normal Panel control. If one of the controls inside of ListadoAuditorias triggers a postback, and it's not contained within an UpdatePanel, then yes, I'd expect the page to do a postback and reload, unless you have specified the ChildrenAsTriggers and UpdateMode properties to be something other than their default values (I think). So I would just suggest that you take a look at where you are adding your controls. Make sure that they're contained within an UpdatePanel, if that's what your intention is.
Also, note that the UseSubmitBehavior property of the Button control does not prevent the button from initiating a postback. That property only determines whether the button gets rendered as <input type="submit" /> or <input type="button" />. In the latter case (when you set UseSubmitBehavior to false) the control still renders javascript in the HTML element's onclick attribute to cause a postback.
EDIT: I've amended my explanation regarding UpdatePanel control to mention the ChildrenAsTriggers and UpdateMode properties.

Categories

Resources