Simple Ajax program needs to update single update panel on a button click does both.
Here is the updated code,
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false"
runat="server">
<ContentTemplate>
<fieldset style="width: 30%">
<legend>Panel - 1 </legend>
<asp:Label ID="label1" runat="server"></asp:Label>
<asp:Button ID="b1" runat="server" OnClick="both" Text="Update Both Pannels" />
<asp:Button ID="b2" runat="server" OnClick="one" Text="Update Single Pannel" />
</fieldset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="b1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<fieldset style="width: 30%">
<legend>Panel - 2 </legend>
<asp:Label ID="label2" runat="server"></asp:Label>
</fieldset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="b2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
Button Click events are as follows,
protected void both(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString();
label2.Text = DateTime.Now.ToLongTimeString();
UpdatePanel2.Update();
}
protected void one(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString();
label2.Text = DateTime.Now.ToLongTimeString();
UpdatePanel2.Update();
}
The output remains same..Thanks in advance...
From Reference to MSDN
If the UpdateMode property is set to Conditional, the UpdatePanel control’s content is updated when one of the following is true:
When the postback is caused by a trigger for that UpdatePanel
control.
When you explicitly call the UpdatePanel control's Update method.
When the UpdatePanel control is nested inside another UpdatePanel
control and the parent panel is updated.
When the ChildrenAsTriggers property is set to true and any child
control of the UpdatePanel control causes a postback. Child controls
of nested UpdatePanel controls do not cause an update to the outer
UpdatePanel control unless they are explicitly defined as triggers
for the parent panel.
So Set ChildrenAsTigger to false set asynchrnous trigger for your first update panel like below.
<asp:UpdatePanel ID="UpdatePanel1" UpdateMode="Conditional" ChildrenAsTriggers="false" runat="server">
<ContentTemplate>
<fieldset style="width:30%">
<legend>Panel - 1
</legend>
<asp:Label ID="label1" runat="server"></asp:Label>
<asp:Button ID="b1" runat="server" OnClick="both" Text="Update Both Pannels" />
<asp:Button ID="b2" runat="server" OnClick="one" Text="Update Single Pannel" />
</fieldset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="b1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional">
<ContentTemplate>
<fieldset style="width:30%">
<legend>Panel - 2
</legend>
<asp:Label ID="label2" runat="server"></asp:Label>
</fieldset>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="b2" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
After that on click of B1 BUTTON update the sceond update panel explicitly...like below
protected void both(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString();
label2.Text = DateTime.Now.ToLongTimeString();
UpdatePanel2.Update();
}
protected void one(object sender, EventArgs e)
{
label1.Text = DateTime.Now.ToLongTimeString();
label2.Text = DateTime.Now.ToLongTimeString();
UpdatePanel2.Update();
}
Related
When I use Button inside Update panel it doesnot fire click event but outside the update panel it works.
here is the code
<asp:UpdatePanel ID="updatePanel2" runat="server" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<asp:Button ID="btnBlock" class="Button" Text="BlockCalls" runat="server"
onclick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" />
</Triggers>
</asp:UpdatePanel>
code for button is
protected void btnBlock_Click(object sender, EventArgs e)
{
CtiWS.CtiWS CtiWS1 = new CtiWS.CtiWS();
Response.Write("<script>alert('"+Convert.ToString(Session["BlockCalls"])+"')</script>");
if (btnBlock.Text == "BlockCalls")
{
btnBlock.Text = "UnBlockCalls";
CtiWS1.BlockCalls("", "", HttpContext.Current.Session["HOSTID"].ToString()); //server block calls
}
else
{
btnBlock.Text = "BlockCalls";
CtiWS1.BlockCalls("", "", HttpContext.Current.Session["HOSTID"].ToString()); //server unblock calls
}
}
Try this
set ChildrenAsTriggers to true and add EventName="Click" in asp:AsyncPostBackTrigger
<asp:UpdatePanel ID="updatePanel2" runat="server" UpdateMode="Conditional"
ChildrenAsTriggers="true">
<ContentTemplate>
<asp:Button ID="btnBlock" class="Button" Text="BlockCalls" runat="server"
onclick="btnBlock_Click" Enabled="True" Width="100px" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btnBlock" EventName="Click"/>
</Triggers>
</asp:UpdatePanel>
This is the code that I have:
<asp:UpdatePanel runat="server" ID="UPnlParent" UpdateMode="Conditional" ChildrenAsTriggers="false">
<ContentTemplate>
<div><h3>title</h3></div>
<asp:UpdatePanel runat="server" ID="UPnlChild" UpdateMode="Conditional">
<ContentTemplate>
<asp:TextBox runat="server" ID="Tb1"></asp:TextBox>
<asp:LinkButton runat="server" ID="Btn1" Text="Create" OnClick="Create" />
</ContentTemplate>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
In the code behind I have a function for an event "Create"
protected void Create(object sender, EventArgs e)
{
string textFromPostBack= Tb1.Text;
//do something...
}
The string is empty.
Thank you for your assistance.
You should add the button as a trigger to the update panel if you want it to trigger a post back:
<asp:UpdatePanel ...>
<ContentTemplate>
blablabla
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Btn1" />
</Triggers>
</asp:UpdatePanel>
More details :Click
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";
}
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>
WebPage.aspx
<asp:Button ID="Button1" runat="server" Text="Submit" onclick="Button1_Click" />
<asp:Timer ID="Timer1" runat="server" Interval="1000"
OnTick="StatusTimer_Tick" Enabled="False" />
<asp:ScriptManager ID="ScriptManager1" runat="server" />
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:Label ID="Label1"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1"/>
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server">
<ContentTemplate>
<asp:Label ID="Label2"></asp:Label>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Button1" Event="Click"/>
</Triggers>
</asp:UpdatePanel>
WebPage.aspx.cs
protected void Timer1_Tick(object sender, EventArgs e)
{
Label1.Text = "Refreshed at : " + DateTime.Now.ToLongTimeString();
}
protected void Button1_Click(object sender, EventArgs e)
{
Timer1.Enabled = true;
//Call some web-service
XMLComparisonService.Service1SoapClient oService = new XMLComparisonService.Service1SoapClient();
oService.XMLComparison();
}
So Button1_Click enables Timer1.
Label1 in UpdatePanel1 should get refreshed every 1 second! (with Label1 printing the current time)
Button1_Click also calls the web-service method "XMLComparison"
But Label1 doesnt refresh after the web-service method "XMLComparison" gets called...
Is anything wrong with my approach?
Regards
-Parag
It looks like you are calling a webservice, but not waiting for a response before updating the page.
Here is an example
http://www.codeproject.com/KB/webservices/WebServiceConsumer.aspx