"Dynamic" form -- how to?

B

Bart Van Hemelen

Here's what I'm trying to accomplish: I get a number from another page
that indicates the number of members.

The page I want to build -- actually it's not a page, but a UserControl
-- needs to display a form with several types of input fields for each
of those members: regular text fields, a dropdown and Infragistics'
WebDateChooser.

The point is that someone inputs thinsg like name, date of birth etc
for each of the members, which is then submitted to the database.

I was thinking of constructing a DataSet and binding that with a
DataGrid, like this:

--ASCX----------------------------------------------------------------

<asp:DataGrid ID="dgMemberList" Runat="server"
Width="100%"></asp:DataGrid>

--CODE-BEHIND---------------------------------------------------------

DataTable oDT = new DataTable( "MLDT" );

oDT.Columns.Add( "LastName" , typeof( System.String ) );
oDT.Columns.Add( "FirstName" , typeof( System.String ) );
oDT.Columns.Add( "DateOfBirth" , typeof( System.DateTime ) );
oDT.Columns.Add( "Nationality" , typeof( System.Int32 ) );
oDT.Columns.Add( "Gender" , typeof( System.String ) );

for ( int i = 1 ; i <= iNumberOfMembers ; i++ )
{
DataRow oDataRow;
oDataRow = oDT.NewRow();
oDT.Rows.Add( oDataRow );
}

DataSet oDS = new DataSet( "MLDS" );
oDS.Tables.Add( oDT );

dgMemberList.DataSource = oDS;
dgMemberList.DataMember = "MLDT";
dgMemberList.DataBind();

--END-----------------------------------------------------------------

And then adding the controls, of course. Plus: I also need to check
what they input, preferably via RequiredFieldValidator and the likes.

But I'm now wondering about the wisdom of that decision, partly since
I'm kinda stuck at the moment. Does anybody have a better idea?

(FYI: This is all in asp.net/c#, made with VisualStudio.NET.)
 
B

Brett Wiltshire

A DataGrid seems like the right choice to me.

You didn't mention *why* you're stuck ... which is a something of an
impediment to further advice ... :)


Brett.
 
B

Bart Van Hemelen

Brett said:
A DataGrid seems like the right choice to me.

You didn't mention *why* you're stuck ... which is a something of an
impediment to further advice ... :)

Yeah, I was just wondering if anyone had ever done such a thing and
what their experiences were.

However, I've now run into several problems. Here's how I'm now doing
the datagrid:

<asp:datagrid id="dgMemberList" runat="server"
AutoGenerateColumns="False" Width="100%">
<Columns>

<asp:TemplateColumn HeaderText="Last Name">
<ItemTemplate>
<asp:TextBox runat="server" id="txtLastName" Text='' />
<asp:RequiredFieldValidator id="rfLastName"
ControlToValidate="txtLastName" Text="The last name field is required!"
runat="server" Display="None" EnableClientScript="false" />
</ItemTemplate>
</asp:TemplateColumn>

<asp:TemplateColumn HeaderText="Gender">
<ItemTemplate>
<asp:DropDownList id="ddGender"
runat="server"></asp:DropDownList>
<asp:RequiredFieldValidator id="rfGender"
ControlToValidate="ddGender" Text="The Gender field is required!"
runat="server" Display="None" EnableClientScript="false" />
</ItemTemplate>
</asp:TemplateColumn>

</Columns>
</asp:datagrid>

<asp:Button id="cmdSubmit" Text=" Continue " runat="server" />

<asp:ValidationSummary id="lblSummary" runat="server"
ShowMessageBox="false" HeaderText="Errors are as
follows:"></asp:ValidationSummary>

1. I want to populate ddGender with data from the database (*), however
when I tried to it didn't work:

ddGender.Items.Insert(0, new ListItem( "Male" , "1" ) );
ddGender.Items.Insert(0, new ListItem( "Female" , "2" ) );

Result: " System.NullReferenceException: Object reference not set to an
instance of an object." I'm guessing that I need to loop through the
Items collection of the DataGrid and access each filed that way?

(*) Yes I know that this is silly when it comes to gender, but there
are other fields like nationality that do come from the database, and
anyway I do need to fill ddGender with data.

2. When I click the submit button, I do get to see the title of the
lblSummary, but no actual errors. I'm guessing that a similar problem
is happening here: the RequiredFieldValidator cannot find the control
it is supposed to check?

Reason I'm not debugging with VisualStudio: I'm at home and I don't
have VS installed here.
 
B

Brett Wiltshire

Sorry for the delay in responding...perhaps you already have the
solution, which is great, but if not then this may help.

Your best bet is to implement databinding for your dropdowns and let
the datagrid deal with populating the values for each instance. This
is what I have done here. Notice the DataSource property. This refers
to a public property of my code-behind class that exposes, in this
instance, a DataSet. I'm fetching the dataset from a database, but
naturally you could build this up with code if you wish.

....
<ItemTemplate>
....
</ItemTemplate
<EditItemTemplate>
<asp:DropDownList
id=ddAnalysts
style="VISIBILITY: visible"
runat="server"
DataValueField="UserId"
DataTextField="FullName"
DataSource="<%# AllUsers %>"
DESIGNTIMEDRAGDROP="2095"
SelectedValue='<%# DataBinder.Eval(Container,
"DataItem.AnalystUserId") %>'
</asp:DropDownList>
<asp:Label
id=lblAnalystName
style="VISIBILITY: visible"
runat="server"
Text=' said:
</asp:Label>
</EditItemTemplate>
....


As for the field validation:

....
<EditItemTemplate>
<asp:TextBox
id=txtDisplayOrder
runat="server"
Text=' said:
</asp:TextBox>
<asp:RegularExpressionValidator
id="RegularExpressionValidator1"
runat="server"
ValidationExpression="[0-9][1-9]|[1-9][0-9]?"
ControlToValidate="txtDisplayOrder"
ErrorMessage="Must be a whole number between 1 and 99"
</asp:RegularExpressionValidator>
</EditItemTemplate>
....

Again I'm defining this in the EditItemTemplate in my case. When the
framework renders the page, it will uniquely name the controls, so
there shouldn't be any problem...


I hope this helps.
Brett.
 

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

Members online

Forum statistics

Threads
473,781
Messages
2,569,615
Members
45,302
Latest member
endevsols

Latest Threads

Top