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;
Related
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);
}
I have a web application that I'm working on. On the parent page I have a status text box within an UpdatePanel. There is an iframe on the parent page that on page load executes some codebehind function in C# that happens to take a long time. (Quite a few lines of code get executed)
I was wondering how I could call out to the parent page from within the iframes code behind and do Updates on the UpdatePanel at various times in the function.
Here is some of the html from the main page:
<asp:UpdatePanel ID="StatusPanel" ClientIDMode="Static" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ClientIDMode="Static" ID="txtStatus" runat="server" Width="99%" Enabled="False"></asp:TextBox>
</ContentTemplate>
</asp:UpdatePanel>
And some of my c# codebehind from the iframe:
foreach (CorpLead lead in leadsPriority1)
{
UpdateStatus(lead.name);
xRMData.AssignCorpLead(agentProfiles, lead, service);
}
private void UpdateStatus(string lead)
{
if (this.Parent.FindControl("txtStatus") != null)
{
((TextBox)this.Parent.FindControl("txtStatus")).Text = lead;
((UpdatePanel)this.Parent.FindControl("StatusPanel")).Update();
}
}
Without some code examples it's just a guess for your specific problem but you can access controls on the parent page like so:
if (this.Parent.FindControl("ControlID") != null)
{
((Control)this.Parent.FindControl("ControlID")).Property = value;
}
Is this what you're looking for?
Since your IFRAME calls a function - it can call a function of the parent page (I am talking about JavaScript function). That parent function can use setInterval to programmatically "click" on a hidden LinkButton control which is set as a trigger for your UpdatePanel.
Result: UpdatePanel is refreshed at given intervals
I have an ASP.NET/C# application.
The application containsa PlaceHolder inside an UpdatePanel.
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButton1" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton2" EventName="Click"/>
<asp:AsyncPostBackTrigger ControlID="LinkButton3" EventName="Click"/>
</Triggers>
<ContentTemplate>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" Visible="false">
</asp:PlaceHolder>
</ContentTemplate>
</asp:UpdatePanel>
Basically I load controls in the placeholder depending on what LinkButton has been clicked using a function
LoadControls("ControlName");
I want to implement the ability for the user to use the Browser Back Button
in order to navigate through the controls. so I used AJAX History like this:
I save the current control ID in a session variable
Session["current"]
I enable History in scriptmanager and add an event handler
<asp:ScriptManager ID="ScriptManager" runat="server" EnableHistory="True" onnavigate="ScriptManager_Navigate" />
(The event handler will be covered below)
First I will create a new history point in the event handler of the Linkbuttons (they all use the same handler)
string ControlId=Session["current"].ToString();
if (ScriptManager.IsInAsyncPostBack && !ScriptManager.IsNavigating)
{
ScriptManager.AddHistoryPoint("Hist", ControlId);
}
then in the Scriptmanager navigate handler
protected void ScriptManager_Navigate(object sender, HistoryEventArgs e)
{
string Controlid = "";
if (e.State["index"] != null)
{
Controlid = e.State["Hist"].ToString();
LoadControls(Controlid );
}
}
When I load the controls I can navigate back and forward and everything is working fine
Here is the problem:
1) first problem
The first time I click on "Back". the history returns 2 steps and not one (after that it works normaly.) It is like the last history is not saved.
2) second problem
After "Backing" to the first step I can do an extra "Back" where the e.State["Hist"] will be null and give me an error. how can I disable the "Browser Back" button when the e.State["Hist"] will be null?
Thank you very much for any help. I hope I was clear.
If you need more info/code I will gladly provide them
Never mind. it was just a bug. the Session["Current"] was saving the previous control and not the current one. It is now fixed. I will leave this question if somebody needs a small example for creating ajax history.
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.
I have this layout:
<div runat="server" OnClick="ChangeText()" id="button">Ok</div>
<asp:UpdatePanel id="updater" runat="server">
<ContentTemplate>
<div id="text">Hello</div>
</ContentTemplate>
</asp:UpdatePanel>
I would like to have it so that when the button is clicked, the function ChangeText() gets called on the server - that function then updates the label in the UpdatePanel like so...
public void ChangeText() {
text.InnerText = "Goodbye";
}
How do I wire up the button so that it triggers the update for the updater?
If I understand your question correctly, add this below your node (inside the node).
<Triggers>
<asp:PostBackTrigger ControlID="button" />
</Triggers>
Why are you doing that with div. Instead of this you need to do other way like use the asp.net button and give the stylesheet for that button same like the div and place a trigger for update panel on button's click event.