AsyncPostBackTrigger in a Repeater outside UpdatePanel - asp.net

I am trying to get a cbxSupplement trigger updatepanel refresh, but am not sure if I am using a wrong EventName or it is just impossible to do it with CheckBox. If I replace CheckBox with Button, it works fine.
<asp:Repeater ID="repSupplements" runat="server">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="repSupplements" EventName="CheckedChanged" />
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>

Since CheckBox controls inside repeater are not available at design time you should register them with ScriptManager.RegisterAsyncPostBackControl method. This method requires ScriptManager either on page or on master page.
Create a handler for Repeater.OnItemCreated event and there register newly created CheckBox. The code is following (note that CheckBox should have AutoPostBack property set to true):
<asp:Repeater ID="repSupplements" runat="server"
OnItemCreated="repSupplements_ItemCreated">
<ItemTemplate>
<asp:CheckBox runat="server" ID="cbxSupplement" AutoPostBack="True" />
</ItemTemplate>
</asp:Repeater>
<asp:UpdatePanel runat="server" ID="up1">
<Triggers>
</Triggers>
<ContentTemplate>
//Get checked items
</ContentTemplate>
</asp:UpdatePanel>
Codebehind:
protected void repSupplements_ItemCreated(object sender, RepeaterItemEventArgs e)
{
var control = e.Item.FindControl("cbxSupplement");
ScriptManager.GetCurrent(Page).RegisterAsyncPostBackControl(control);
}
This should do what you want.

Related

How to fire TextChanged event without using AutoPostBack property

I have one master page and one web form selected with that master page.
In this web form I take two text boxes and one label.
Code I wrote in C# file [aspx.cs] as follows :
protected void TextBox1_TextChanged(object sender, EventArgs e)
{
Label1.Text = "Welcome " + TextBox1.Text;
}
I don't want to keep AutoPostBack propery because when I press tab button then its value fires and appears to the label so that I don't want AutoPostBack property.
From internet i see the code for updatepanel and scriptmanager.
In updatepanel i write trigger tag but also it is not working.
my ASPX file :
<asp:ScriptManager ID="ScriptManager1" EnablePartialRendering="true" runat="server">
</asp:ScriptManager>
<asp:TextBox ID="TextBox1" runat="server" ontextchanged="TextBox1_TextChanged"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" ontextchanged="TextBox2_TextChanged"></asp:TextBox>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TextBox1" EventName ="TextChanged" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
so how to fire TextChanged event.

Disable Postback for Linkbutton

Im using ASP.NET LinkButton.
I have a LinkButton which calls code behind from onclick:
on aspx:
<asp:LinkButton ID="LinkTest" runat="server" onclick="LinkTest_Click" OnClientClick="return false;" >LinkButton</asp:LinkButton>
on aspx.cs:
protected void LinkTest_Click(object sender, EventArgs e)
{
//code here
}
As you can see I have set OnClientClick="return false;" so that it wont do a postback.
The postback does not happens. But the code behind is not fired.
I want the code behind to fire and I want no postback when the linkbutton is clicked.
How can I do this?
Thanks.
Remove the OnclientClick attribute of the linkbutton. Try using an UpdatePanel like:
<asp:UpdatePanel ID="up1" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="LinkTest" EventName="Click" />
</Triggers>
<ContentTemplate>
<asp:label id="Label1" runat="server" Text="Initial Label" />
</ContentTemplate>
</asp:UpdatePanel>
Also, you will need to add a ScriptManager in your page.
Add the linkbutton inside updatepanel,
remove onclientclick attribute of the linkbutton:
Page:
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="lblName" runat="server" Text="Old Value"></asp:Label>
<br />
<asp:LinkButton ID="lnkbtChange" runat="server" OnClick="lnkbtChange_Click">Change</asp:LinkButton>
</ContentTemplate>
</asp:UpdatePanel>
CodeBehind:
protected void lnkbtChange_Click(object sender, EventArgs e)
{
lblName.Text = "New value";
}

Refresh only an UpdatePanel inside another UpdatePanel

If there is UpdatePanel inside another UpdatePanel, and in the inner UpdatePanel there is a button, I want when clicking this button, only refresh the inner UpdatePanel. how ?
IN the innerupdate panel set the updatemode to conditional and set the outerupdatepanel childrenastriggers property to false. In the inner update panel add a postbacktrigger and set it to the button which will cause the postback. Something like this
<asp:UpdatePanel ID="parentup" runat="server" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:UpdatePanel ID="chidlup" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:Button ID="btn" runat="server" />
</ContentTemplate>
<Triggers>
<asp:PostBackTrigger ControlID="btn" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
#Waqar Janjua is right.
But you don't have to set the ChildrenAsTriggers to false, sometimes it is more convenient to leave it as true.
Set in both of your updatepanels the attribute UpdateMode="Conditional" (leave the ChildrenAsTriggers as its default true). then between the: to the add the trigger to your button as Janjua said:
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn" />
</Triggers>
When the UpdateMode is not conditional each updatepanel would update it.
This code helps you:Here is Source
<asp:ScriptManager ID="script1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="up1" runat="server">
<ContentTemplate>
<asp:Label ID="lblTime" runat="server" ForeColor="Red"></asp:Label>
<asp:Button ID="buttonOuter" runat="server" OnClick="buttonOuter_Click" Text="What is the time?" />
<asp:UpdatePanel ID="up2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Label ID="lblTime2" runat="server" ForeColor="Blue"></asp:Label>
<asp:Button ID="buttonInner" runat="server" OnClick="buttonInner_Click" Text="What is the time?" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="buttonOuter" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
And here is code behinde:
protected void Page_Load(object sender, EventArgs e)
{
}
protected void buttonInner_Click(object sender, EventArgs e)
{
up2.Update();
lblTime2.Text = DateTime.Now.Second.ToString();
}
protected void buttonOuter_Click(object sender, EventArgs e)
{
lblTime.Text = DateTime.Now.Second.ToString();
}
If UP1 is the outer UpdatePanel , and UP2 is the inner, and you want to prevent the outer to be updated with inner's Button(for exp:Btn):
UP1.UpdateMode="Conditional"
UP1.ChildrenAsTriggers= "False"
Also you shouldn't add the Btn as a Trigger in UP1(like The following code)
<asp:UpdatePanel ID="UP1" runat="server" ....>
<ContentTemplate>
....
</ContentTemplate>
<Triggers>
<asp:Trigger ControlID="btn" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>

