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
  1. Home
  2. Web Development
  3. ASP.NET
  4. Custom Web user Control

Custom Web user Control

Scheduled Pinned Locked Moved ASP.NET
htmldesignsysadminsecurityworkspace
5 Posts 2 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.
  • M Offline
    M Offline
    MalikRizwan
    wrote on last edited by
    #1

    I have a custom control with two properties and one overrided method render .. whenever i assign DataTable to one property i preserves it but when the render function is called its value is null here is my code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Nav { /// /// Summary description for ucMenu /// [ToolboxData("<{0}:ucMenu runat=server>")] public class ucMenu : WebControl { DataTable _dtMenu; string _id; public ucMenu() { // // TODO: Add constructor logic here // //_dsMenu = new DataSet(); } public DataTable XMLDataSource { get { return (_dtMenu); } set { _dtMenu = value.Copy(); } } public string NavID { get { return (_id); } set { _id = value; } } protected override void Render(HtmlTextWriter writer) { writer.Write(" "); if (_dtMenu != null) { for (int i = 0; i < _dtMenu.Rows.Count; i++) { writer.Write("* [" + _dtMenu.Rows[i]["text"].ToString() + "]( + _dtMenu.Rows[i][) "); } } writer.Write(" "); writer.Flush(); base.Render(writer); } } } IN HTML i m embedding it as <MyBar:ucMenu ID="UcMenu1" runat="server" /> and in code behind protected ucMenu MyBar = new ucMenu(); on top of class can you please tell me why my datatable refernce to null in render even i have ref it to datatable from property**

    R A M

    **

    M 2 Replies Last reply
    0
    • M MalikRizwan

      I have a custom control with two properties and one overrided method render .. whenever i assign DataTable to one property i preserves it but when the render function is called its value is null here is my code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Nav { /// /// Summary description for ucMenu /// [ToolboxData("<{0}:ucMenu runat=server>")] public class ucMenu : WebControl { DataTable _dtMenu; string _id; public ucMenu() { // // TODO: Add constructor logic here // //_dsMenu = new DataSet(); } public DataTable XMLDataSource { get { return (_dtMenu); } set { _dtMenu = value.Copy(); } } public string NavID { get { return (_id); } set { _id = value; } } protected override void Render(HtmlTextWriter writer) { writer.Write(" "); if (_dtMenu != null) { for (int i = 0; i < _dtMenu.Rows.Count; i++) { writer.Write("* [" + _dtMenu.Rows[i]["text"].ToString() + "]( + _dtMenu.Rows[i][) "); } } writer.Write(" "); writer.Flush(); base.Render(writer); } } } IN HTML i m embedding it as <MyBar:ucMenu ID="UcMenu1" runat="server" /> and in code behind protected ucMenu MyBar = new ucMenu(); on top of class can you please tell me why my datatable refernce to null in render even i have ref it to datatable from property**

      R A M

      **

      M Offline
      M Offline
      minhpc_bk
      wrote on last edited by
      #2

      How do you set the XMLDataSource property? in code-behind or in the web page? Are you sure that the value is not null by the time you set the property of the control?

      M 1 Reply Last reply
      0
      • M minhpc_bk

        How do you set the XMLDataSource property? in code-behind or in the web page? Are you sure that the value is not null by the time you set the property of the control?

        M Offline
        M Offline
        MalikRizwan
        wrote on last edited by
        #3

        I m setting through code behind and value is not null when i set it to XMLDataSource property. It assigns the value correctly because i have checked through debugging. I think there is some problem with declaring the control .. isnt it?**

        R A M

        **

        1 Reply Last reply
        0
        • M MalikRizwan

          I have a custom control with two properties and one overrided method render .. whenever i assign DataTable to one property i preserves it but when the render function is called its value is null here is my code using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Nav { /// /// Summary description for ucMenu /// [ToolboxData("<{0}:ucMenu runat=server>")] public class ucMenu : WebControl { DataTable _dtMenu; string _id; public ucMenu() { // // TODO: Add constructor logic here // //_dsMenu = new DataSet(); } public DataTable XMLDataSource { get { return (_dtMenu); } set { _dtMenu = value.Copy(); } } public string NavID { get { return (_id); } set { _id = value; } } protected override void Render(HtmlTextWriter writer) { writer.Write(" "); if (_dtMenu != null) { for (int i = 0; i < _dtMenu.Rows.Count; i++) { writer.Write("* [" + _dtMenu.Rows[i]["text"].ToString() + "]( + _dtMenu.Rows[i][) "); } } writer.Write(" "); writer.Flush(); base.Render(writer); } } } IN HTML i m embedding it as <MyBar:ucMenu ID="UcMenu1" runat="server" /> and in code behind protected ucMenu MyBar = new ucMenu(); on top of class can you please tell me why my datatable refernce to null in render even i have ref it to datatable from property**

          R A M

          **

          M Offline
          M Offline
          minhpc_bk
          wrote on last edited by
          #4

          Rizvi Malik wrote:

          MyBar:ucMenu ID="UcMenu1" runat="server" /> and in code behind protected ucMenu MyBar = new ucMenu();

          Ah yes, now I look at your sample code again and see the cause: You declare the control in the web page with the id UcMenu1, meanwhile you declare a new instance MyBar in code-behind, and I guess that you set the property of the second instance other than the first one. In this case, you can declare the control in code-behind like this: (in the version 2.0, you no need to this):

          protected ucMenu UcMenu1;

          M 1 Reply Last reply
          0
          • M minhpc_bk

            Rizvi Malik wrote:

            MyBar:ucMenu ID="UcMenu1" runat="server" /> and in code behind protected ucMenu MyBar = new ucMenu();

            Ah yes, now I look at your sample code again and see the cause: You declare the control in the web page with the id UcMenu1, meanwhile you declare a new instance MyBar in code-behind, and I guess that you set the property of the second instance other than the first one. In this case, you can declare the control in code-behind like this: (in the version 2.0, you no need to this):

            protected ucMenu UcMenu1;

            M Offline
            M Offline
            MalikRizwan
            wrote on last edited by
            #5

            Thanx alot minhpc_bk .. so nice of you ..**

            R A M

            **

            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