Custom Web user Control
-
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
**
-
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
**
-
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?
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
**
-
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
**
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 instanceMyBar
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;
-
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 instanceMyBar
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;
Thanx alot minhpc_bk .. so nice of you ..**
R A M
**