Button click not working inside update panel - asp.net

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>

Related

UpdatePanel AsyncPostbackTrigger not firing

I've got a little problem, any help is appreciated.
Situation looks like this:
<asp:UpdatePanel ID="UP_Panel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlSwitch" runat="server" Width="250px" AutoPostback="true"
OnSelectedIndexChanged="ddlSwitch_SelectedIndexChanged">
<asp:ListItem Value="continent" Text="Continent"></asp:ListItem>
<asp:ListItem Value="region" Text="Region"></asp:ListItem>
<asp:ListItem Value="country" Text="Country"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="UP_Switch" runat="server" UpdateMode="Conditional">
<ContentTemplate>
// Some content
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlSwitch" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
When ddlSwitch selected index change I want to update content inside UP_Switch, but SelectedIndexChanged event doesn't seem to fire. What am I doing wrong?
Thanks in advance!
I have just tested your code and seems to be working with some test modifications: ASPX
<asp:UpdatePanel ID="UP_Panel" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:DropDownList ID="ddlSwitch" runat="server" Width="250px" AutoPostback="true"
OnSelectedIndexChanged="ddlSwitch_SelectedIndexChanged">
<asp:ListItem Value="continent" Text="Continent"></asp:ListItem>
<asp:ListItem Value="region" Text="Region"></asp:ListItem>
<asp:ListItem Value="country" Text="Country"></asp:ListItem>
</asp:DropDownList>
<asp:UpdatePanel ID="UP_Switch" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Literal ID="litUpSwitch" runat="server"></asp:Literal>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="ddlSwitch" EventName="SelectedIndexChanged" />
</Triggers>
</asp:UpdatePanel>
</ContentTemplate>
</asp:UpdatePanel>
Code behind for ddlSwitch_SelectedIndexChanged:
protected void ddlSwitch_SelectedIndexChanged(object sender, EventArgs e)
{
litUpSwitch.Text = "DDL Switch go activated";
}
Result:

How to avoid timer reset in asp.net/ajax

I am using updatepanel and timer to display an alert every 15 seconds. It is working fine when I am not clicking anything in the page. It displays alert every 15 seconds. I have a button outside of this updatepanel. Whenever I click this button, the timer resets and it doesn't display alert every 15 seconds. If I stops clicking the button, it starts to display the alert after 15 seconds. BAsically, timer resets the interval when ever I click teh button. I want to display the alert regardless clicking a button or not. Please help me.
in ASPX page
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional"
ViewStateMode="Enabled">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:Timer ID="Timer1" runat="server" Interval="15000" OnTick="Timer1_Tick">
</asp:Timer>
In .CS page
public void Timer1_Tick(object sender, EventArgs e)
{
ScriptManager.RegisterClientScriptBlock(UpdatePanel1, typeof(Page), "ToggleScript", "
alert('Hello')", true);
}
Reason looks lik your button do a post back and your timer in out side the update panel so it will reset.
if you can place your button in to another update panel this will work.
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional" ViewStateMode="Enabled">
<ContentTemplate>
<asp:Timer ID="Timer1" runat="server" Interval="10000" OnTick="Timer1_Tick">
</asp:Timer>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
</asp:UpdatePanel>
<asp:UpdatePanel ID="UpdatePanel2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>

TextBox.Text returned empty after postback from nested 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

Both Updatepanel's getting update, while only 1 have to work

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();
}

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>

Resources