UpdatePanel wrapped around a user control - asp.net

I have a user control which contains some buttons and a placeholder. Those buttons cause controls to be added/removed from placeholder. Everything works fine.
Now I want to put this user control in a page, and wrap it in an updatepanel like so:
<asp:UpdatePanel ChildrenAsTriggers="true" ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
</asp:UpdatePanel>
When I run the page, it's still doing a full postback when I hit one of the buttons inside the user control. What am I doing wrong, and how can I remedy this?
Update:
protected void Page_Init()
{
ScriptManager scr = ScriptManager.GetCurrent(this.Page);
Response.Write("EnablePartialRendering: " + scr.EnablePartialRendering);
}
Outputs "EnablePartialRendering: true"

Make sure you have EnablePartialRendering=true on your ScriptManager in the page.
Update
It looks like your UserControl has no events to be looking for...you have 2 options here. Move the UpdatePanel inside the UserControl .ascx so it can see the button events as children to rig up or add an event for it to see, to do that try something like this:
public event EventHandler Click;
void btn_del_Click(object sender, EventArgs e)
{
if (NumberOfRowControls > 0)
{
var rowToWhack = panel_rows.Controls.Children().Single(x => x.ID == "myrow" + (NumberOfRowControls - 1));
panel_rows.Controls.Remove(rowToWhack);
NumberOfRowControls--;
}
if(Click != null) Click(this, e);
}
void btn_add_Click(object sender, EventArgs e)
{
var row = NewRow(NumberOfRowControls);
panel_rows.Controls.Add(row);
if(Click != null) Click(this, e);
}
And update the UpdatePanel to be looking for it:
<asp:UpdatePanel ID="UpdatePanelFoo" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<grid:tablegrid ID="tablegrid_chapters" runat="server" SomeProperty="bar" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tablegrid_chapters" EventName="Click">
</Triggers>
</asp:UpdatePanel>

Make sure you add a ScriptManager as well to the page, otherwise there's no UpdatePanel functionality.

Related

ASP.NET UpdatePanel in UserControl doesn't trigger Click event

I have set the following in my ascx file :
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Button ID="from_dispo_to_affich" runat="server" OnClick="from_dispo_to_affich_Click"/>
</ContentTemplate>
</asp:UpdatePanel>
My problem is that the OnClick event doesn't trigger.
You want to make a javascript function call right?
Change OnClick to OnClientClick. And put () after your function.
OnClientClick="from_dispo_to_affich_Click()"
Otherwise put this event in your codebehind
protected void from_dispo_to_affich_Click(object sender, EventArgs e)
{
// do something
}

UpdatePanel with TextBox.OnTextChanged Async Event not firing

