How to make UpdatePanel to do Update after custom Event? - asp.net

I'm setting UpdatePanel1.UpdateMode = UpdatePanelUpdateMode.Conditional; to make manual updates but it doesn't work for some custom events, when I have some event alike here:
protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
UpdatePanel1.Update();
}
My project is failing with:
An unhandled exception of type 'System.InvalidOperationException' occurred in System.ServiceModel.Internals.dll
Additional information: The Update method can only be called on UpdatePanel with ID 'UpdatePanel1' before Render.
even if I set ChildrenAsTriggers or not. Error message is not clear for me and I can't understand what should I do to process update right after I process my event?
addition:
aspx:
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
<asp:ListView ID="ListView1" runat="server">
</asp:ListView>
</ContentTemplate>
</asp:UpdatePanel>

I suppose you should change your markup like this
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
....
You should set UpdateMode="Conditional" in your markup itself

protected void Button1_Click(object sender, EventArgs e) {
discovery.FindAlreadyRegisteredServices();
discovery.discoveryClient.FindCompleted += FoundEvent;
// Try to call the update method after calling the custom even but in the click event of the button. Ensure you update the Trigger accordingly in the update panel
**UpdatePanel1.Update();**
}
protected void FoundEvent(object sender, FindCompletedEventArgs e) {
Label1.Text = (discovery.endpoints.Count > 0) ? discovery.endpoints[0].Address.ToString() : "nothing";
}
Try to add AsyncPostBackTrigger to the update panel with update mode as conditional
Although you are doing same thing explicitly.
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" EventName="Click" />
</Triggers>
Just to check if there is any other issue, you can set the updateMode property of the update panel to "Always"

Related

Buttons on ASP Repeater as trigger for Update Panel?

I am trying to work out with the idea of assigning a trigger for Update Panel Dynamically.
<asp:Repeater ID='justAnID' runat='server'>
<ItemTemplate>
<asp:Button ID='justAnotherID' runat='server'/>
<ItemTemplate>
</asp:repeater>
<asp:UpdatePanel runat='server'>
<ContentTemplate>
//mycontents
<ContentTemplate>
<triggers>
//??
<triggers>
</asp:repeater>
The point is - I can't come up any idea how to assign as trigger the buttons created by my repeater. Any idea for this concept?
You can assign an async trigger in code behind by looping the Repeater items. This has to be done on every postback.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
}
foreach (RepeaterItem item in justAnID.Items)
{
Button button = item.FindControl("justAnotherID") as Button;
ScriptManager.GetCurrent(Page).RegisterPostBackControl(button);
}
}

UpdatePanel only update from events

hello every one my problem goes like this
i have two updatepannels
<asp:UpdatePanel ID="new_word_panel_UpdatePanel" runat="server" UpdateMode="Conditional">
<ContentTemplate >
<asp:Panel ID='new_word_panel' runat="server">
<asp:Button ID='new_word_btn' runat="server" Text='give me a new word' Visible='false' OnClick='GenNewWord' />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate >
<asp:Button ID='Button8' runat="server" Text='hide' OnClick='hidegive' />
</ContentTemplate>
</asp:UpdatePanel>
the first updatepanel UpdateMode is set to Conditional so i can
update is content from the second updatepanel asp:Button ID='Button8' OnClick='hidegive'
event easily by using the Update() method.
this is the eventhandler:
protected void hidegive(object sender, EventArgs e)
{
if (new_word_btn.Visible == true)
new_word_btn.Visible = false;
else
new_word_btn.Visible = true;
**new_word_panel_UpdatePanel.Update();**
}
my problem is that i cant update the first UpdatePanel from reguler method on my page
although i am using the Update() method, i have try to update panel from this
method and nothing hapeens:
void PlayerWinEventHandler(object sender,Game.PlayerWinEventArgs e)
{
Session["score"] = int.Parse(Session["score"].ToString()) + 10;
UpdateScore();
if (new_word_btn.Visible == true)
new_word_btn.Visible = false;
else
new_word_btn.Visible = true;
new_word_btn.Text = "zibi";
**new_word_panel_UpdatePanel.Update();**
}
thanks for your help...
Why dont you use Triggers in your first update panel.
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button8" EventName="Click"/>
</Triggers>

All UpdatePanels on page updating on __doPostBack

