Refreshing content in an update panel after postback - c#

I have a simple command event that toggles the visibility of two buttons.
When the controls are wrapped in an update panel as below, each subsequent click does not toggle the visibility of the buttons even though the code executes as expected.
Testing
The code works as expected when the update panel is removed.
After clicking the button refreshing the page reflects the changes made.
The issue clearly lies with refreshing the content after a postback.
I have tried upd.update and updateMode="always"
ASPX
<asp:UpdatePanel ID="upd" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Repeater ID="rpt" runat="Server">
<ItemTemplate>
<asp:Button ID="btnOn" runat="Server" Text="ON" CommandName="ON" CommandArgument='<%#: Container.ItemIndex%>' UseSubmitBehavior="false" />
<asp:Button ID="btnOff" runat="Server" Text="OFF" CommandName="OFF" CommandArgument='<%#: Container.ItemIndex%>' UseSubmitBehavior="false" />
</ItemTemplate>
</asp:Repeater>
</ContentTemplate>
</asp:UpdatePanel>
VB.NET
Protected Sub Page_PreRenderComplete(sender As Object, e As EventArgs) Handles Me.PreRenderComplete
For Each item In rpt.Items
'Do a check and set output'
btnOn.Visible = Not Output
btnOff.Visible = Output
Next
End Sub

Related

AsyncPostBackTrigger works only the first time

I have an update panel that includes a gridview.
This grid has a drop down list column.
Beta aspx code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" EnablePartialRendering="false" UpdateMode="Conditional">
...
<asp:GridView ID="Gv_Queue" runat="server">
<Columns>
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</Columns>
</asp:GridView
</asp:UpdatePanel>
I add the triggers of the DDL in the UpdatePanel by code:
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = dl.UniqueID; //dl is the Drop Down control
UpdatePanel1.Triggers.Add(trigger);
It works good at the fist selectedIndexChanged event... but the second time that the event is fired the trigger does not work correctly because a post back operation runs.
I already tried:
to change the AsyncPostBackTrigger to a PostBackTrigger but a missing
component exception is thrown.
change the updateMode in theUpdatePanel attribute to 'Always'.
Put another UpdatePanel in the ItemTemplate Column only for the
DropDown.
You have to re-create the trigger on every postback. You could add this code to the Load event of the DropDownList:
aspx:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:DropDownList ID="ddl_proprietà" OnLoad="ddl_proprietà_OnLoad" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged" AutoPostBack="true"/>
</ItemTemplate>
</asp:TemplateField>
codebehind:
protected void ddl_proprietà_OnLoad(object sender, EventArgs e)
{
AsyncPostBackTrigger trigger = new AsyncPostBackTrigger();
trigger.ControlID = ((Control)sender).UniqueID; // sender is the DropDown control
UpdatePanel1.Triggers.Add(trigger);
}
I found a solution to my own question.
You were all rights: I naively forgot that the trigger did not work after the first event just because it must be recreate.
Strangely even the recreation of the trigger in the on_Load even did not resolve the problem.
I did the trick with another update panel like this:
<asp:TemplateField HeaderText="H">
<ItemTemplate>
<asp:UpdatePanel runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddl_proprietà" runat="server" OnSelectedIndexChanged="ddl_proprietà_SelectedIndexChanged"
AutoPostBack="true"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddl_proprietà" />
</Triggers>
</asp:UpdatePanel>
</ItemTemplate>
</asp:TemplateField>
In my case the issue was because I had 2 controls on the page with the same Id. You may want to check this out if the previous responses did not resolve the issue.

Duplicate web form on button click

I have an ASP web form that includes divs, checkboxes, textboxes, buttons and dropdowns. Backend is c#.
I have an "Add" button at the bottom that when clicked, I want it to duplicate my form below the existing one.
Other than copying my form multiple times and toggling visibility based on a button click, is there some way I can put my form in a class and call on it based on the button click? Or any other way? Would think gridview is an option but I use divs within the form which is not allowed in gridview.
For Example:
<form id="form" runat="server">
<asp:ScriptManager ID="MainScriptManager" runat="server" />
<asp:UpdatePanel ID="updatepnl" runat="server">
<ContentTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:DropDownList ID="DropDownList1" runat="server"></asp:DropDownList>
<asp:Button ID="add" runat="server" Text="Add" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
The Add button can be moved outside of the update panel if necessary. But I would like to duplicate the form below the original via clicking the Add button. Would also be handy if I could have a remove option as well.

Asp .Net update panel not updating

