Problem with a DropDownList in a WebControl

Z

Zürcher See

I created a webcontrol with inside a dropdownlist, and exposed its
proprieties DataSource,DataMember,DataTextField,DataValueField
But I can't set the DataSource from the aspx page
(DataSource='<%#this.DataSource%>' ). If I set a breakpoint inside the
DataSet propriety, I see that
the thread never reach this point, the DataSource propriety will never be
called. Why?

<%@ Register TagPrefix="wcl" Namespace="WebControlLibrary1"
Assembly="WebControlLibrary1" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebProject1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout"
xmlns:wcl="urn:http://schemas.myCompany.org/myTechnology">
<form id="Form1" method="post" runat="server">
&nbsp;
<wcl:WebCustomControl1 id="WebCustomControl11"
DataSource='<%#this.DataSource%>' DataMember="ddl" DataTextField="text"
DataValueField="value" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute;
TOP: 48px"
runat="server"></wcl:WebCustomControl1>
</form>
</body>
</HTML>


namespace WebProject1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 WebCustomControl11;
private System.Data.DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
if(Session["StoreData"] == null)
{
DataRow dr;
ds=new DataSet();
DataTable t=new DataTable("ddl");
t.Columns.Add(new DataColumn("value",typeof(int)));
t.Columns.Add(new DataColumn("text",typeof(string)));
t.Rows.Add(new object[]{1,"one"});
t.Rows.Add(new object[]{2,"two"});
t.Rows.Add(new object[]{3,"three"});
t.Rows.Add(new object[]{4,"four"});
this.ds.Tables.Add(t);
Session["StoreData"] = ds;
}
else ds=(DataSet)Session["StoreData"];
}

public DataSet DataSource
{
get{return this.ds;}
}
}
}

namespace WebControlLibrary1
{
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private System.Web.UI.WebControls.DropDownList _dropDownList=new
DropDownList();

public object DataSource
{
get{return this._dropDownList.DataSource;}
set{this._dropDownList.DataSource=value;}
}

public string DataMember
{
get{return this._dropDownList.DataMember;}
set{this._dropDownList.DataMember=value;}
}

public string DataTextField
{
get{return this._dropDownList.DataTextField;}
set{this._dropDownList.DataTextField=value;}
}

public string DataValueField
{
get{return this._dropDownList.DataValueField;}
set{this._dropDownList.DataValueField=value;}
}

protected override void Render(HtmlTextWriter output)
{
this._dropDownList.DataBind();
this._dropDownList.RenderControl(output);
}
}
}
 
A

Alessandro Zifiglio

hi Zurcher,
You have a control that inherits from webcontrols and then you want to
expose datasource properties of a dropdownlist to whom you have merely set a
reference ?
Unfortunately its not as easy at ;P

1.You might want to try and inherit from the dropdownlist directly and then
go about exposing those properties. The following article goes into the
details :
http://www.microsoft.com/indonesia/msdn/servercontrols.asp


2. rewrite from scratch and have the webcontrol expose these properties,
theres some extra work here and its not as simple as inheriting from the
dropdownlist which already has these properties. Your going to have to write
the logic yourself. For a proper discussion that goes into the details with
example code reference this article by shawn wilde :
http://www.codeproject.com/aspnet/webcontrolsdatabinding.asp


Zürcher See said:
I created a webcontrol with inside a dropdownlist, and exposed its
proprieties DataSource,DataMember,DataTextField,DataValueField
But I can't set the DataSource from the aspx page
(DataSource='<%#this.DataSource%>' ). If I set a breakpoint inside the
DataSet propriety, I see that
the thread never reach this point, the DataSource propriety will never be
called. Why?

<%@ Register TagPrefix="wcl" Namespace="WebControlLibrary1"
Assembly="WebControlLibrary1" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebProject1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout"
xmlns:wcl="urn:http://schemas.myCompany.org/myTechnology">
<form id="Form1" method="post" runat="server">
&nbsp;
<wcl:WebCustomControl1 id="WebCustomControl11"
DataSource='<%#this.DataSource%>' DataMember="ddl" DataTextField="text"
DataValueField="value" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute;
TOP: 48px"
runat="server"></wcl:WebCustomControl1>
</form>
</body>
</HTML>


namespace WebProject1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 WebCustomControl11;
private System.Data.DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
if(Session["StoreData"] == null)
{
DataRow dr;
ds=new DataSet();
DataTable t=new DataTable("ddl");
t.Columns.Add(new DataColumn("value",typeof(int)));
t.Columns.Add(new DataColumn("text",typeof(string)));
t.Rows.Add(new object[]{1,"one"});
t.Rows.Add(new object[]{2,"two"});
t.Rows.Add(new object[]{3,"three"});
t.Rows.Add(new object[]{4,"four"});
this.ds.Tables.Add(t);
Session["StoreData"] = ds;
}
else ds=(DataSet)Session["StoreData"];
}

