Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: RE: [Q2] Web User Control: databinding, getting AT the data...


Message #1 by "Chris Kersey" <ckersey@m...> on Fri, 24 May 2002 09:40:06 -0700
comments embedded below...

----- Original Message -----
From: "Feduke Cntr Charles R" <FedukeCR@m...>
To: "ASP+" <aspx@p...>
Sent: Friday, May 24, 2002 5:43 AM
Subject: [aspx] RE: Web User Control: databinding, getting the data...


> Chris,
>
> If you're binding a data set then are your inheriting from Repeater,
> DataList, or DataGrid?  If not, then you should; you can override or add a
> new delegate to the event OnItemCreated and write your own logic there to
> handle the display of items.

I'm inheriting from WebControl.  Let's just say that I'm trying to create
*my own* datagrid control from scratch.  I know that the WebControl has a
DataBind() method so inheriting from WebControl will at least give me that
interface.  My question was/is, once I bind a dataset to this exposed
method, how do I work with it inside of *my own* control.  i.e. once I bind
a dataset to the inherited method DataBind(), is there a protected data
member that I also inherited that I can then iterate thru?  That's the
question I intended to convey.

So the following code snippet you supply makes sense, but I have to ask
where does the "ds" in DataTable dt=(DataTable)ds.Tables["data"]; come from?
Was that variable inherited from WebControl?  By default in Visual Studio 7,
creating a new "Web Control Library" does not make System.Data an available
Reference.  I'm guessing I have to add that manually...

Thanks again for your help.  I'm new to controls and .NET so getting my
hands around the concepts is challenging for me.

Chris


>
> Otherwise, I think you're looking for something like the following:
>
> // get your dataset, ds, with a table named "data" in it
> DataTable dt = (DataTable)ds.Tables["data"];
> foreach (DataRow row in dt.Select())
> {
> output.Write(row["last_name"].ToString() +
> ", " + row["first_name"].ToString() +
> "<br>\n";
> }
>
> FYI, if you want to remain lightweight I'd recommend using a
> *DataReader rather than the complete DataSet/DataTable.  Unless you
> specifically have a need for relationships, use a *DataReader (they are
easy
> to use!).
>
> HTH,
> - Chuck
>
> -----Original Message-----
> From: Chris Kersey [mailto:ckersey@m...]
> Sent: Thursday, May 23, 2002 6:03 PM
> To: ASP+
> Subject: [aspx] Web User Control: databinding, getting the data...
>
>
> I created a Web User control that is derived from the WebControl and  I
want
> to be able to render my control from multiple rows out of the database.
> Question is, if I am able to bind my control to a dataset via the
> WebControl's inherited Bind method and if so how do I get at the data that
> was bound?
>
> I wanna do this...
>
>  protected override void Render(HtmlTextWriter output)
> {
>     //I want to loop thru stuff.. foreach(string stuff in Dataset("eh"))
>         output.Write(stuff + "<BR>");
> }
>
> Thanks for any input you might have :)
> Chris
>
>
>
>
>

Message #2 by Feduke Cntr Charles R <FedukeCR@m...> on Tue, 28 May 2002 08:32:41 -0400
Chris,

	Alright, I see what you're saying.  You'll have to make a private
variable for the control to hold a dataset or a datatable (I'd recommend
datatable unless you specifically need relationships).  You also need a way
for the user to specify how the datatable (or dataset, sigh) is created; for
example, in some of my controls I offer a SqlSelect parameter and a
ConnectionString parameter (and we get the Sql statements from an XML file
that specifies our database, so no I'm not working in reverse!).  So in my
DataBind() method for my web controls, it creates a datatable and populates
it using the SqlSelect against the connection specified with
ConnectionString.  You'll probably want to implement a DataSource property
or what not that the user can set, for example:

// this is all user code:
System.Data.OleDb.OleDbConnection con = new
System.Data.OleDb.OleDbConnection(connectionString);
System.Data.OleDb.OleDbCommand cmd = new
System.Data.OleDb.OleDbCommand(sqlSelect, con);
con.Open();
// now our `fictional' control has a DataReader property for the DataSource
// because using heavier controls would just waste cycles!
fictional.DataSource 
cmd.ExecuteReader(System.Data.CommandBehavior.CloseConnection);
// now what's the use of the DataBind() method?  For this particular
instance, nothing
// because when the DataSource is set, what else is there to do?

// then in your control you can use the code I showed you the first time to
iterate.

HTH,
- Chuck

-----Original Message-----
From: Chris Kersey [mailto:ckersey@m...]
Sent: Friday, May 24, 2002 12:40 PM
To: ASP+
Subject: [aspx] RE: [Q2] Web User Control: databinding, getting AT the
data...


comments embedded below...

----- Original Message -----
From: "Feduke Cntr Charles R" <FedukeCR@m...>
To: "ASP+" <aspx@p...>
Sent: Friday, May 24, 2002 5:43 AM
Subject: [aspx] RE: Web User Control: databinding, getting the data...


> Chris,
>
> If you're binding a data set then are your inheriting from Repeater,
> DataList, or DataGrid?  If not, then you should; you can override or add a
> new delegate to the event OnItemCreated and write your own logic there to
> handle the display of items.

I'm inheriting from WebControl.  Let's just say that I'm trying to create
*my own* datagrid control from scratch.  I know that the WebControl has a
DataBind() method so inheriting from WebControl will at least give me that
interface.  My question was/is, once I bind a dataset to this exposed
method, how do I work with it inside of *my own* control.  i.e. once I bind
a dataset to the inherited method DataBind(), is there a protected data
member that I also inherited that I can then iterate thru?  That's the
question I intended to convey.

So the following code snippet you supply makes sense, but I have to ask
where does the "ds" in DataTable dt=(DataTable)ds.Tables["data"]; come from?
Was that variable inherited from WebControl?  By default in Visual Studio 7,
creating a new "Web Control Library" does not make System.Data an available
Reference.  I'm guessing I have to add that manually...

Thanks again for your help.  I'm new to controls and .NET so getting my
hands around the concepts is challenging for me.

Chris


>
> Otherwise, I think you're looking for something like the following:
>
> // get your dataset, ds, with a table named "data" in it
> DataTable dt = (DataTable)ds.Tables["data"];
> foreach (DataRow row in dt.Select())
> {
> output.Write(row["last_name"].ToString() +
> ", " + row["first_name"].ToString() +
> "<br>\n";
> }
>
> FYI, if you want to remain lightweight I'd recommend using a
> *DataReader rather than the complete DataSet/DataTable.  Unless you
> specifically have a need for relationships, use a *DataReader (they are
easy
> to use!).
>
> HTH,
> - Chuck
>
> -----Original Message-----
> From: Chris Kersey [mailto:ckersey@m...]
> Sent: Thursday, May 23, 2002 6:03 PM
> To: ASP+
> Subject: [aspx] Web User Control: databinding, getting the data...
>
>
> I created a Web User control that is derived from the WebControl and  I
want
> to be able to render my control from multiple rows out of the database.
> Question is, if I am able to bind my control to a dataset via the
> WebControl's inherited Bind method and if so how do I get at the data that
> was bound?
>
> I wanna do this...
>
>  protected override void Render(HtmlTextWriter output)
> {
>     //I want to loop thru stuff.. foreach(string stuff in Dataset("eh"))
>         output.Write(stuff + "<BR>");
> }
>
> Thanks for any input you might have :)
> Chris
>
>
>
>
>



  Return to Index