Thursday 13 September 2012

Timer In Asp.net

Timer is available in asp.net toolbox. Double click it to add your page.
Then add Script Manager in our aspx page. Script Manager is Important for timer.


Design Page


<asp:ScriptManager ID="ScriptManager1" runat="server" >
    </asp:ScriptManager>
      <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
        <asp:Timer ID="Timer1" runat="server" interval="1000" ontick="Timer1_Tick">
        </asp:Timer>
        <asp:Label ID="Label1" runat="server" Text="60"></asp:Label>
        </ContentTemplate>
        </asp:UpdatePanel>


Coading


  protected void Timer1_Tick(object sender, EventArgs e)
    {
        if (Convert.ToString(Label1.Text) != "")
        {
            seconds = int.Parse(Label1.Text);
            min = int.Parse(Label1.Text);
        }
        else
        {
             seconds =Convert.ToInt32(defaultTimer);//set some time count in default timer
        }
        Session["timeout"] = (seconds - 1).ToString();
        if (seconds > 0)
        {

            Label1.Text = (seconds + 1).ToString();

        }
        else
        {
            Timer1.Enabled = false;

        }
    }

Set paging in datalist control in asp.net

Design Page

 <asp:DataList ID="dlPaging" runat="server" CellSpacing="15" RepeatColumns="2" Width="85%">
                        <ItemTemplate>
          <table>
                                <tr>
                                    <td >
                                        <h4>
            <asp:Label ID="Label6" runat="server" Text='<%#Eval("Column Name") %>'></asp:Label></h4>
                                    </td>
                                </tr>
           </table>
<div id="paging">
                     <asp:Label ID="lblCurrpage" runat="server" CssClass="labelpaging"></asp:Label>
                </div>
</ItemTemplate>
</asp:DataList>


Coading 

string m_pagingId;

protected void Page_Load(object sender, EventArgs e)
    {
    if (!string.IsNullOrEmpty(this.Request.QueryString["page"]))
        {
            m_pagingId = this.Request.QueryString["page"];
            Paging();
        }
     }

private void Paging()
    {
        string strQuery = "";
        SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["con"].ConnectionString);
        strQuery = string.Format("Select  Query");

        SqlCommand cmd = new SqlCommand(strQuery, cn);
        SqlDataAdapter adp = new SqlDataAdapter(cmd);
        DataTable DT = new DataTable();
        adp.Fill(DT);

        if (DT.Rows.Count == 0)
        {
            divpage.Visible = false;
            lblError.Text = " No Items to Show";
            return;
        }
        //if (Session["catID"] == null && Session["object"] == null) { return; }

        PagedDataSource pageds = new PagedDataSource();
        pageds.DataSource = DT.DefaultView;
        pageds.AllowPaging = true;
        pageds.PageSize = 2;

        int curpage = 0;

        //when click paging, it sends the page index through query string
        if (m_pagingId != null)
        {
            curpage = Convert.ToInt32(m_pagingId);
        }
        else
        {
            curpage = 1;
        }
        pageds.CurrentPageIndex = curpage - 1;

        //display the current page index setup
        if (curpage == 1 && pageds.DataSourceCount < pageds.PageSize)
            lblCurrpage.Text = "Page:";
        else if (pageds.DataSourceCount == 0)
            lblCurrpage.Text = "No productst";
        else if (curpage > 1 && pageds.DataSourceCount > pageds.PageSize)
            lblCurrpage.Text = "Page: <a href='Current  Page Name.aspx?page=' style='text-decoration:none;' ></a>";
        else
            lblCurrpage.Text = "Page:";

        //set link for each index
        for (int i = 1; i <= pageds.PageCount; i++)
        {
            if (i == curpage)
                lblCurrpage.Text = lblCurrpage.Text + " &nbsp; " + i.ToString();
            else
                lblCurrpage.Text = lblCurrpage.Text + " &nbsp; <a href='Current Page Name.aspx?page=" + i.ToString() + "' style='text-decoration:none;'>" + i.ToString() + "</a>";
        }

        //bind the value in datalist
        dlPaging.DataSource = pageds;
        dlPaging.DataBind();
    }