public DataSet DataSource
{
get{return this.ds;}
}
}
}

namespace WebControlLibrary1
{
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private System.Web.UI.WebControls.DropDownList _dropDownList=new
DropDownList();

public object DataSource
{
get{return this._dropDownList.DataSource;}
set{this._dropDownList.DataSource=value;}
}

public string DataMember
{
get{return this._dropDownList.DataMember;}
set{this._dropDownList.DataMember=value;}
}

public string DataTextField
{
get{return this._dropDownList.DataTextField;}
set{this._dropDownList.DataTextField=value;}
}

public string DataValueField
{
get{return this._dropDownList.DataValueField;}
set{this._dropDownList.DataValueField=value;}
}

protected override void Render(HtmlTextWriter output)
{
this._dropDownList.DataBind();
this._dropDownList.RenderControl(output);
}
}
}
 
Z

Zürcher See

Thank's, I had found also the article on the codeproject, it was what I was
looking for.

Alessandro Zifiglio said:
hi Zurcher,
You have a control that inherits from webcontrols and then you want to
expose datasource properties of a dropdownlist to whom you have merely set a
reference ?
Unfortunately its not as easy at ;P

1.You might want to try and inherit from the dropdownlist directly and then
go about exposing those properties. The following article goes into the
details :
http://www.microsoft.com/indonesia/msdn/servercontrols.asp


2. rewrite from scratch and have the webcontrol expose these properties,
theres some extra work here and its not as simple as inheriting from the
dropdownlist which already has these properties. Your going to have to write
the logic yourself. For a proper discussion that goes into the details with
example code reference this article by shawn wilde :
http://www.codeproject.com/aspnet/webcontrolsdatabinding.asp


Zürcher See said:
I created a webcontrol with inside a dropdownlist, and exposed its
proprieties DataSource,DataMember,DataTextField,DataValueField
But I can't set the DataSource from the aspx page
(DataSource='<%#this.DataSource%>' ). If I set a breakpoint inside the
DataSet propriety, I see that
the thread never reach this point, the DataSource propriety will never be
called. Why?

<%@ Register TagPrefix="wcl" Namespace="WebControlLibrary1"
Assembly="WebControlLibrary1" %>
<%@ Page language="c#" Codebehind="WebForm1.aspx.cs" AutoEventWireup="false"
Inherits="WebProject1.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm1</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema"
content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout"
xmlns:wcl="urn:http://schemas.myCompany.org/myTechnology">
<form id="Form1" method="post" runat="server">
&nbsp;
<wcl:WebCustomControl1 id="WebCustomControl11"
DataSource='<%#this.DataSource%>' DataMember="ddl" DataTextField="text"
DataValueField="value" style="Z-INDEX: 101; LEFT: 56px; POSITION: absolute;
TOP: 48px"
runat="server"></wcl:WebCustomControl1>
</form>
</body>
</HTML>


namespace WebProject1
{
public class WebForm1 : System.Web.UI.Page
{
protected WebControlLibrary1.WebCustomControl1 WebCustomControl11;
private System.Data.DataSet ds;

private void Page_Load(object sender, System.EventArgs e)
{
if(Session["StoreData"] == null)
{
DataRow dr;
ds=new DataSet();
DataTable t=new DataTable("ddl");
t.Columns.Add(new DataColumn("value",typeof(int)));
t.Columns.Add(new DataColumn("text",typeof(string)));
t.Rows.Add(new object[]{1,"one"});
t.Rows.Add(new object[]{2,"two"});
t.Rows.Add(new object[]{3,"three"});
t.Rows.Add(new object[]{4,"four"});
this.ds.Tables.Add(t);
Session["StoreData"] = ds;
}
else ds=(DataSet)Session["StoreData"];
}

public DataSet DataSource
{
get{return this.ds;}
}
}
}

namespace WebControlLibrary1
{
[DefaultProperty("Text"),
ToolboxData("<{0}:WebCustomControl1
runat=server></{0}:WebCustomControl1>")]
public class WebCustomControl1 : System.Web.UI.WebControls.WebControl
{
private System.Web.UI.WebControls.DropDownList _dropDownList=new
DropDownList();

public object DataSource
{
get{return this._dropDownList.DataSource;}
set{this._dropDownList.DataSource=value;}
}

public string DataMember
{
get{return this._dropDownList.DataMember;}
set{this._dropDownList.DataMember=value;}
}

public string DataTextField
{
get{return this._dropDownList.DataTextField;}
set{this._dropDownList.DataTextField=value;}
}

public string DataValueField
{
get{return this._dropDownList.DataValueField;}
set{this._dropDownList.DataValueField=value;}
}

protected override void Render(HtmlTextWriter output)
{
this._dropDownList.DataBind();
this._dropDownList.RenderControl(output);
}
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,588
Members
45,094
Latest member
PollyBlau4

Latest Threads

Top