dynamically usercontrol Paging Issue

4 posts, 0 answers
  1. Prasanth100
    Prasanth100 avatar
    17 posts
    Member since:
    Oct 2012

    Posted 28 Feb 2014 Link to this post

    Hi Gurus,

      I am dynamically calling a usercontrol,

      my issue is,when i click the paging button,the control is not loading (i need to use custom paging not built in paging)

    Here is the code:

    Default.aspx

    ------------------

    <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="DynamicControls._Default" %>

    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>
    <asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
    </asp:Content>
    <asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <asp:ScriptManager ID="Sc1" runat="server">
    </asp:ScriptManager>
    <table>
    <tr>
    <td>
    <telerik:RadTabStrip runat="server" ID="rdTabMessages" Skin="MetroTouch" EnableEmbeddedSkins="False"
    CssClass="Tabcontrols_left" MultiPageID="RadMultiPage1" Orientation="VerticalLeft"
    SelectedIndex="0" OnTabClick="tabClick">
    <Tabs>
    <telerik:RadTab TabIndex="0" CssClass="appointments" Text="Control1">
    </telerik:RadTab>
    <telerik:RadTab TabIndex="1" CssClass="appointments" Text="Control2">
    </telerik:RadTab>
    <telerik:RadTab TabIndex="2" CssClass="appointments" Text="Control3">
    </telerik:RadTab>
    </Tabs>
    </telerik:RadTabStrip>
    </td>
    <td>
    <telerik:RadMultiPage runat="server" ID="RadMultiPage1" SelectedIndex="0" CssClass="Tacontrols_right">
    <telerik:RadPageView runat="server" ID="RadPageView1">
    <asp:Panel ID="pnlConfiguration" runat="server">
    This is Control1
    </asp:Panel>
    </telerik:RadPageView>
    <telerik:RadPageView runat="server" ID="RadPageView2">
    <asp:Panel ID="pnlWebPages" runat="server">
    This is Control2
    </asp:Panel>
    </telerik:RadPageView>
    <telerik:RadPageView runat="server" ID="RadPageView3">
    <asp:Panel ID="pnlTestimonial" runat="server">
    </asp:Panel>
    </telerik:RadPageView>
    </telerik:RadMultiPage>
    </td>
    </tr>
    </table>
    </asp:Content>



    CodeBehind

    ----------------

    protected void tabClick(object sender, RadTabStripEventArgs e)
    {
    if (e.Tab.TabIndex == 0)
    {
    //e.Tab.Selected = true;
    }
    if (e.Tab.TabIndex == 1)
    {

    }
    if (e.Tab.TabIndex == 2)
    {

    pnlTestimonial.Controls.Clear();
    string controlPath = "~/Controls/RadSchedulerCS.ascx";
    RadSchedulerCS uc = (RadSchedulerCS)LoadControl(controlPath);
    uc.ID = "RadScheduler";
    uc.BindGrid(1);
    pnlTestimonial.Controls.Add(uc);

    //if (uc != null)
    //{
    // DataList dlPager = (DataList)uc.FindControl("dlPager");
    // if (dlPager != null)
    // {
    // LinkButton lnkPageNo = (LinkButton)dlPager.FindControl("lnkPageNo");
    // dlPager.ItemCommand += new DataListCommandEventHandler(dlPager_ItemCommand);

    // }
    //}

    }


    }

    protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
    {

    pnlTestimonial.Controls.Clear();
    string controlPath = "~/Controls/RadSchedulerCS.ascx";
    RadSchedulerCS uc = (RadSchedulerCS)LoadControl(controlPath);
    uc.ID = "RadScheduler";
    uc.BindGrid(1);
    pnlTestimonial.Controls.Add(uc);

    }



    RadSchedulerCS.ascx

    ---------------------------

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="RadSchedulerCS.ascx.cs" Inherits="DynamicControls.Controls.RadSchedulerCS" %>
    <%@ Register TagPrefix="telerik" Namespace="Telerik.Web.UI" Assembly="Telerik.Web.UI" %>



    <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false">
    <Columns>
    <asp:TemplateField HeaderText="ID">
    <ItemTemplate>
    <asp:Label ID="lblID" runat="server" Text='<%#Eval("ID") %>' Visible="true"></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>
    <asp:TemplateField HeaderText="Name">
    <ItemTemplate>
    <asp:Label ID="lblName" runat="server" Text='<%# Eval("FName") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    <asp:TemplateField HeaderText="DOB">
    <ItemTemplate>
    <asp:Label ID="lblDOB" runat="server" Text='<%# Eval("DOB") %>'></asp:Label>
    </ItemTemplate>
    </asp:TemplateField>

    </Columns>
    </asp:GridView>
    <asp:DataList CellPadding="5" RepeatDirection="Horizontal" runat="server" ID="dlPager"
    OnItemCommand="dlPager_ItemCommand" >
    <ItemTemplate>
    <asp:LinkButton Enabled='<%#Eval("Enabled") %>' runat="server" ID="lnkPageNo" Text='<%#Eval("Text") %>'
    CommandArgument='<%#Eval("Value") %>' CommandName="PageNo" CausesValidation="false"></asp:LinkButton>
    </ItemTemplate>
    </asp:DataList>



    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    namespace DynamicControls.Controls
    {
    public partial class RadSchedulerCS : System.Web.UI.UserControl
    {
    #region DECLRATIONS
    public delegate void MyCustomHandler(object sender, EventArgs e); //This is for button click
    public event MyCustomHandler SomethingClicked;

    #endregion


    protected void Page_PreRender(object sender, EventArgs e)
    {
    // Workaround to prevent clicking twice on the pager to have results displayed properly
    this.Gv1.DataBind();
    }
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGrid(1);
    }
    }
    public void BindGrid(int currentPage)
    {
    List<Employee> empList = new List<Employee>();
    empList.Add(new Employee() { ID = 1, FName = "John", DOB = DateTime.Parse("12/11/1971") });
    empList.Add(new Employee() { ID = 2, FName = "Mary", DOB = DateTime.Parse("01/17/1961") });
    empList.Add(new Employee() { ID = 3, FName = "Amber", DOB = DateTime.Parse("12/23/1971") });
    empList.Add(new Employee() { ID = 4, FName = "Kathy", DOB = DateTime.Parse("11/15/1976") });
    empList.Add(new Employee() { ID = 5, FName = "Lena", DOB = DateTime.Parse("05/11/1978") });
    empList.Add(new Employee() { ID = 6, FName = "John1", DOB = DateTime.Parse("12/11/1971") });
    empList.Add(new Employee() { ID = 7, FName = "Mary1", DOB = DateTime.Parse("01/17/1961") });
    empList.Add(new Employee() { ID = 8, FName = "Amber1", DOB = DateTime.Parse("12/23/1971") });
    empList.Add(new Employee() { ID = 9, FName = "Kathy1", DOB = DateTime.Parse("11/15/1976") });
    empList.Add(new Employee() { ID = 10, FName = "Lena1", DOB = DateTime.Parse("05/11/1978") });
    empList.Add(new Employee() { ID = 11, FName = "John2", DOB = DateTime.Parse("12/11/1971") });

    int TotalCount = empList.Count();

    var pgNo = currentPage;
    var pgRec = 10;
    empList = empList.Skip((pgNo - 1) * pgRec).Take(pgRec).ToList();
    Gv1.DataSource = empList;
    Gv1.DataBind();
    generatePager(TotalCount, pgRec, pgNo);
    }



    public void generatePager(int totalRowCount, int pageSize, int currentPage)
    {
    int totalLinkInPage = 3;
    int totalPageCount = (int)Math.Ceiling((decimal)totalRowCount / pageSize);
    int startPageLink = Math.Max(currentPage - (int)Math.Floor((decimal)totalLinkInPage / 2), 1);
    int lastPageLink = Math.Min(startPageLink + totalLinkInPage - 1, totalPageCount);
    if ((startPageLink + totalLinkInPage - 1) > totalPageCount)
    {
    lastPageLink = Math.Min(currentPage + (int)Math.Floor((decimal)totalLinkInPage / 2), totalPageCount);
    startPageLink = Math.Max(lastPageLink - totalLinkInPage + 1, 1);
    }
    List<ListItem> pageLinkContainer = new List<ListItem>();

    if (startPageLink != 1)
    {
    int prevcounts = currentPage - 1;
    pageLinkContainer.Add(new ListItem("First", prevcounts.ToString(), currentPage != 1));
    }
    for (int i = startPageLink; i <= lastPageLink; i++)
    {
    pageLinkContainer.Add(new ListItem(i.ToString(), i.ToString(), currentPage != i));
    }
    if (lastPageLink != totalPageCount)
    {
    int Nextcounts = currentPage + 1;
    pageLinkContainer.Add(new ListItem("Last", Nextcounts.ToString(), currentPage != totalPageCount));
    }

    dlPager.DataSource = pageLinkContainer;
    dlPager.DataBind();
    }
    protected void dlPager_ItemCommand(object source, DataListCommandEventArgs e)
    {

    if (SomethingClicked != null)
    {
    SomethingClicked(source, e);
    if (e.CommandName == "PageNo")
    {
    BindGrid(Convert.ToInt32(e.CommandArgument));
    }
    }

    }
    class Employee
    {
    public int ID { get; set; }
    public string FName { get; set; }
    public DateTime DOB { get; set; }
    }

    }
    }





    I need it urgent,how to accomplish this,is any possibilities by using Delegates or any wayz,if so how to do that.

    Help me with this.



    Thanks in Advance





  2. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 05 Mar 2014 Link to this post

    Hello,

    Please find attached a modified version of your project. I added your user control in the Page_Load server-side event handler of your content page. Your custom handler SomethingClicked is always null so the highlighted line of code is never executed:
    //code behind
    if (SomethingClicked != null)
            {
                SomethingClicked(source, e);
                if (e.CommandName == "PageNo")
                {
                    BindGrid(Convert.ToInt32(e.CommandArgument));
                }
            }



    Regards,
    Boyan Dimitrov
    Telerik

    DevCraft Q1'14 is here! Join the free online conference to see how this release solves your top-5 .NET challenges. Reserve your seat now!

  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Prasanth100
    Prasanth100 avatar
    17 posts
    Member since:
    Oct 2012

    Posted 05 Mar 2014 in reply to Boyan Dimitrov Link to this post

    Hi Boyan Dimitrov,
     
        Thanks for the reply,it worked fine, i have another issue.
     I am using Update Panel in the webpage, now the javascript inside usercontrol is not working,
    please suggest me to do this:

    Here is the UserControl Code:


    <script type="text/javascript">

        function ShowInsertForm() {
            window.radopen("EditUserDetail.aspx", "UserListDialog");
            return false;
        }

        function GetSelectedRow(lnk) {
            alert(lnk);
        }
     </script>
     
     
     

    <asp:GridView ID="Gv1" runat="server" AutoGenerateColumns="false" AllowPaging="true" OnRowDataBound="RadGrid1_ItemCreated">
        <Columns>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="lblID" runat="server"  Visible="true"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name">
                <ItemTemplate>
                    <asp:Label ID="lblName" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="DOB">
                <ItemTemplate>
                    <asp:Label ID="lblDOB" runat="server"></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

            <asp:TemplateField HeaderText="DOB">
                <ItemTemplate>
                    <asp:HyperLink ID="EditLink" runat="server" Text="Add"></asp:HyperLink>
                </ItemTemplate>
            </asp:TemplateField>
            
        </Columns>
    </asp:GridView>
     
     <telerik:RadWindowManager ID="RadWindowManager1" runat="server" EnableShadow="true">
            <Windows>
                <telerik:RadWindow ID="UserListDialog" runat="server" Title="Editing record" Height="320px"
                    Width="310px" Left="150px" ReloadOnShow="true" ShowContentDuringLoad="false"
                    Modal="true">
                </telerik:RadWindow>
            </Windows>
        </telerik:RadWindowManager>


    code behind
    --------------------
     protected void RadGrid1_ItemCreated(object sender, GridViewRowEventArgs e)
            {
                if (e.Row.RowType == DataControlRowType.DataRow)
                {
                    Employee row = (Employee)e.Row.DataItem;
                    if (row != null)
                    {
                        Label lblID = (Label)e.Row.FindControl("lblID");
                        Label lblName = (Label)e.Row.FindControl("lblName");
                        Label lblDOB = (Label)e.Row.FindControl("lblDOB");
                        lblID.Text = row.ID.ToString();
                        lblName.Text = row.FName;
                        lblDOB.Text = row.DOB.ToString();

                        HyperLink editLink = (HyperLink)e.Row.FindControl("EditLink");
                        editLink.Attributes["href"] = "javascript:void(0);";
                        editLink.Attributes["onclick"] = String.Format("return ShowInsertForm();");
                      
                    }
                }
            }

    Thanks in Advance










  5. Boyan Dimitrov
    Admin
    Boyan Dimitrov avatar
    1746 posts

    Posted 10 Mar 2014 Link to this post

    Hello,

    Please find attached a sample project that implements very similar scenario and the JavaScript is executed as expected.


    Regards,
    Boyan Dimitrov
    Telerik
     

    DevCraft Q1'14 is here! Watch the online conference to see how this release solves your top-5 .NET challenges. Watch on demand now.

     
Back to Top