I have 2 pages each with an update panel. It has the same content and the same trigger on both pages.
This is how it looks:
<div id="rating">
<span class="rateTxt">Rate</span>
<telerik:RadRating ID="RadRating1" runat="server" ItemCount="5" SelectionMode="Continuous"
Precision="Item" Skin="Default" OnRate="RadRating1_Rate" AutoPostBack="true">
</telerik:RadRating>
</div>
</li>
</ul>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<div id="divAddMessage" runat="server" visible="false">
<span class="rateTxt">Do you want to add a comment?</span>
<asp:TextBox runat="server" ID="txtComment" TextMode="MultiLine" Rows="3" Width="195">
</asp:TextBox>
<br />
<br />
<asp:ImageButton runat="server" ID="lnkAddComment" ImageUrl="~/App_Themes/123reg/Images/submit-rating.png"
OnClick="lnkAddComment_Click" OnClientClick="showSuccessMessage();" />
<br />
<br />
</div>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="RadRating1" EventName="Rate" />
</Triggers>
</asp:UpdatePanel>
Now when the user rates something with the RadRating the Rate event is triggered which causes a postback.
On the handler for the Rate event I do the following:
protected void RadRating1_Rate(object sender, EventArgs e)
{
divAddMessage.Visible = true;
}
The same code is exactly on 2 pages. On one page it updates the divAddMessage and it becomes visible but on another page it doesn't become visible. Even if I set it to visible on that page when it postback again the Visible property is still false even after setting it to true in the above handler.
I only set the visibily to false in the aspx file and in the above handler I set it to true.
It turns out that I get an error in the javascript console:
Sys.InvalidOperationException: Sys.InvalidOperationException: Could not find UpdatePanel with ID 'ctl00_mainContentPlaceHolder_updatePanel'. If it is being updated dynamically then it must be inside another UpdatePanel.
I have another update panel inside a multiview. But because the active view is not the one with the update panel on postback the update panel isn't rendered.
Check the parent/container elements for divAddMessage. Have they got their Visibility set to false. If so then the child element will always be Visible=false even if you have set it explicitly to true. That's driven me mad in the past.
Just a thought
I had a custom control and had set the Visible property to false on declaration in the aspx file and that was overriding the visibility of any child elements within the control itself.

A control with ID could not be found for the trigger in UpdatePanel

I have an update panel that has UpdateMode of Conditional and ChildrenAsTriggers set to false. I only want a few controls to cause an asynchronous postback:
<asp:UpdatePanel ID="updPnlMain" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
// ...
<asp:Repeater ID="rptListData" runat="server">
<ItemTemplate>
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ItemTemplate>
</asp:Repeater>
// ...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnAddSomething" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
I am getting the following error when I try and load this page:
A control with ID 'btnAddSomething' could not be found for the trigger in UpdatePanel 'updPnlMain'.
Since my btnAddSomething control is in a repeater and might not be there right away it acts like it is nonexistent. How can I get around this?
Because your control is in the repeater control and it is out of scope to the Trigger collection. By the way you don't need to add trigger because your button control is already in the UpdatePanel, it will update when you click the button.
Edit: There is a solution if you really want to update your updPnlMain updatepanel. You can put in another updatepanel and put your button in that panel. e.g.
<asp:UpdatePanel ID="updButton" runat="server" UpdateMode="Conditional">
<asp:Button ID="btnAddSomething" runat="server" OnClick="btnAddSomething_Click" />
</ContentTemplate>
and then simply call the updPnlMain.Update(); method in btnAddSomething_Click event.
It will actually do what you are looking for :)

Update panel and AsyncPostbackTriggers

I would love to add AsyncPostback Triggers dynamically to the ImageButtons found within the UpdatePanel control
<asp:Content ID="Content1" ContentPlaceHolderID="Content" runat="server">
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers></Triggers>
<ContentTemplate>
<asp:ListView ID="ListView2" runat="server">
<ItemTemplate>
<asp:ImageButton ID="btnRemove" runat="server" ImageUrl="~/Images/Delete.png" CommandName='<%# DataBinder.Eval(Container.DataItem, "QuestionsID") %>'/>
</ItemTemplate>
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>
</asp:Content>
The problem is that i cannot figure out how to do it!
I already tried different ways, but NONE seems to work.
My last try, was trying to add the triggers in the ListView ItemDataBound event
Private Sub ListView2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles ListView2.ItemDataBound
For Each btnError As ImageButton In e.Item.Controls.OfType(Of ImageButton)()
Select Case btnError.ID
Case "btnRemove"
Dim trigger As New AsyncPostBackTrigger()
trigger.ControlID = UpdatePanel1.FindControl(btnError.ID).UniqueID
UpdatePanel1.Triggers.Add(trigger)
End Select
Next
End Sub
which of course is not correct.
So could you please tell me how can i add dynamically triggers to the UpdatePanel controls?
Since your buttons are already inside the update panel there is no reason for adding them dynamically as they generate an async postback anyway.

Categories

Resources