TextBox loses its focus when timer is ticking - asp.net

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.

Related

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
}

AsyncPostBackTrigger of button not firing the custom alert

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.

cant update label using timer

i have a master page which uses script manager and on another page i want to show a digital clock which counts from 0 to 10 minutes.
asp.net code:
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<asp:Timer ID="Timer1" runat="server" Interval="5000" ontick="Timer1_Tick">
</asp:Timer>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<Triggers>
<asp:AsyncPostBackTrigger ControlID="Timer1" EventName="Tick" />
</Triggers>
<ContentTemplate>
<asp:Label ID="Label1" runat="server" Text=""></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
C# code:
int min=0,sec=0;
protected void Timer1_Tick(object sender, EventArgs e)
{
if (sec == 55)
{
min = min + 1;
sec = 0;
}
else
{
sec = sec + 5;
}
Label1.Text = min.ToString() +":"+ sec.ToString();
if(min==10&&sec==0)
{
Timer1.Enabled=false;
Label1.Text = "clock over!";
}
the page starts with 0:5 and doesn't change at all. pls point out if there is an error in d code!
My bet is that it will be your update panel. For debug purposes
Try without the upadte panel at all - does it work then
Put the timer in the update panel and forget about the trigger
In fact point 2 could be done anyway - I'm not sure of the benefit of having it as a postback trigger. It would do just as well with the timer inside
However
Honestly I wouldn't do this. Won't the Tick method perform a postback (albeit partial) just to get the timer display going.
There are lots of freely available components that will run purely on the client side which will be a lot (lot) more efficient e.g.
JQuery Timer
EDIT
Actually I think I've spotted the problem. It is how you record mins and secs. These are in variables and would be reset every postback (Timer tick). Youu would need to store them into something that would perist postback e.g. ViewState
protected void Timer1_Tick(object sender, EventArgs e) {
ViewState["min"] //not int min
ViewState["sec"] //not int sec
//.. you would need to use Convert (or Int32.Parse) to get
//.. then as an int to work with
}
But homestly - still don't do it. Use the JQuery component or similar

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

UpdatePanel wrapped around a user control

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.

Resources