I have a textbox for inputting a desired username. When the user types in their name and the input loses focus, I want to send an async postback to check whether their username is already taken. This works fine in the cases that no error is thrown, but I want to handle errors, as well.
Per MSDN, I've added an AsyncPostBackError handler to the ScriptManager. I've tried this both on every page load and only on non-postback page loads.
I have my TextBox in an UpdatePanel like so:
<asp:UpdatePanel ID="upLogin" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox ID="tbLogin" runat="server" placeholder="username"
OnTextChanged="tbLogin_TextChanged" required />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="tbLogin" EventName="TextChanged" />
</Triggers>
</asp:UpdatePanel>
In the code-behind, I have:
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager.GetCurrent(Page).AsyncPostBackError +=
BootstrapLogin_AsyncPostBackError;
}
void BootstrapLogin_AsyncPostBackError(object sender, AsyncPostBackErrorEventArgs e)
{
tbLogin.Attributes.Remove("data-success");
tbLogin.Attributes["data-error-message"] = myErrorMessage;
}
protected void tbLogin_TextChanged(object sender, EventArgs e)
{
throw new Exception("what?");
}
I also added Async="true" to my Default.aspx Page markup.
When I have AutoPostBack="true" on my TextBox, the error is thrown and the AsyncPostBackError handler is never called. For some reason, during the postback, Page.IsAsync is false. I suspect this is the reason (or indication of why) it's not being handled.
When I set AutoPostBack="false", no postback is fired at all, despite the specified trigger.
Ideas?
It looks like following code doesn't work as intended
ScriptManager.GetCurrent(Page).AsyncPostBackError +=
BootstrapLogin_AsyncPostBackError;
Instead of that I just set the OnAsyncPostBackError property of the ScriptManager in markup and it worked with AutoPostback = "True" in your TextBox.
<asp:ScriptManager ID="ScriptManager1" runat="server" OnAsyncPostBackError="BootstrapLogin_AsyncPostBackError"></asp:ScriptManager>
Not sure why your code didn't work. It picks up the correct ScriptManager.
UPDATE
Solved it!
I should refresh my knowledge on Page lifecycle :(
Actually, we should add your code into Page_Init rather than in Page_Load. Following works perfectly.
protected override void OnInit(EventArgs e)
{
base.OnInit(e);
ScriptManager.GetCurrent(Page).AsyncPostBackError +=
BootstrapLogin_AsyncPostBackError;
}
UPDATE 2
See this screen capture
Hope this helped!

ListView in UpdatePanel not Refreshing

I have a ListView that's inside a UpdatePanel, UpdateMode = Conditional. It's a really large Listview, lots of templates, so I'm not showing details.
<asp:UpdatePanel ID="updListView" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:ListView ID="lstvScanPreview" runat="server" OnItemDataBound="lstvScanPreview_ItemDataBound">
...
</ContentTemplate>
</asp:UpdatePanel>
I also have a radiobutonlist outside of the update panel with a OnSelectedIndexChanged of
protected void rgbShowIssues_SelectedIndexChanged(object sender, EventArgs e)
{
if (rgbShowIssues.SelectedIndex == 0)
lstvScanPreview.DataSource = previewData.Data.Where(S => S.IssueType != ScanIssues.None);
else
lstvScanPreview.DataSource = previewData.Data;
updListView.Update();
}
A breakpoint set inside this method does get hit, but the listview doesn't refresh. Other controls also have events with code-behind that call updListView.Update(), and these do work. If I trigger one of these other events after I clicking on the rgbShowIssues radiobuttonlist, then when the update does occur, I can see the changes I would have expected had the UpdatePanel refreshed when expected.
Any ideas why this isn't working? Thanks.

TextBox loses its focus when timer is ticking

I put a timer control on a specific section on my page, but every time the timer is ticking, my text box (I have multiple text boxes) in another section loses it focus.
How can I resolve this? I tried placing the timer in a separate update panel.
the code in timer Tick event is
protected void Timer1_Tick(object sender, EventArgs e)
{
if ((List<AllPostInformation>)Session["AllNewsPostCollection"] != (List<AllPostInformation>)Session["CheckExistData"])
{
if ((List<AllPostInformation>)Session["AllNewsPostCollection"] != null)
{
List<AllPostInformation> o = new List<AllPostInformation>();
o = (List<AllPostInformation>)Session["AllNewsPostCollection"];
rptNews.DataSource = o;
rptNews.DataBind();
}
Session["CheckExistData"] = Session["AllNewsPostCollection"];
}
}
and on asp page
<asp:UpdatePanel runat="server" ID="upTimer">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="5000" />
</ContentTemplate>
</asp:UpdatePanel>
If you look at the post in this link, Focus lost on partial postback with UserControls inside UpdatePanel you will get idea that you need not to put the timer control in the update panel instead put the items that are going to be updated.

asp.net : exclude control in updatepanel from doing async postback

I have placed a user control inside update panel after doing asynchronous postback of page associated js file of that user control is not working so that is there any method to exclude a control from updatepanel in another word i don't want to post that user control.
<asp:UpdatePanel ID="upPnlAnswerList" runat="server">
<ContentTemplate>
// another code that required to placed inside updatepanel
<div id="miancontainer" class="containerr"
<klmsuc:Share ID="shareUserControl" runat="server" />
// another code that required to placed inside updatepanel
</div>
Use a PostBackTrigger to perform exclusion rather than having to specify a large number of includes.
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:LinkButton ID="lnkExport" runat="server" OnClick="lnkExport_Click" Text="Export Data"></asp:LinkButton>
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="lnkExport" />
</Triggers>
</asp:UpdatePanel>
Set UpdateMode=Conditional and provide exclusive Triggers for the UpdatePanel.
See:
http://msdn.microsoft.com/en-us/library/bb386454.aspx
you must add some controls in code behind and in the right event and register it for exclusion(postback) instead and AsyncPostBack which is a ajax call.
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnAdd);
https://stackoverflow.com/a/23036830/184572
protected void grdExpense_RowCreated(object sender, GridViewRowEventArgs e)
{
LinkButton btnAdd = (LinkButton)e.Row.Cells[0].FindControl("btnAdd");
if (btnAdd != null)
{
ScriptManager.GetCurrent(this).RegisterPostBackControl(btnAdd);
}
}
look for another similar page which excludes all the controls in a gridview
http://www.aspsnippets.com/Articles/Assign-PostBack-Trigger-Full-PostBack-for-LinkButton-inside-GridView-within-AJAX-UpdatePanel-in-ASPNet.aspx
private void RegisterPostBackControl()
{
foreach (GridViewRow row in GridView1.Rows)
{
LinkButton lnkFull = row.FindControl("lnkFull") as LinkButton;
ScriptManager.GetCurrent(this).RegisterPostBackControl(lnkFull);
}
}

Resources