Saturday 31 August 2013

Asp.Net Custom Paging in Datalist Control

                Asp.Net Custom Paging in Datalist Control

Step 1- Datalist Control and Two div as given below 
 <div class="PagingClass" id="Upperpaging" runat="server" align="right">
                                <table width="300px">
                                    <tr>
                                        <td>
                                            <asp:LinkButton ID="lbtnFirst2" BackColor="#333333" SkinID="PageLnk" Text="First"
                                                runat="server" OnClick="lbtnFirst2_Click"></asp:LinkButton>
                                        </td>
                                        <td>
                                            <asp:ImageButton ID="lbtnPrivo2" ImageUrl="~/Images/Prious.png" runat="server" OnClick="lbtnPrivo2_Click">
                                            </asp:ImageButton>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblcurrentpage2" runat="server"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:ImageButton ID="lbtnNext2" ImageUrl="~/Images/Next.png" runat="server" OnClick="lbtnNext2_Click">
                                            </asp:ImageButton>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="lbtnLast2" BackColor="#333333" Text="Last" SkinID="PageLnk" runat="server"
                                                OnClick="lbtnLast2_Click"></asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </div>
                            <asp:DataList ID="dlUnanswered" runat="server" Width="100%" DataKeyNames="AskedBy">
                                <ItemTemplate>
                                    <table width="100%">
                                        <tr>
                                            <td>
                                                <a id="lnkQuestion" href='<%# String.Format("~/CommentReply.aspx?QuestionId={0}", Eval("QuestionId")) %>'
                                                    runat="server" class="question">
                                                    <%# Eval("Questions") %></a>
                                            </td>
                                        </tr>
                                        <tr>
                                            <table width="100%">
                                                <tr>
                                                    <td align="left" style="padding-right: 100px;" width="600px">
                                                        <asp:Label ID="lblAskedby" runat="server" Font-Size="Small" ForeColor="gray" Text="Asked by:"></asp:Label>
                                                        <a id="lnkAskedBy" href='<%# String.Format("~/ViewUserProfile.aspx?ID={0}", Eval("AskedBy")) %>'
                                                            runat="server" class="userlink">
                                                            <%# Eval("Name")%></a>
                                                    </td>
                                                </tr>
                                            </table>
                                        </tr>
                                    </table>
                                    <hr class="line" style="margin-top: 1px" />
                                </ItemTemplate>
                            </asp:DataList>
                            <div class="PagingClass" id="Topppaging" runat="server" align="right">
                                <table width="300px">
                                    <tr>
                                        <td>
                                            <asp:LinkButton ID="lbtnFirst" BackColor="#333333" Text="First" SkinID="PageLnk"
                                                runat="server" OnClick="lbtnFirst_Click"></asp:LinkButton>
                                        </td>
                                        <td>
                                            <asp:ImageButton ID="lbtnPrivo" ImageUrl="~/Images/Prious.png" runat="server" OnClick="lbtnPrivo_Click">
                                            </asp:ImageButton>
                                        </td>
                                        <td>
                                            <asp:Label ID="lblcurrentpage" runat="server"></asp:Label>
                                        </td>
                                        <td>
                                            <asp:ImageButton ID="lbtnNext" ImageUrl="~/Images/Next.png" runat="server" OnClick="lbtnNext_Click">
                                            </asp:ImageButton>
                                        </td>
                                        <td>
                                            <asp:LinkButton ID="lbtnLast" BackColor="#333333" SkinID="PageLnk" Text="Last" runat="server"
                                                OnClick="lbtnLast_Click"></asp:LinkButton>
                                        </td>
                                    </tr>
                                </table>
                            </div>