ASP.NET HyperLink as updatepanel trigger

I want to trigger my updatepanel change when I click on a HyperLink But I get an error saying:
Control with ID 'X' being registered through RegisterAsyncPostBackControl or RegisterPostBackControl must implement either INamingContainer, IPostBackDataHandler, or IPostBackEventHandler.
If I use ASP Button, then everything works correctly
My code:
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel runat="server" id="UpdatePanel" updatemode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger controlid="UpdateButton2" eventname="Click" />
</Triggers>
<ContentTemplate>
<asp:Repeater ID="rptDossiers" runat="server">
<ItemTemplate>
...
</ItemTemplate>
</asp:Repeater>
<asp:HyperLink NavigateUrl="#" runat="server" id="UpdateButton2" onclick="tousLesDossiers_Click">
Tous les Dossiers
</asp:HyperLink>
<%--<asp:Button runat="server" id="UpdateButton2" onclick="tousLesDossiers_Click" text="Update" />--%>
</ContentTemplate>
</asp:UpdatePanel>
Any suggestions?
Thanks
There is no Click event handler that is tied to the Hyperlink control, you have to use LinkButton Instead

Make update panel component change visibility of item outside of the update panel

I have a repeater and the items are editable through an edit button which opens a FormView in edit mode. The formView is initially invisible and the repeater visible. Once edit is pressed the repeater goes invisible then the formview becomes visible with the item to edit.
Once changes have been made the user presses update. This sets the formview invisible and the repeater visible.
The problem is the formview goes invisible but the repeater doesn't become visible. This I think is caused by the fact the formview is within an update panel and the repeater isn't? Only the items in the update panel are being altered on clicking update because it is only a partial page update.
I can't put the repeater within the update panel because there is a requirement that the public view doesn't use javascript.
Does anyone know how I could make the repeater reappear?
<asp:Repeater id="resultsRepeater" runat="server" DataSourceID="vehiclesDataSource" >
<ItemTemplate>
<asp:Label id="makeLabel" runat="server" Text='<%# Eval("Make") %>' />
<asp:Button id="editButton" runat="server" Text="Edit" CommandArgument='<%# Eval("Id") %>' OnClick="EditButton_Click" />
</ItemTemplate>
<asp:Repeater>
<asp:UpdatePanel ID="updatePanel" runat="server">
<ContentTemplate>
<asp:Panel id="insertUpdatePanel" runat="server" Visible="false">
<asp:FormView id="editformview" runat="server" DataKeyNames="Id" Datasourceid="VehiclesEditDataSource" >
<EditItemTemplate>
<uc:VehiclesEdit ID="VehiclesEdit" runat="server" />
<asp:Button id="updateButton" runat="server" OnClick="Update_Click" />
</EditItemTemplate>
</asp:FormView>
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>
protected void EditButton_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = false;
insertUpdatePanel.Visible = true;
}
protected void Update_Click(object sender, EventArgs e)
{
resultsRepeater.Visible = true;
insertUpdatePanel.Visible = false;
}
This might help. I had a similar problem and this worked for me. I simply used ScriptManager to register the button (even iterated by row) as a postback control like this:
ScriptManager.GetCurrent(Page).RegisterPostBackControl(updateButton)
This caused a full postback and allowed me to set the visibility of a panel outside the update panel. I hope it works!
REVISED: Add a PostBackTrigger to your UpdatePanel to force a full post-back when your UpdateButton is clicked. This will hide your UpdatePanel and reveal your Repeater again. See final code below:
For more info refer to: https://stackoverflow.com/questions/2545508/how-do-i-force-full-post-back-from-a-button-within-an-updatepanel
<asp:UpdatePanel ID="updatePanel1" runat="server">
<Triggers>
<asp:PostBackTrigger ControlID="updateButton" />
</Triggers>
<ContentTemplate>
<asp:Panel ID="Panel1" runat="server" Visible="false">
<asp:FormView ID="editformview" runat="server" DataKeyNames="Id" DataSourceID="VehiclesEditDataSource">
<EditItemTemplate>
<uc:vehiclesedit id="VehiclesEdit" runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:Button ID="updateButton" runat="server" OnClick="Update_Click" />
</asp:Panel>
</ContentTemplate>
</asp:UpdatePanel>

Resources