Wrox Home  
Search P2P Archive for: Go

  Return to Index  

aspx thread: Creating DataBinding Expressions on the fly and binding to them


Message #1 by "Chris Sano" <t-csano@m...> on Sun, 15 Jul 2001 23:06:17 -0700

I've written some code that enables me to create List-Bound controls

(more specifically, a repeater) and their templates on the fly, but am

having trouble trying to figure out how I can add data binding

expressions to the templates I'm creating and then bind to them at

runtime.







If you look at the Select Case statement of the InstantiateIn sub

procedure in the CreateTemplate class, you'll see that I'm trying to

bind the au_id field of the recordset I'm retrieving from the database

in the page_load sub procedure.







This is what is compiled and returned to the browser:







<table><tr><td><%# Container.DataItem( "au_id" ) %></td></tr></table>







As you can see, the data isn't bound to the container. Instead the

expression is converted to a literal.







I've been pulling my hair out over this for the last few days and am

hoping someone out there has worked with this kind of thing before and

will be able to help out. If you have created List-Bound controls on the

fly and were able to create data binding expressions with those controls

and then bind them later on in the script, would you be willing to share

some snippets of code?







Thanks in advance!







<script language=3D"vb" runat=3D"server">







Public Class CreateTemplate : Implements ITemplate







            dim cTemplate as string







            Public Sub new( template as string )



                        cTemplate =3D template



            End Sub







            Overridable Sub InstantiateIn( container As Control )

implements ITemplate.InstantiateIn







                        Select Case cTemplate







                                    Case "HeaderTemplate"



                                                dim literal1 as new

literal



                                                literal1.text =3D

"<table>"





container.Controls.Add(literal1)







                                    Case "ItemTemplate"



                                                dim literal2 as new

literal



                                                literal2.text =3D

"<tr><td><%# Container.DataItem( ""au_id"" ) %></td></tr>"



                                                container.Controls.Add(

literal2 )



                                                container.DataBind()



                       



                                    Case "FooterTemplate"



                                                dim literal3 as new

literal



                                                literal3.text =3D

"</table>"



                                                container.Controls.Add(

literal3 )                                                







                        End Select



                       



            End Sub







End class







Sub page_load( server as Object, e as EventArgs )







            dim data as new SqlDataAdapter()



            dim ds as new DataSet()







            data.SelectCommand =3D new SqlCommand( "SELECT TOP 1 * FROM

authors", new SqlConnection(

"server=3Dsano;database=3Dpubs;trusted_connection=3Dyes" ) )



            data.SelectCommand.CommandType =3D CommandType.Text



            data.Fill( ds, "authors" )







            dim rep as new Repeater()







            rep.HeaderTemplate =3D new CreateTemplate( "HeaderTemplate" 

)



            rep.ItemTemplate =3D new CreateTemplate( "ItemTemplate" )



            rep.FooterTemplate =3D new CreateTemplate( "FooterTemplate" 

)







            place1.Controls.Add( rep )







            rep.DataSource =3D ds.Tables( "authors" ).DefaultView



            place1.DataBind()



           



End Sub                       







</script>











chris sano | webdev intern - web infrastructure team |

t-csano@m... <mailto:t-csano@m...>





Message #2 by "Pamela Reinskou" <preinsko@h...> on Mon, 16 Jul 2001 14:11:15 -0700
I haven't done any of this in .Net yet so I may be totally offbase; but have

you tried using the JScript eval method?



string = eval("<table><tr><td><%#Container.DataItem( "au_id" )

%></td></tr></table>");





 If anyone knows of an equivalent method in C# I would love to hear about

it.





Pamela Reinskou

Application Developer

http://beta2.eraserver.net/preinsko/





----- Original Message -----

From: "Chris Sano" <t-csano@m...>

To: "ASP+" <aspx@p...>

Sent: Sunday, July 15, 2001 11:06 PM

Subject: [aspx] Creating DataBinding Expressions on the fly and binding to

them





>

> I've written some code that enables me to create List-Bound controls

> (more specifically, a repeater) and their templates on the fly, but am

> having trouble trying to figure out how I can add data binding

> expressions to the templates I'm creating and then bind to them at

> runtime.

>

> 

>

> If you look at the Select Case statement of the InstantiateIn sub

> procedure in the CreateTemplate class, you'll see that I'm trying to

> bind the au_id field of the recordset I'm retrieving from the database

> in the page_load sub procedure.

>

>


  Return to Index