AsyncPostBackTrigger of button not firing the custom alert - asp.net

Hi all I am having a button on my form for which I have used an updatepanel with AsyncPostBackTrigger every thing works fine but what ever code I have written to show a custom alert is getting executed but the alert is not firing can some one help me
<asp:UpdatePanel ID="updatePanel" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="button1" EventName="Click" />
</Triggers>
</asp:UpdatePanel>
protected void button1_Click(object sender, EventArgs e)
{
if(somecondition)
{
// code
}
else
{
this.ClientScript.RegisterClientScriptBlock(.....);
}
}

When using Update panel, you should use ScriptManager.RegisterStartupScript
ScriptManager.RegisterStartupScript(updatePanelId,updatePanelId.GetType()
, "key_name", "alert('Hello');", true)
Check this link for complete explanation/details.

Related

Timer1_tick is not working until postback event

As per topic in discussed. I've assigned one Timer1_Tick (AJAx Control) into my web form user control but it is not working right until i've perform "__doPostBack" in client side and it is not be able to perform automatically once the form is loaded.
Code in markup:
<asp:UpdatePanel ID="TimeSheetUpdate" runat="server" UpdateMode="Conditional">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="TimeSheetTimer" EventName="Tick"/>
</Triggers>
<ContentTemplate>
<asp:Timer ID="TimeSheetTimer" Interval="1000" runat="server" OnTick="TimeSheetTimer_Tick" ClientIDMode="Static"></asp:Timer>
</ContentTemplate>
</asp:UpdatePanel>
Code Behind:
protected void TimeSheetTimer_Tick(object sender, EventArgs e)
{
File.WriteAllText(#"C:\checkTimesheetcontrol.txt", "");
File.AppendAllText(#"C:\checkTimesheetcontrol.txt", "Check :" + DateTime.Now.Second);
return;
}
I need it to be perform ealier once the control in loaded in page.How Can I do for it?

How can i refresh the UI using Component Art Callback in C# Asp.net

once the page is loaded if we need to add something on the UI then how we will do it by using callback without loading the whole page again.
You can use UpdatePanel to do this i.e. without refreshing your whole page it can change components of your page.
<asp:UpdatePanel ID="UpdatePanelMain" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="BtnClick" runat="server" OnClick="BtnClick_Click" />
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="BtnClick" EventName="Click" />
</Triggers>
Now you will get call in code behind whenever "BtnClick" button is clicked. There you can write your code.
protected void BtnClick_Click(object sender, EventArgs e)
{
// Your logic
}

How to make UpdatePanel to do Update after custom Event?

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"

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.

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)
{
}

Resources