Category Archives: Timer

[RESOLVED] LinkButton and Event


I have a small problem, namely, i create dynamicly few LinkButtons like this:  

LinkButton lb = new LinkButton();
lb.CommandName = username;
  lb.CommandArgument = username;
  lb.Text = username;
   lb.Command += new CommandEventHandler(lb_Command);

And event code looks like this:

private void hpUser_Command(object sender, CommandEventArgs e)
txtTo.Text = e.CommandArgument.ToString();


So what exactly i want is, after pushing on the linkbutton i want to see the text appear in the textbox from the event of that linkbutton.

But i never get it there :( really strange, if i debug the code, the event handeling does execute, everything works fine, but i dont see the actual text in the textbox

i also tried to put page.databind() in the event it help but the text appears there only after i click on some button, no on other linkbutton but ordinary button.

Can anyone help me, and tell me what i should do , to get the text immidiately visible in the textbox.

I thank you in advance for your help 

Debug and see what's the username. It might be empty and that's why Text is blank. Where are you adding the control in that function?

Check this example

<asp:Panel runat="server" ID="Panel1">
<br />
<asp:Label ID="Label1" runat="server"></asp:Label>
protected void Page_Load(object sender, EventArgs e)
    List<string> list = new List<string>() { "user 1""user 2""user 3""user 4""user 5" };
    foreach (string s in list)
        LinkButton lb = new LinkButton() { CommandName = "Click", CommandArgument = s, Text = s };
        lb.Command += new CommandEventHandler(lb_Command);

protected void lb_Command(object sender, CommandEventArgs e)
    Label1.Text = "You clicked user " + e.CommandArgument.ToString();

server events to client click events in webforms are faked.

when you click on a link button in the browser, it call javascript to fill in 2 hidden fields, one with the name, one with the command arg, then does a form.submit() (postback)

on the server side, during postback event processing, looks for a control with the same UniqueID as the one in the hidden field, and passes it the command arg. the control then fires its click event (or whatever is the correct event). if there is no control with the same name as the value in the hidden field, nothing happens.

this means for dynamic control to fire events, they must be recreated on postback with the same UniqueID, and early enough in the page life cycle to receive the postbackevent data.


Dynamic controls need to be created on every postback.....that is the most common problem for events not behaving properly....check out your code for that...

Yes, all those controls are created ont he page_load, so then i suppose every postback they are recreated, but the think is, the linkbutton i have noticed doenst really do a postback.

  Might it also be the problem that does linkbuttons are created in UpdatePanel? And the textbox is outside of that updatepanel?

Yes, that is probably the reason it is happening.

Controls inside an updatepanel don't do a full postback, instead they only do an ajax call to update the info inside the updatepanel.


Yes, that is probably the reason it is happening.

Controls inside an updatepanel don't do a full postback, instead they only do an ajax call to update the info inside the updatepanel.

Hmm ok, so how could i solve the problem, should i put the textbox in another updatepanel and trigger it when the linkbutton is clicked? but the problem then is that i cant make a trigger thingie cuz i dont know the ID of those linkbuttons.

Is there any ohter suggestions, how to solve this problem? 


Refer here

Thank you altho it wasnt really of much help, cuz i know how to dynamicaly create linkbuttons and such + someone here already even gave the example.

And secondly i solved the problem, i just added that textbox into the updatepanel and in the event ive added updatepanel.update();

Anyway, thank you everyone for your suggestions and help. 

One more small question, so the updatepanel, if it updates no matter what, it does hit everytime page_load, or normally it shouldnt? i just putted a break point at the page load, and yeah, every 5 sec i hit that break point, (5 sec is my timer setted on and is binded as trigger for the updatepanel) 



if it updates no matter what, it does hit everytime page_load, or normally it shouldnt

The updatepanel do a partial postback to server, and it will always trigger the page_load event. But it does refresh the control state inside the updatepanel commonly.

Here's a good article for updatepanel. You can check it:

Hope this can help you.

[RESOLVED] Timer executes Page_Load that should execute javascript function but does not work


I am using a Timer with a 3 minutes interval. I am exeuting the Page_Load event every 3 minutes. I am doing that this way as I have all this code inside an UpdatePanel1 which causes

a partial unnoticed postback to the server. I have a lot of code in the Page_Load event that I haven´t put here.

But what does not work is the code I have in the Page_Load event now.

I am trying to execute the javascript function with this line: body1.Attributes.Add("Onload", "startbk()");


This function should start an imageButton to change image between 2 images. This function do works if I refresh the page so the function itself do works.

But it seems this function does not execute when the Timer executes the Page_Load event, so this is my problem how I can solve this?

System.Timers.Timer t = new System.Timers.Timer();
    protected void Page_Load(object sender, EventArgs e)
        t.Interval = 1000 * 3 * 60; //3 Minutes timer
        t.Enabled = true;
        t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
		 body1.Attributes.Add("Onload", "startbk()");
	catch { }

    private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        Page_Load(this, EventArgs.Empty);

<asp:Timer ID="Timer1" runat="server" ></asp:Timer>
<script type="text/javascript">
    var TimerProps = {
        refTimer: null,
        index: 0

    function startTimer() {
        if (TimerProps.index > 1) {
            TimerProps.index = 0;
            document.getElementById('<%= ImageButton6.ClientID %>').src = "";
            TimerProps.index += 1;

        else {
            document.getElementById('<%= ImageButton6.ClientID %>').src = "";
            TimerProps.index += 1;

    function startbk() {
        TimerProps.refTimer = setInterval("startTimer()", 550)


I wonder if this call from the Page_Load event only work the Very first time the Page_Load event is loading and the HTML code is loading.

I am thinking of that I put "OnLoad" as the first argument in this call.


I might wonder if it perheps is possible to call this javascript function from the Page_Load event every time the Page_Load event is called in any other way

that the one I use below that seems to work only the first time the Page_Load event loads?

body1.Attributes.Add("Onload", "startbk()");



I'm afraid that you can not run a server side timer to control the client function. When the page rendered to client browser, you need to use client timer to raise some client function.

So the code on server side:

 private void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)  
        Page_Load(this, EventArgs.Empty);  

will not fired.

According to your requirement. You need to trigger the function startbk() every 3 minutes.

If you don't want to refresh the page to call the startbk(), you can remove the server timer and modify the javascript code like this:


function startbk() {
        TimerProps.refTimer = setInterval("startTimer()", 550);
        setTimeout("startbk()", 1000 * 3 * 60);  //fired every 3 minutes


protected void Page_Load(object sender, EventArgs e)   
        //t.Interval = 1000 * 3 * 60; //3 Minutes timer   
        //t.Enabled = true;   
        //t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);   
         body1.Attributes.Add("Onload", "startbk()");   
    catch { }   

Hope this can help you.

Image Handler Questions (VB.NET)

I have written a simple Image Handler (ashx) but I'd like to know a few things

1) It seems I have to pass '/Images/' to it when I want that as a mapped path

2) How to do Server.MapPath("Images")? I only can get it similarly if I use the way I have done it but it causes me issues.

3) Right-click SAVE pic has 'file type' ASP.NET Server Page on 2008 R2 server but thumb.aspx (jpg) on Windows 7. Why?

The thumbnail side works very quickly but I also wish to expire the content say after 30 mins or less. How?

<%@ WebHandler Language="VB" Class="Thumb" %>

Imports System
Imports System.Web
Imports System.Drawing
Imports System.Drawing.Imaging

Public Class Thumb : Implements IHttpHandler

    Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
        Dim sPath As String = [String].Empty
        Dim strNoThumb As [String] = context.Request.MapPath(context.Request.ApplicationPath.TrimEnd("/"c) + "/images/nothumb.jpg")
        If context.Request("IPath") IsNot Nothing Then
            sPath = context.Request.MapPath(context.Request("../Images/" & "IPath"))
            sPath = strNoThumb
        End If

        Dim bmpThumb As Bitmap = New Bitmap(sPath)
        If bmpThumb Is Nothing Then
            bmpThumb = New Bitmap(strNoThumb)
        End If

        bmpThumb = DirectCast(bmpThumb.GetThumbnailImage(100, 100, New System.Drawing.Image.GetThumbnailImageAbort(AddressOf ThumbCallback), IntPtr.Zero), Bitmap)
        context.Response.ContentType = "image/Jpeg"
        bmpThumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg)
    End Sub

    Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Return True
        End Get
    End Property

    Public Function ThumbCallback() As Boolean
        Return False
    End Function

End Class

Any help would be wonderful

Thank you in advance



1) It seems I have to pass '/Images/' to it when I want that as a mapped path

It depend on your requirement. If the "/Images/" folder is changeable. I suggest you to use web.config.


    <add key="ImageFolder" value="/Images/" />

In the handler.cs, use:


The advantage of this is that when you changed the image folder, you needn't to modify the handler code and re-compile it.


2) How to do Server.MapPath("Images")? I only can get it similarly if I use the way I have done it but it causes me issues

