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.
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 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;
I have simplified and probably made a typo or two below, but I am trying to show and hide the visibility of pnl2, this code however doesn't work. I thought as long as the panel was in another update panel I can control visibility.
Oddly enough if I debug this in Visual studio, and F10 all the way through it, it shows the visibility correctly, but when I hit my Last F10 and the page loads, it's always incorrect.
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="updPanel1">
<ContentTemplate>
<asp:Panel runat="Server" ID="pnl1"/>
<asp:Button runat="Server" ID="hidePanel2" OnClick="HidePanel2"/>
</ContentTemplate>
</UpdatePanel>
<asp:UpdatePanel runat="server" UpdateMode="Always" ID="updPanel2">
<ContentTemplate>
<asp:Panel runat="Server" ID="pnl2"/>
</ContentTemplate>
</UpdatePanel>
protected void hidePanel2(object sender, EventArgs e)
{
if (pnl2.Visible == true)
{
pnl2.Visible = false;
}else
{
pnl2.Visible = true;)
}
The problem is that you are trying to update the visibility of “pnl2” from a button that is in the UpdatePanel named “updPanel1”. The postback for the button “hidePanel2” only causes the content of update panel “updPanel1” to change. The rest of the form, including update panel “updPanel2”, does not change.
The most direct solution, if the two panels are next to each other, is to put them both in one update panel. If you have some other layout then you will need to explain what it is before we can help.
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.
I have a master page that contains an image as follow:
<asp:Image ID="imgCompanyLogo" runat="server" ImageUrl="image path" />
and in a child page I want to edit the property ImageUrl as follow:
Image imgCompanyLogo = (Image)Page.Master.FindControl("imgCompanyLogo");
imgCompanyLogo.ImageUrl = ResolveUrl("~/images/CompanyLogo/Logo.png");
and it doesn't give me an exception, but it doesn't change anything.
Note: I have an UpdatePanel in the child page.
Since the image is sitting outside of the UpdatePanel, server side changes will not be executed on the image after a partial postback. Your only option is to inject JavaScript into the page and change the image URL.
Use the ScriptManager.RegisterStartupScript Method to inject JavaScript after the partial postback.
Something like the following will work for you:
C#
protected void btnPostback_Click(object sender, EventArgs e)
{
imgCompanyLogo.ImageUrl = ResolveUrl("~/images/CompanyLogo/Logo.png");
ScriptManager.RegisterStartupScript(btnPostback,this.GetType(), "myScript", "ChangeImage('" + ImageUrl + "');",false);
}
JavaScript
function ChangeImage(imgURL) {
//make sure the ID of the image is set correctly
document.getElementById('imgCompanyLogo').src = imgURL;
}
Wrap image by UpdatePanel with UpdateMode="Always"
Master Page:
<asp:UpdatePanel runat="server" UpdateMode="Always">
<ContentTemplate>
<asp:Image runat="server" ID="Image1" />
</ContentTemplate>
</asp:UpdatePanel>
<asp:ContentPlaceHolder ID="MainContent" runat="server">
</asp:ContentPlaceHolder>
public void SetImageUrl(string url)
{
Image1.ImageUrl = url;
}
Child Page:
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:UpdatePanel runat="server">
<ContentTemplate>
<asp:Button Text="Click Me" runat="server" OnClick="UpdateImage" />
</ContentTemplate>
</asp:UpdatePanel>
protected void UpdateImage(object sender, EventArgs e)
{
((Main)Master).SetImageUrl("~/Images/0306d95.jpg");
}
The code above works well for me.
Have a look at Sys.WebForms.PageRequestManager Class. Define handler in javascript and may change the image source.
If your code is being run after an async postback (per your UpdatePanel) then changes to anything outside the UpdatePanel will not be rendered. Content in the master page would definitely seem to qualify.
If this is what you're trying to do, this model won't really work. You will need to use some client script to effect changes to already-rendered content when working with this model.
An UpdatePanel is a construct to identify an area that's updated through ajax. The page is not actually reloaded. So an postback can never change content that's outside of the UpdatePanel (or control bound to that panel) that sourced it.
Here's a basic implementation (using jQuery). Add a hidden field to pass the new source to the client. This must be inside the UpdatePanel. Change this value from the server when you want the image to update with new_image_src.Value=ResolveUrl(...);
<asp:HiddenField ClientIdMode="Static" runat="server" id="new_image_src"
value="" EnableViewState="false">
Give your image a static id too to make life easier:
<asp:Image ClientIdMode="Static" runat="server" id="dynamic_image" ImageUrl="..." >
Add javascript to the page (should NOT be in the UpdatePanel):
function updateImage() {
var new_src=$('#new_image_src');
if (new_src) {
$('#dynamic_image').attr('src',new_src);
/// erase it - so it won't try to update on subsequent refreshes
new_src.val('');
}
}
$(document).ready(function() {
/// adds an event handler after page is refreshed from asp.net
Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(updateImage);
});
This wouldn't be difficult to do without jquery either but seems more common than not these days.