FindControl and repeater

M

MattB

This is just a rephrased version of a question I posted earlier. I think I'm
closer now, so it seemed worthy of a new (more specific) post.

In my repeater I'm dynamically creating text boxes, so at compile time I
don't know what text boxes are going to be present at run time. This is
where it seems the FindControl method would be come into play. I loop
through a list of control (all textboxes) names I created on page load and
see if they exist. Unfortunately, even though I see them on the page,
FindControl doesn't come back with anything. I'm guessing it's how I'm
implementing it. Here's some code:

For Each drfields In dtVFields.Rows

Dim strField As String = drfields.Item("Field")

Dim tb As Control = rptVerify.FindControl("txt" & strField)

If Not IsNothing(tb) Then

txtInput = tb.Text

End If

-----------------------

In my test scenario I know I created a textbox with an ID of txtbirth_date
inside the repeater rptVerify, but the code above returns nothing for tb. If
I load the page in a browser and view source, I see my textbox, and it's
id="rptVerify__ctl0_txtbirth_date". So the parent control name is in there,
the id I was expecting is in there, and some other stuff is in there. I
don't even know if that's really relevant. Anyone got any hints to make this
work? Thanks!



Matt
 
D

David Krussow

Sorry I don't have time today to go back and forth with you to get further
clarification of what you are doing - so I'll go off of what my best guess
is:

I have done apparently similar things but with a DataList (rather than with
a Repeater), and this is what worked for me.

1. I included a <table> in the ItemTemplate (which gets repeated for each
DataRow in the DataTable to which the control is bound at runtime.
2. Within the <table> I have a number of controls all arranged nicely of
course - one of which is a textbox (presumably similary to what you are
doing). Look at the HTML closely:
<asp:TextBox id="txtSequence" style="text-align:'center';" Width="35px"
MaxLength="4" Text='<%# (DataBinder.Eval(Container.DataItem,
"SequenceInCatalog")) %>' runat="server"></asp:TextBox>

Notice this part of the above line:
Text='<%# (DataBinder.Eval(Container.DataItem, "SequenceInCatalog")) %>'

This causes the textbox to get populated at runtime.

So this is what populates the textboxes in my DataList at runtime (and no,
you don't have to know what the names of the textboxes are ahead of time, or
find them, or anything else), and the number of textboxes is determined by
the number of DataRows in the DataTable to which the DataList is bound.

3. Then, when it's time to read the values of the textboxes during a
PostBack (to get the user's newly entered values), I loop through the
Request.Form collection which has everything within the <form> tags in your
aspx page as it was submitted (more than you'll want); and parse out the
control names to find the textboxes and their values.

Perhaps this is all totally off base (I suspect it may be), but I hope it
gives you some ideas for how to proceed.

I'd be interested in knowing why you cannot know the names of your textboxes
ahead of time (apparently they come in from Web.config?). It seems like that
one fact is hanging you up because the Repeater takes the name you supply
and modifies it in order to provide uniqueness when more than one instance
if the control gets rendered. Perhaps you can tell us why you don't bind
your control and prefer instead to loop through the DataTable manually (as
opposed to binding it to the Repeater).

HTH

DK
 
M

MattB

Thanks, I'll see what I can come up with based on your information.

To answer your question: This web application is slated to be distributed to
clients so they can web-enable a legacy application without the client
having to do much programming. This particular issue stems from the ability
for our clients to have their customers look up their own records on the web
to make updates to their personal information. Different clients like to use
different pieces of information for a web visitor to confirm they are who
they say they are. So I'm trying to make it so our clients only have to list
a field (or fields) from the guest table of the underlaying db in web.config
and that/those fields will be used to generate prompts for the web visitor
to verify themselves. Then, at runtime the repeater generates text boxes
based on what field names are in the web.config. So AFIK, I won;t know the
names of the text boxes util the app is running and has dynamically created
them.

I maybe taking an awkward approach, as I'm relativley new to asp.net. If you
or anyone else see a fatal flaw in my approach, please let me know. I really
appreciate all the help so far.

An earlier version of this application was written in something called
West-Wind Web Connect (VFP/Web dev environment). That environment had a very
handy method call IsFormVar() that filled this need very well. I'm just
trying to replicate that behavior in asp.net.

Matt

David Krussow wrote:
 
D

David Krussow

Some thoughts on your explanations (with the intent to simplify the problem
before solving it):

First, don't try to model the existing system in terms of implementation;
providing the same functionality to your users in ASP.NET may be much easier
than how it was implemented in that earlier product. Same functionality,
totally different/simpler implementation.

An important distinction to be made in order to solve this problem is
between the "different pieces of information" and textboxes.
The "different pieces of information" ultimately correspond to differen
columns ("fields") in a database. The textboxes can be used to display that
information. It's bad practice to tie these two things together via a naming
convention (i.e., do not do this: have a column in the database named "age"
and a textbox named "age" such that if you change the name of the textbox,
the app breaks and cannot display age from the database).
a field (or fields) from the guest table of the underlaying db in web.config
and that/those fields will be used to generate prompts for the web visitor
to verify themselves. <<<

Your clients don't care about the actual name of the text box(es) - so you
are free to name them whatever you want (textBox1, textBox2), and arrange
them in a table that appears in the <ItemTemplate> of a DataList like I
showed you in my previous post. ASP.NET and HTML for all that matters does
care about the names - so you can make your life easier by NOT trying to
name the controls at runtime. It can be done, but it makes things
difficult - and unnecessarily so in your case because your clients don't
need to know or care about the actual control name.

What your clients apparently do care about is the name of a column ("field")
in a database (as may be listed in your Web.config). What you can do is let
the users choose the datase columns they do want from a list (which is
populated from Web.config or from querying the database for a valid list of
column names), then based on the columns the users selected, display/collect
information in the textboxes which serve a generic data displaying/gathering
role.

What you would to is something like this:
1. Show an aspx page that lets the users choose from a list the possible
database columns they want to use for user authentication (populated from
your Web.config - but ideally from a database.

2. Dynamically build an SQL statement based on the users selection(s) from
the list.

3. Populate an ADO.NET DataSet or DataTable by using the dynamic SQL
statement.

4. Bind the DataSet or DataTable to your DataList control (like described in
my previous post). This is where you have the textboxes populated by
whatever columns exist in the DataSet/DataTable. You can bind by name or by
ordinal position in the Columns collection. Binding by ordinal gets you away
from having to know the column name ahead of time.

You'll want to include in the SQL statement, DataSet, and DataList the
ability to populate label controls that will describe the contents of the
textbox. The textboxes can apparently contain different types of
information, so it would be important to tell the users which textboxes are
showing which columns. You can display the column name in a label next to
the textbox that shows the value of the column for the current row.

I hope this helps.

DK
 
M

MattB

That's some good info. I have all of this working except my comparison (if
text entered matches db) because I'm trying to resolve names. Your point
about ordinal position was one thing I didn't realize I could do, so I think
that's the missing link here. Thanks for taking the time to explain this.

Matt
 

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