Step 2 . demo.aspx.cs

 public int CurrentPage
    {
        get
        {
            object obj = this.ViewState["_CurrentPage"];

            if (obj == null)
            {
                return 0;
            }
            else
            {
                return (int)obj;
            }
        }
        set
        {
            this.ViewState["_CurrentPage"] = value;
        }
    }
    protected void lbtnFirst_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnNext_Click(object sender, EventArgs e)
    {
        CurrentPage += 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnPrivo_Click(object sender, EventArgs e)
    {
        CurrentPage -= 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnLast_Click(object sender, EventArgs e)
    {
        CurrentPage = BindUnAnsweredQuestion() - 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnNext2_Click(object sender, ImageClickEventArgs e)
    {
        CurrentPage += 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnPrivo2_Click(object sender, ImageClickEventArgs e)
    {
        CurrentPage -= 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnLast2_Click(object sender, EventArgs e)
    {
        CurrentPage = BindUnAnsweredQuestion() - 1;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);
    }
    protected void lbtnFirst2_Click(object sender, EventArgs e)
    {
        CurrentPage = 0;
        BindUnAnsweredQuestion();
        System.Web.UI.ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "Script", "Selectedtab();", true);

    }

step 3- Method show be int return type
 public int BindUnAnsweredQuestion()
    {
        int count = 0;
        try
        {
            var query = (from obj in dc.SP_GET_UNANSWERED_QUESTION(500)
                         select new
                         {
                             obj.QuestionId,
                             Questions = obj.DETAIL,
                             Name = obj.FIRSTNAME,
                             obj.AskedBy,
                         }).ToList();

            if (query.Count > 0)
            {
                int iPageSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["PageSize"]);
                PagedDataSource objpds = new PagedDataSource();
                objpds.DataSource = query;
                objpds.AllowPaging = true;
                objpds.PageSize = iPageSize;
                objpds.CurrentPageIndex = CurrentPage;
                count = objpds.PageCount;
                lblcurrentpage.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
                lblcurrentpage2.Text = "Page: " + (CurrentPage + 1).ToString() + " of " + objpds.PageCount.ToString();
                ViewState["totalpages"] = objpds.PageCount;
                lbtnFirst.Enabled = !objpds.IsFirstPage;
                lbtnFirst2.Enabled = !objpds.IsFirstPage;
                lbtnPrivo.Enabled = !objpds.IsFirstPage;
                lbtnPrivo2.Enabled = !objpds.IsFirstPage;
                lbtnNext.Enabled = !objpds.IsLastPage;
                lbtnNext2.Enabled = !objpds.IsLastPage;
                lbtnLast.Enabled = !objpds.IsLastPage;
                lbtnLast2.Enabled = !objpds.IsLastPage;
                lbtnFirst.Enabled = !objpds.IsFirstPage;
                lbtnFirst2.Enabled = !objpds.IsFirstPage;
                dlUnanswered.DataSource = objpds;
                dlUnanswered.DataBind();
            }
            return count;
        }
        catch (Exception ex)
        {
            throw ex;
        }


    }

ASP.NET CUSTOM ERROR PAGE

                  ASP.NET CUSTOM ERROR PAGE


There is simple TWO step to create custom error in asp.net using c#

1- create a error.aspx and coding in error.aspx.cs
2- create a Global.asax page and coding in this page inside Application_Error event.


stept 1- error.aspx

  <h4 id="errordetail" style="color: #CC3300" runat="server"></h4>
                            <asp:Literal ID="lit" runat="server"></asp:Literal><br />                            
                            <h4 style="color: #CC3300">Error - Please contact to administrator. </h4>

step 2- error.aspx.cs
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            string strERef = Request.QueryString["ERef"];
            //Create Exception object from application variable
            Exception oException = (Exception)HttpContext.Current.Application[strERef];
            if (HttpContext.Current.IsDebuggingEnabled == true)
            {
                if (oException.InnerException.Message != null)
                {
                    errordetail.InnerText = oException.InnerException.Message.ToString();
                    lit.Text = oException.StackTrace.ToString();
                }
            }
            //Remove application variable
            HttpContext.Current.Application.Remove(strERef);
        }
        catch (Exception ex)
        {

        }
    }
    protected void Page_PreInit(object sender, EventArgs e)
    {
        if (Request.QueryString["aspxerrorpath"] != null)
        {
            if (Request.QueryString["aspxerrorpath"].Contains("Modal"))
            {
                this.MasterPageFile = "~/MasterPage/TelerikModel.master";
            }
        }
    }

step 3- global.asax


void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs
       // Server.Transfer("~/Error.aspx", true);

        string strERef = Guid.NewGuid().ToString();
        Exception objError = Server.GetLastError();
        Application.Add(strERef, objError);
        Server.ClearError();
        Response.Redirect(string.Format("~/Error.aspx?aspxerrorpath={0}&ERef={1}", HttpUtility.UrlEncode(Request.RawUrl), strERef), true);
    }

Wednesday 7 August 2013

ALPHABETIC PAGING USING ALPHABETICAL PAGER IN ASP.NET GRIDVIEW



ALPHABETIC PAGING USING ALPHABETICAL PAGER IN ASP.NET GRIDVIEW 




<div class="AlphabetPager">
    <asp:Repeater ID="rptAlphabets" runat="server">
        <ItemTemplate>
            <asp:LinkButton runat="server" Text='<%#Eval("Value")%>' Visible='<%#!Convert.ToBoolean(Eval("Selected"))%>'
                OnClick="Alphabet_Click" />
            <asp:Label runat="server" Text='<%#Eval("Value")%>' Visible='<%#Convert.ToBoolean(Eval("Selected"))%>' />
        </ItemTemplate>
    </asp:Repeater>
