locked
Added Dynamic LinkButton doesn't fire while datasouce of Datalist has been set RRS feed

  • Question

  • User-922313987 posted

    Hi Guys,

         I want to create a custom server control which inherits from DataList with linkbuttons to handle paging in VS 2005. But when the datasource of datalist is set, The page navigation linkbuttons don't contain "dopostback()" script and the event doesn't fire . how to deal with it?

     thanks

    <?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>  
    1       [ToolboxData("&lt;{0}:CustomDataList runat=server></{0}:CustomDataList>")]
    2        public class CustomDataList : DataList, INamingContainer
    3        {
    4    
    5            private event EventHandler _onDataListDataBind;
    6            private PagedDataSource pageSource;
    7            private Label ctl_lbl_TotalComments, ctl_lbl_PageNumber;
    8            private TextBox ctl_tbx_PageNumber;
    9            private LinkButton ctl_lbtn_PrevPage, ctl_lbtn_NextPage;
    10   
    11   
    12           [Bindable(true)]
    13           [Category("Data")]
    14           [DefaultValue("")]
    15           [Localizable(true)]
    16           [Browsable(true)]     
    17           public event EventHandler OnDataListDataBind
    18           {
    19               add { _onDataListDataBind += value; }
    20               remove { _onDataListDataBind -= value; }
    21           }
    22     
    23           public override object DataSource
    24           {
    25               get
    26               {
    27                   return base.DataSource;
    28               }
    29               set
    30               {
    31                   if (pageSource == null)
    32                   {
    33                       pageSource = new PagedDataSource();
    34                       pageSource.DataSource =(System.Collections.IEnumerable)value;
    35                       pageSource.AllowPaging = true;
    36                       pageSource.PageSize = 1;
    37                       pageSource.CurrentPageIndex = Convert.ToInt32(ctl_tbx_PageNumber.Text) - 1;
    38                   }
    39                   base.DataSource = pageSource;
    40               }
    41           }
    42   
    43           protected LinkButton PrevPageLinkButton
    44           {
    45               get { EnsureChildControls(); return ctl_lbtn_PrevPage; }
    46           }
    47   
    48           protected LinkButton NextPageLinkButton
    49           {
    50               get { EnsureChildControls(); return ctl_lbtn_NextPage; }
    51           }
    52   
    53           protected TextBox PageNumTextBox
    54           {
    55               get { EnsureChildControls(); return ctl_tbx_PageNumber; }
    56           }
    57   
    58           protected override void OnLoad(EventArgs e)
    59           {
    60               EnsureChildControls();
    61               if (!this.Page.IsPostBack)
    62               {
    63                     //ViewState["PageNumber"] = 1;
    64                     BindData(1);
    65               }
    66           }
    67   
    68           protected override void CreateChildControls()
    69           {
    70               Controls.Clear();
    71   
    72               ctl_lbl_TotalComments = new Label();
    73               ctl_lbl_TotalComments.Text = "0";
    74               Controls.Add(ctl_lbl_TotalComments);
    75   
    76               ctl_lbl_PageNumber = new Label();
    77               ctl_lbl_PageNumber.Text = "1";
    78               Controls.Add(ctl_lbl_PageNumber);
    79   
    80               ctl_lbtn_PrevPage = new LinkButton();
    81               ctl_lbtn_PrevPage.Text = "&lt;<";
    82               ctl_lbtn_PrevPage.CssClass = "linkbold0066ff fontS14";
    83               ctl_lbtn_PrevPage.Click += new EventHandler(ctl_lbtn_PrevPage_Click);
    84               Controls.Add(ctl_lbtn_PrevPage);
    85   
    86               ctl_lbtn_NextPage = new LinkButton();
    87               ctl_lbtn_NextPage.Text = "&gt;>";
    88               ctl_lbtn_NextPage.CssClass = "linkbold0066ff fontS14";
    89               ctl_lbtn_NextPage.Click += new EventHandler(ctl_lbtn_NextPage_Click);
    90               Controls.Add(ctl_lbtn_NextPage);
    91   
    92               ctl_tbx_PageNumber = new TextBox();
    93               ctl_tbx_PageNumber.MaxLength = 3;
    94               ctl_tbx_PageNumber.AutoPostBack = true;
    95               ctl_tbx_PageNumber.Text = "1";
    96               ctl_tbx_PageNumber.CssClass = "pagerinput";
    97               ctl_tbx_PageNumber.TextChanged += new EventHandler(ctl_tbx_PageNumber_TextChanged);
    98               Controls.Add(ctl_tbx_PageNumber);
    99   
    100              ChildControlsCreated = true;
    101              ClearChildControlState();
    102          }
    103  
    104          void ctl_lbtn_NextPage_Click(object sender, EventArgs e)
    105          {
    106              int _nextPageNumber = Convert.ToInt32(ctl_tbx_PageNumber.Text) + 1;
    107              BindData(_nextPageNumber);
    108          }
    109  
    110          void ctl_lbtn_PrevPage_Click(object sender, EventArgs e)
    111          {
    112              int _prevPageNumber = Convert.ToInt32(ctl_tbx_PageNumber.Text) - 1;
    113              BindData(_prevPageNumber);
    114          }
    115  
    116          void ctl_tbx_PageNumber_TextChanged(object sender, EventArgs e)
    117          {
    118              int _customPageNumber = Convert.ToInt32(ctl_tbx_PageNumber.Text);
    119              BindData(_customPageNumber);
    120          }
    121  
    122          private void BindData(int pageNum)
    123          {
    124              if (_onDataListDataBind != null)
    125              {
    126                  _onDataListDataBind(null, new EventArgs());
    127  
    128                  ctl_lbl_TotalComments.Text = pageSource.DataSourceCount.ToString();
    129                  ctl_lbl_PageNumber.Text = pageSource.PageCount.ToString();
    130                  ctl_tbx_PageNumber.Text = pageNum.ToString();
    131  
    132                  if (!pageSource.IsFirstPage)
    133                  {
    134                      ctl_lbtn_PrevPage.Enabled = true;
    135                  }
    136                  else
    137                  {
    138                      ctl_lbtn_PrevPage.Enabled = false;
    139                  }
    140  
    141                  if (!pageSource.IsLastPage)
    142                  {
    143                      ctl_lbtn_NextPage.Enabled = true;
    144                  }
    145                  else
    146                  {
    147                      ctl_lbtn_NextPage.Enabled = false;
    148                  }
    149              }
    150          }
    151  
    152          protected override void Render(HtmlTextWriter writer)
    153          {
    154              RenderContents(writer);
    155              writer.WriteBreak();
    156              ctl_lbl_TotalComments.RenderControl(writer);
    157              writer.Write("Records");
    158              writer.WriteBreak();
    159              PrevPageLinkButton.RenderControl(writer);
    160              PageNumTextBox.RenderControl(writer);
    161              writer.Write(" of ");
    162              ctl_lbl_PageNumber.RenderControl(writer);
    163              NextPageLinkButton.RenderControl(writer);
    164          }
    165      }
    
     
    Monday, March 31, 2008 3:07 AM

All replies

  • User-922313987 posted

    Could any one help me?

    Tuesday, April 1, 2008 9:44 PM