Use Server.MapPath("~/Images"), this well return a current request application path combine with "/Images" folder.


3) Right-click SAVE pic has 'file type' ASP.NET Server Page on 2008 R2 server but thumb.aspx (jpg) on Windows 7. Why?

Not very sure about this, can you elaborate this issue?


The thumbnail side works very quickly but I also wish to expire the content say after 30 mins or less. How?

Do you mean Cache or Response.Expires = 30? Also you can do a javascript timer in the content page that if time exceed 30 mins then alert user.

If I've misunderstood you. Please let me know.

Hope this can help you.

Hello and thank you for replying

1) Images is a virtual directory. When I use my watermarking code I use Dim strImages As String = Server.MapPath("Images") for example. I then pass the image name to the path making the full correct path. The 'Images' directory is always going to be virtual & will never change



Dim strQuery As String = Response.QueryString("PicId")

Dim strFile as String = Path.Combine(StrImages, strQuery)

But I am having to pass 'PicId=../Images/MyPicHere.jpg'. I wish to remove the '../Images/' being passed in my Http Module. How to change the code to achieve this?

2) You say '~/Images/'... well Images is in the root so it would always be '../Images/'

3) If you understand the SaveDialog where you pass the file types All Files (*.*) |*.* and so on. Pass a pic through the http handler in my original post. Now right-click | Save Image As (IE) which shows the Save Dialog. Filname=Thumb.aspx, File Type= JPG files on Windows 7 but if I do exactly the same thing on Server 2008 R2 Enterprise the File Types say ASP.NET instead of JPG. JPG are registered with MS Paint on both machines.

