Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / ASP.NET / General / February 2008

Tip: Looking for answers? Try searching our database.

<%# Bind(colname) %> in custom formview template

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Rbrt - 18 Feb 2008 10:56 GMT
I have a custom insert template in a formview control. If you create formview
insert and update templates at design time, you can bind their controls to a
datasource using the Bind syntax, for example,
<asp:textbox id="TextBox1"   text='<%# Bind("dbField") %>' .../>

However, when you are building a template at run time in code, this does not
work. What is the way to do this when building templates in code at run time.
The objective is to build the template so that I can get at its data when the
page is posted so it can be inspected and processed in a business layer
object.

Thank you.
Lars - 18 Feb 2008 11:27 GMT
Hi

The only ting I can come to think of is

Here's one example taken from the auto generated code

<asp:Label ID="HTMLLabel" runat="server" Text='<%# Eval("HTML", "{0}") %>'
/>

Lars

>I have a custom insert template in a formview control. If you create
>formview
[quoted text clipped - 13 lines]
>
> Thank you.
Rbrt - 18 Feb 2008 11:46 GMT
Thank you for taking the time to respond. This will not help though. Here is
the code I am using to generate my formview insert template. This code is
executed by Instantiatein at runtime. The value of strColName is taken from
the column names of a datatable so that the code creates an insert template
that matches the database table into which the data will be inserted.

             tb = New TextBox
             tb.ID = strColName
             tb.Enabled = True
             tb.Visible = True
             tb.text="Some default value"
             container.Controls.Add(tb)

The problem is that when data is entered into the template and the Insert
button is clicked, I am not able to find a way to get the data that was
entered and I think it might be because I am not binding the data properly.

> Hi
>
[quoted text clipped - 24 lines]
> >
> > Thank you.
Lars - 18 Feb 2008 12:50 GMT
OK

Some questions I need to know to understand your problem.

There is a function you can call from a TextBox called DataBind(). I think
you have to use that function according to the book "ASP.NET step by step".
But I also think you have to Bind the DataSource to the control

Here's a short example

protected DataTable GetInventory()
{
   DataTable = new DataTable();
   //....
   //....    Set ConnectionString, open the connection, select data or what
ever you want, fetch data from IDataReader for example
   //....
   return dt;
}

protected DataTable BindToInventory()
{
   DataTable dt;
   dt = this.getInventory()
   this.DataList1.DataSource = dt;
   this.DataBind();
   return dt;
}

Lars

> Thank you for taking the time to respond. This will not help though. Here
> is
[quoted text clipped - 56 lines]
> ----= East and West-Coast Server Farms - Total Privacy via Encryption
> =----
Rbrt - 18 Feb 2008 13:44 GMT
I tried that but with an insert template, you are not really binding to a
datasource until after the data has been entered, I would think. In any case,
I did try it but the problem is that when I execute the
formview.iteminserting event, there is no data. Here's the code....

Protected Sub formview_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
formview.ItemInserting

   Dim itemArray(e.Values.Count - 1) As DictionaryEntry
   e.Values.CopyTo(itemArray, 0)

   Dim entry As DictionaryEntry
   For Each entry In itemArray
       ...
   Next

 End Sub

There are no entries in the itemarray. In other words, the formview with my
custom insert template to is not able to retrieve the data entered by the
user.

> OK
>
[quoted text clipped - 87 lines]
> > ----= East and West-Coast Server Farms - Total Privacy via Encryption
> > =----
Rbrt - 18 Feb 2008 14:56 GMT
OK after a lot of searching, googling, and a lot of trial-and-error, I have
found the following solution, which works, but which is not very elegant or
efficient...

in the formview.iteminserting event, I do the following....

Protected Sub formview_ItemInserting(ByVal sender As Object, ByVal e As
System.Web.UI.WebControls.FormViewInsertEventArgs) Handles
formviw.ItemInserting

   Dim fv As FormView = CType(sender, FormView)
   Dim tb As TextBox = CType(fv.FindControl(strFieldName), TextBox)
   Dim s As String

   s = tb.Text

   ....................

s now contains the text that was entered by the user in the textbox.

Of course the preferred method would have been to get the values from the
name/value array that is supposed to be passed in the FormViewEventArgs. Why
it is not there is beyond me.

> I have a custom insert template in a formview control. If you create formview
> insert and update templates at design time, you can bind their controls to a
[quoted text clipped - 8 lines]
>
> Thank you.

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.