Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. Web Development
  3. ASP.NET
  4. Binding Controls via Generic Method

Binding Controls via Generic Method

Scheduled Pinned Locked Moved ASP.NET
helpcssdatabasewpfwcf
7 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • O Offline
    O Offline
    overtech06
    wrote on last edited by
    #1

    I have a few web applications that I maintain and I find myself very often writing the same block of code over and over again to bind a GridView to a data source. I'm trying to create a Generic method to handle data binding but I'm having trouble getting it to work with Repeaters and DataLists. Here is the Generic method I have so far:

        public void BindControl<T>(T control, SqlCommand sql) where T : System.Web.UI.WebControls.BaseDataBoundControl
        {
            cmd = sql;
            cn.Open();
            SqlDataReader dr = cmd.ExecuteReader();
            if (dr.HasRows)
            {
                control.DataSource = dr;
                control.DataBind();
            }
            dr.Close();
            cn.Close();
        }
    

    That way I can just define my CommandText then make a call to "BindControls(myGridView, cmd)" instead of retyping this same basic block of code every time I need to bind a grid. The problem is, this doesn't work with Repeaters or DataLists. The error shown says "There is no implicit reference conversion from 'System.Web.UI.WebControls.Repeater' to 'System.Web.UI.WebControls.BaseDataBoundControl'." I can't seem to find the right base class that is in common with all 3 controls. For now I have overloaded the method specifically for Repeaters and DataLists, but does anyone know what I could use instead of "BaseDataBoundControl" that would work for all three control types? - Dave

    T D 2 Replies Last reply
    0
    • O overtech06

      I have a few web applications that I maintain and I find myself very often writing the same block of code over and over again to bind a GridView to a data source. I'm trying to create a Generic method to handle data binding but I'm having trouble getting it to work with Repeaters and DataLists. Here is the Generic method I have so far:

          public void BindControl<T>(T control, SqlCommand sql) where T : System.Web.UI.WebControls.BaseDataBoundControl
          {
              cmd = sql;
              cn.Open();
              SqlDataReader dr = cmd.ExecuteReader();
              if (dr.HasRows)
              {
                  control.DataSource = dr;
                  control.DataBind();
              }
              dr.Close();
              cn.Close();
          }
      

      That way I can just define my CommandText then make a call to "BindControls(myGridView, cmd)" instead of retyping this same basic block of code every time I need to bind a grid. The problem is, this doesn't work with Repeaters or DataLists. The error shown says "There is no implicit reference conversion from 'System.Web.UI.WebControls.Repeater' to 'System.Web.UI.WebControls.BaseDataBoundControl'." I can't seem to find the right base class that is in common with all 3 controls. For now I have overloaded the method specifically for Repeaters and DataLists, but does anyone know what I could use instead of "BaseDataBoundControl" that would work for all three control types? - Dave

      T Offline
      T Offline
      T M Gray
      wrote on last edited by
      #2

      Try implementing an Interface.

      O 1 Reply Last reply
      0
      • T T M Gray

        Try implementing an Interface.

        O Offline
        O Offline
        overtech06
        wrote on last edited by
        #3

        I think that might be a little outside of my comfort zone, but I'll do some research on it. It might be a good learning experience if nothing else... I was really hoping there would be some base class that I had just overlooked that would be inclusive of all three control types... - Dave

        1 Reply Last reply
        0
        • O overtech06

          I have a few web applications that I maintain and I find myself very often writing the same block of code over and over again to bind a GridView to a data source. I'm trying to create a Generic method to handle data binding but I'm having trouble getting it to work with Repeaters and DataLists. Here is the Generic method I have so far:

              public void BindControl<T>(T control, SqlCommand sql) where T : System.Web.UI.WebControls.BaseDataBoundControl
              {
                  cmd = sql;
                  cn.Open();
                  SqlDataReader dr = cmd.ExecuteReader();
                  if (dr.HasRows)
                  {
                      control.DataSource = dr;
                      control.DataBind();
                  }
                  dr.Close();
                  cn.Close();
              }
          

          That way I can just define my CommandText then make a call to "BindControls(myGridView, cmd)" instead of retyping this same basic block of code every time I need to bind a grid. The problem is, this doesn't work with Repeaters or DataLists. The error shown says "There is no implicit reference conversion from 'System.Web.UI.WebControls.Repeater' to 'System.Web.UI.WebControls.BaseDataBoundControl'." I can't seem to find the right base class that is in common with all 3 controls. For now I have overloaded the method specifically for Repeaters and DataLists, but does anyone know what I could use instead of "BaseDataBoundControl" that would work for all three control types? - Dave

          D Offline
          D Offline
          daveyerwin
          wrote on last edited by
          #4

          You must "overload" your method. In words, you write a method with the same name several times each taking differrent args, something like

          public void inABind(Gridview gv, DataSet ds)
          {Put you code here}
          public void inABind(DropDownList ddl, dataset ds)
          {Put you code here}
          public void inABind(WhateverControl wc, WhateverDataSource weds)
          and on and on like that till
          you cover all bases

          O 1 Reply Last reply
          0
          • D daveyerwin

            You must "overload" your method. In words, you write a method with the same name several times each taking differrent args, something like

            public void inABind(Gridview gv, DataSet ds)
            {Put you code here}
            public void inABind(DropDownList ddl, dataset ds)
            {Put you code here}
            public void inABind(WhateverControl wc, WhateverDataSource weds)
            and on and on like that till
            you cover all bases

            O Offline
            O Offline
            overtech06
            wrote on last edited by
            #5

            That's what I've already done in my current solution. I was just looking for a common interface that would allow me to cover them all in one Generic method... - Dave

            D 2 Replies Last reply
            0
            • O overtech06

              That's what I've already done in my current solution. I was just looking for a common interface that would allow me to cover them all in one Generic method... - Dave

              D Offline
              D Offline
              daveyerwin
              wrote on last edited by
              #6

              overtech06 wrote:

              That's what I've already done in my current solution.

              OOps sorry, I missed that in your original post.

              1 Reply Last reply
              0
              • O overtech06

                That's what I've already done in my current solution. I was just looking for a common interface that would allow me to cover them all in one Generic method... - Dave

                D Offline
                D Offline
                daveyerwin
                wrote on last edited by
                #7

                overtech06 wrote:

                one Generic method...

                Well you could use GetType() then a switch ...

                protected void inaBind(Object a, Object c)
                {
                switch (a.GetType().Name)
                {
                case "GridView":
                (a as GridView).DataSource = c as DataSet;
                break;
                case "Repeater":
                (a as Repeater).DataSource = c as DataSet;
                break;
                }
                }

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups