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


R

Rbrt

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.
 
Ad

Advertisements

L

Lars

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
 
R

Rbrt

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.
 
L

Lars

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
 
R

Rbrt

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.
 
Ad

Advertisements

R

Rbrt

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.
 
Ad

Advertisements


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

Top