Generate Web Controls Dynamically?

G

Guest

Hi,

How can I generate web controls such as textboxes and drop-menus on the fly?

My web application allows users to fill out PDF forms online. There are many
PDF forms, and my application reads the fields in the user's chosen PDF file,
displays a webform with relevant fields (textbox or drop-menu or radio
buttons etc) for user to fill out online. I'm using a 3rd party software to
read and write to the PDF file, but I don't know how to generate the webform
on the fly and subsequently extract the values.


YWB.
 
G

Guest

One of the simplest solutions will be creating ALL possible controls in the
design with Viisible property set to false, and then only make visible those
that are requested by the User. This way you will always be able to read
values of your controls. If you want a bit more elegant (from the user's
perspective) solution, you can create a table where each row will contain
both label and control to fill in, and make table rows server side html
controls. Then, similar to the previous scenario, only make visible those
rows controls for which have been requested by User.
 
G

Guest

Hi,

It's not practical for me to create ALL possible controls 'cuz my web
application has to deal with hundreds of PDF files with different fields and
these files are constantly updated.

I think ideally I'd like to have something like your second suggestion. I'd
like to have a table (DataGrid?) and the application will read the related
PDF file during run time, grab all the field names with the 3rd party
software and save these names into an array. Then I'll display a webform in
the table, and add row by row for each element in the field name array and
put a text-box / drop-menu in each row. And upon postback, I'll extract the
value from each control...

But how can I do this?



ywb.
 
G

Guest

WB,

Not trying to scare you - but the way you chose is at least a few days of
hard work...

In general, you create a DataGrid with 1 column being a label, and the
second - template column that contains a set of standard controls - 1
textbox, 1 combo (we omit others, like radiobuttonlist, for simplicity). Then
in DataGrid.ItemCreated event for item rows where
e.Item.ItemType.ToString().IndexOf("Item") > -1 you make relevant controls
visible, something similar to the following:

TableCell cell = e.Cells[myControlsCellsOrdinal];
foreach (Control ctl in cell.Controls)
{
ctl.Visible = false;
if (ctlType.GetType().Name == myTypeFromDatabase)
{
ctl.Visible = true;
if (ctl is TextBox)
((TextBox)ctl).Text = myValueFromDatabase;
else if (ctl is ....
}
}

You take the row from your feeding table of e.Item.ItemIndex to get your
database values.

Then on the postback, you read Items collection of your datagrid to and
check visible controls to retrieve the values posted by the user.

That is a very exciting exercise that you chose.'

Good luck!
 
S

Stuart Irving

Add an <asp:placeholder id="myPlaceholder" etc

In code:
Placeholder myPlaceholder = Page.FindControl("myPlaceholder");

in Form_Load:
TextBox txt = new TextBox();
txt.ID = idstring;
myPlaceholder.Controls.Add(txt)

in PostBack:
string myValue = Request.Form["TextBox1"];

HTH
 

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