</div>
<br />
<asp:GridView ID="GridView1" HeaderStyle-BackColor="#3AC0F2" HeaderStyle-ForeColor="White"
    RowStyle-BackColor="#A1DCF2" AlternatingRowStyle-BackColor="White" AlternatingRowStyle-ForeColor="#000"
    runat="server" AutoGenerateColumns="false" PageSize="5" AllowPaging="true"OnPageIndexChanging="OnPageIndexChanging">
    <Columns>
        <asp:BoundField DataField="ContactName" HeaderText="Contact Name" HeaderStyle-Width="150px" />
        <asp:BoundField DataField="CompanyName" HeaderText="Company Name" HeaderStyle-Width="150px" />
        <asp:BoundField DataField="City" HeaderText="City" HeaderStyle-Width="100px" />
        <asp:BoundField DataField="Country" HeaderText="Country" HeaderStyle-Width="100px" />
        <asp:BoundField DataField="PostalCode" HeaderText="Postal Code" HeaderStyle-Width="100px" />
    </Columns>
    <EmptyDataTemplate>
        <table cellspacing="0" rules="all" border="0" style="border-collapse: collapse;">
            <tr style="color: White; background-color: #3AC0F2;">
                <th scope="col" style="width: 150px;">
                    Contact Name
                </th>
                <th scope="col" style="width: 150px;">
                    Company Name
                </th>
                <th scope="col" style="width: 100px;">
                    City
                </th>
                <th scope="col" style="width: 100px;">
                    Country
                </th>
                <th scope="col" style="width: 100px;">
                    Postal Code
                </th>
            </tr>
            <tr>
                <td colspan="99" align = "center">
                    No records found for the search criteria.
                </td>
            </tr>
        </table>
    </EmptyDataTemplate>
</asp:GridView>

using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Collections.Generic;



protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
    {
        ViewState["CurrentAlphabet"] = "ALL";
        this.GenerateAlphabets();
        this.BindGrid();
    }
}
 
private void BindGrid()
{
    string strConnString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(strConnString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers WHERE ContactName LIKE @Alphabet + '%' OR @Alphabet = 'ALL'"))
        {
            using (SqlDataAdapter sda = new SqlDataAdapter())
            {
                cmd.Connection = con;
                cmd.Parameters.AddWithValue("@Alphabet", ViewState["CurrentAlphabet"]);
                sda.SelectCommand = cmd;
                using (DataTable dt = new DataTable())
                {
                    sda.Fill(dt);
                    GridView1.DataSource = dt;
                    GridView1.DataBind();
                }
            }
        }
    }
}
 
private void GenerateAlphabets()
{
    List<ListItem> alphabets = new List<ListItem>();
    ListItem alphabet = new ListItem();
    alphabet.Value = "ALL";
    alphabet.Selected = alphabet.Value.Equals(ViewState["CurrentAlphabet"]);
    alphabets.Add(alphabet);
    for (int i = 65; i <= 90; i++)
    {
        alphabet = new ListItem();
        alphabet.Value = Char.ConvertFromUtf32(i);
        alphabet.Selected = alphabet.Value.Equals(ViewState["CurrentAlphabet"]);
        alphabets.Add(alphabet);
    }
    rptAlphabets.DataSource = alphabets;
    rptAlphabets.DataBind();
}


protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridView1.PageIndex = e.NewPageIndex;
    this.BindGrid();
}




protected void Alphabet_Click(object sender, EventArgs e)
{
    LinkButton lnkAlphabet = (LinkButton)sender;
    ViewState["CurrentAlphabet"] = lnkAlphabet.Text;
    this.GenerateAlphabets();
    GridView1.PageIndex = 0;
    this.BindGrid();
}
<style type="text/css">
        body
        {
            font-family: Arial;
            font-size: 10pt;
        }
        .AlphabetPager a, .AlphabetPager span
        {
            font-size: 8pt;
            display: inline-block;
            height: 15px;
            line-height: 15px;
            min-width: 15px;
            text-align: center;
            text-decoration: none;
            font-weight: bold;
            padding: 0 1px 0 1px;
        }
        .AlphabetPager a
        {
            background-color: #f5f5f5;
            color: #969696;
            border: 1px solid #969696;
        }
        .AlphabetPager span
        {
            background-color: #A1DCF2;
            color: #000;
            border: 1px solid #3AC0F2;
        }
    </style>


Alphabet Paging using Alphabetical Pager in ASP.Net GridView