4) It seems the watermark/thumbnail images are in memory so I wish those to expire (cached memory). Of course I have called Dispose() but references are still locking the real images going through the watermarking process

Really appreciate your response and help

Thanks again

[RESOLVED] Set focus on gridview to turn off timer control

I have a timer control on my gridview that refreshes it at a 3 minute interval, but when a user is updating the gridview it will mess us the update and then they have to start over again.  How can i set some kind of focus event on the gridview to shut off the timer update that refreshes the gridview.


When you say update, you might be setting GridView to edit mode using EditIndex or so. Try disabling the Timer Control in Editing event and enable it back in Updated event.

You can refer this article.

It explains how to enable and disable timer using javascript.

you can disable timer on focus and re-enable when focus is out


[RESOLVED] Making Sure an Input form doesn't time out prematurely



I have an input form that users use to put their information into the database. But it times out before the users are done filling out their information and draws an error. How do I correct this?




do you mean session timeout? If so you can set the timeout, default 20 minutes, to something higher. Or you can make use of ajax to call back every ... minutes to keep the session alive.

For more information about the sessionState element:

Another technique, I used it in the past before there was ajax, is to make use of

<meta http-equiv="refresh" content="600">

This will autopostback your page from html after 600 seconds. Of course you can make up another value for it if needed.

Grz, Kris.

Increase the Session expiry time. By default its 20 minutes.  add this to web.config file <system.web>
        <sessionState timeout="540"/>


Put a timer control on top.. notifying user that page is going to expire.. so HURRY!! lol just kidding