I am experiencing off behaviour when trying to update an UpdatePanel via JavaScript when multiple UpdatePanels are on the page.
Will try to keep it short:
I'm already setting the UpdateMode to Conditional
I am passing in the correct ClientID of the UpdatePanel into the __EventTarget
In the code-behind, I am successfully able to find the target control
But for some reason, the OnLoad of every UpdatePanel on the page is fired.
Below is a snippet of code - any idea why this is happening?
Ascx:
<div class="updatePanelWrapper">
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" RenderMode="Inline" OnLoad="UpdatePanel1Load"
runat="server">
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
</div>
<div class="updatePanelWrapper">
<asp:UpdatePanel ID="UpdatePanel2" UpdateMode="Conditional" RenderMode="Inline" OnLoad="UpdatePanel2Load"
runat="server">
<ContentTemplate>
<div>
<asp:Panel ID="Panel2" runat="server">
</asp:Panel>
</div>
</ContentTemplate>
</asp:UpdatePanel>
</div>
CodeBehind:
protected void UpdatePanel1Load(object sender, EventArgs e)
{
UpdatePanelLoad(sender, e);
}
protected void UpdatePanel2Load(object sender, EventArgs e)
{
UpdatePanelLoad(sender, e);
}
protected void UpdatePanelLoad(object sender, EventArgs e)
{
// if only a async postback then load the control
if (ScriptManager.GetCurrent(Page).IsInAsyncPostBack)
{
// Some Logic ...
}
}
JavaScript (where I'm triggering the ASYNC PostBack):
var updatePanelWrappers = $(".updatePanelWrapper");
var availableWrapper = updatePanelWrappers[0];
var updatePanelElement = $(availableWrapper).children()[0];
var updatePanelId = $(updatePanelElement).attr("id");
window.__doPostBack(updatePanelId, "Some Arguments...");
With an UpdatePanel callback the page still goes through most of its lifecycle; which includes all of the Load events.

radiobutton checked status on asp:linkbutton postback

I have four radiobuttons inside a repeater control which by itself is inside an update panel
// code is something like this
`<asp:update panel ..>
...
<asp:Repeater>
..
<asp:checkbox>
..
..
</asp:update panel ..>
<asp:LinkButton ID="next2" runat="server" CssClass="button_Submit" Font-Bold="true" OnClick="next_ServerClick" Text="Submit">
<asp:ImageButton ID="next" ImageUrl="~/images/newSummary.jpg" runat="server" OnClick="next_ServerClick" ImageAlign="Middle"/>
protected void next_ServerClick(object sender, EventArgs e)
{
foreach (System.Web.UI.WebControls.RepeaterItem Item in repeatercontrol.Items)
{
chkbox = ((CheckBox)Item.FindControl(chkboxName));
if (chkbox.checked)
{
...
}
}
}`
I select one of the checkboxes and when i click image button, am able to get the correct status (checked =true) .
But when i use link button, it is always coming as checked =false as if the selection did not register.
Any ideas on why this is happening?
You need to register the linkbutton click event as a trigger in the update panel. See example below:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" >
<ContentTemplate>
...
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkButtonID" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Without seeing the code, the only reason the controls would be incorrect is if you are checking them in the Page Life Cycle before the ViewState is loaded.
EDIT:
Use separate event declarations and centralize the lookup logic:
protected void LinkButton1_Click(object sender, EventArgs e)
{
}
protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
{
}

UpdatePanel with ASP.NET Repeater and Checkbox Aync Postback issue

I have a rather annoying issue here
I can't get my CheckBox CheckedChange event to fire, or catch or whatever it is that fails:
ASPX Code
<asp:UpdatePanel runat="server" ID="udp_Lists" UpdateMode="Always">
<ContentTemplate>
<asp:Repeater ID="rep_showings" runat="server">
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
<div class="div_assignment">
<div class="div_assignment_text">
<asp:LinkButton runat="server" ID="lnk_show_task" OnClick="lnk_show_task_Click" CommandArgument='<%# Eval("Id") %>' Text='<%# Eval("TaskTitle") %>'></asp:LinkButton>
</div>
<div class="div_assignment_checkbox">
<asp:CheckBox runat="server" ID="chk_handle" AutoPostBack="true" OnCheckedChanged="chk_handle_Changed" ToolTip='<%# Eval("Id") %>' />
</div>
</div>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
</ContentTemplate>
<Triggers>
</Triggers>
The Code behind function "chk_handle_Changed" is never reached.
The Linkbutten works perfectly.
I took a look at your problem. I used the following code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.rep_showings.DataSource = new object[] { new { Title = "title", ID = "id" } };
this.rep_showings.DataBind();
}
}
protected void chk_handle_Changed(object source, EventArgs e)
{
Trace.Write("here");
}
protected void lnk_show_task_Click(object source, EventArgs e)
{
Trace.Write("here 2");
}
protected void rep_showings_ItemCommand(object source, RepeaterCommandEventArgs e)
{ }
The above code works. I think you are probably re-binding your repeater on every postback - I tested this by removing the "if (!IsPostBack)" statement in Page_Load(), and I was able to reproduce the problematic behaviour you describe.
Rebinding a control on every postback should be avoided if possible. Once a control is populated, it's data is taken care of by ViewState, so unless the data is changing, you should probably not be rebinding it all the time.

Resources