Database Dynamically Generated WebForms

H

Heimer

Is the following possible in .Net? Specifically c# codebehind using
webcontrols. I'm pretty sure it isn't but I wanted to check to make
sure..

Basically I have an object, lets call it a TEMPLATE. Each TEMPLATE
has x MAP(s) on it. Each MAP has x ATTRIBUTE(s). I need to be able
to create a web form that will allow me to edit the MAP(s) and
ATTRIBUTE(s) when I supply a TEMPLATE(ID). The MAP(s) will be listed
on a page with the optoin of changing the ATTRIBUTE(s). The
ATTRIBUTE(s) values will be in html select boxes.

In any scripting languagues I would just loop over my query object and
build the select and text boxes as needed in html, keeping naming
simple by adding the unique IDentifier to the name attribute of the
select box. Then on form submit I'd loop through the Request.Form[]
and do my query updates that way. See pseudo code below...

templateQuery="SELECT * FROM TEMPLATE INNER JOIN MAPS ON
Template.MapId= Maps.MapId WHERE TemplateId = @templateId"; //returns
template with 3 maps
positionQuery="SELECT * FROM POSITION";

<% for(int i=0;i<templateQuery.Length;i++){ %>
<tr><td colspan="2"><%= templateQuery.templateName %></td></tr>
<tr><td>Position: </td>
<td><select name="position<%= templateQuery.templateId%>">
<% for(int j=0;j<positionQuery.Length;j++){ %>
<option <% if (templateQuery.positionId ==
positionQuery[j].positionId){ %>SELECTED <%}%> value="<%=
positionQuery[j].positionId %>"><%= positionQuery[j].positionName %>
<%} %>
</select></td></tr>

<% } %>
 
J

Josh

Is the following possible in .Net? Specifically c# codebehind using
webcontrols. I'm pretty sure it isn't but I wanted to check to make
sure..

Perfectly possible old chum. You can add controls to a form as you wish,
although when I've done this, laying them out nicely and meaningfully and be
a little tricky.

I used a data dictionary table that included some addition GUI info, such as
"DisplayFieldOnLeft", blah blah blah.
 
H

Heimer c

Well Josh, how about an example? I'm clueless as to how I'd create and
name the controls while I'm looping through my query structure.
 
J

Josh

Well Josh, how about an example? I'm clueless as to how I'd create and
name the controls while I'm looping through my query structure.

System.Web.UI.WebControls.TextBox myTextbox = new
System.Web.UI.WebControls.TextBox();

myTextbox.ID = "thingy";

myTextbox.Text = "some text";

this.pnlEditControls.Controls.Add(myTextbox );



A common pitfall when doing this kind of thing is that you must reload all
dynamicly loaded controls on each post back. the viewstate for the controls
is created as normal, but if you do not reload on postback the viewstate can
not be reinatated to the control.
 
H

Heimer .

Josh,

I think you missed the point. Put a loop around your code and tell me
how you would end up with X number of differenntly named controls you
could map query results to.

I need to dynamically create X number of
System.Web.UI.WebControls.TextBox with different VARIABLE NAMES. Your
"myTextBox" needs to be "myTextBox"+tableId. You are assuming I am
going to have a static number of TextBoxes every visit to the page and
you can create them prior to the query. Do you understand?
 
R

Robert Koritnik

I'd rather do ID naming like this:
template_mapX_attributeY
where x and Y are numbers

and if you have to display many templates I'd make a web user control that
would handle a display of a single template. Instead of that template string
I would use control's ID or template name...

So names would be meaningful and understandable.

Dinamic creation of controls SHOULD be done in codebehind. Don't create in
ASPX using server script tags. ASPX is for static HTML and databound server
scripts...

I think there's no need for code samples. Check MSDN/internet instead about
dynamic control creation. As easy as that.
 
H

Heimer .

Robert,

Did you read my first post closely? I know I "SHOULD" be doing this in
codebehind and I'm trying... hence the post. The example was code as to
how I HAVE done it in other scripting languages so you could understand
my issue.

I tried to keep my example simple, but this seems to have you people
thinking I'm completely inept, so here's the real example. I will have
a webpage where I will pass in templateId. Let's say templateId=47 and
I do my query (which outer joins several tables where template_id = 47)
and the result comes back with this:
TEMPLATE_ID 47:
MAP_ID 2023
POSITION_ID = 23
POSITION_ID = 26
POSITION_ID = 35
COORS_ID = 990
COORS_ID = 994
ZEVN_ID = 6
RALF_ID = 20293
RALF_ID = 20246
MAP_ID 2039
POSITION_ID = 45
COORS_ID = 977
COORS_ID = 904
RALF_ID = 20255
ZWEX_ID = 55
ZWEX_ID = 56
MAP_ID 2051
POSITION_ID = 23
POSITION_ID = 39
COORS_ID = 975
COORS_ID = 994
ZEVN_ID = 12
RALF_ID = 20293
RALF_ID = 20246
QRTZ_ID = 5546
QRTZ_ID = 5545

I want to create a form to update these on the fly. In this example
there are three maps, in the next there map be one or five? Do you kind
of see where I'm going now?
 
R

Robert Koritnik

It wasn't that we thought you are a total beginner. I think you didn't
explain what the problem was? You just gave us something as asked if it's
possible to make? I give you an idea about a car and ask you if it's
possible, but forget to tell you how I already made it and that I have some
problems with transmission. D? You also shouldn't treat us like morons. Fair
enough? So If I miss the point here in this post, try to realy enlighten us
what exactly should be done in your scenario...

Ok. The try of an answer. I would create a Table on the fly. With two nested
foreach loops creating the rows.
Ok. First you display template data and store identification in a custom
attribute, because you'll have to know the id when posting data back to
server and updating it in the database.

Ok. First row is always the same. some data about your template. If it's ID,
that you'll be refering to, add a custom attribute to your TableRow element
on the server. Like:
Attributes.Add("templateIdentifier", whatever.ID);

ok then there are two nested foreach loops:

foreach (depends on the object you get back - maps)
{
create tablerow (+ cells) and display map data
and ad custom attribute with identifier
foreach(depends on the object you get back - attributes)
{
create tablerow (+cells) and display its data + textbox
(with some parsable ID), to provide new value
and add custom attribute for identifiing purposes on postbacks
}
}

This should display and work just fine. ok. I would create a table with
cells and ident items with css, so all forms (textboxes etc) would align.

Regarding data to enter. Hmm. You know the types you'll get and for each
data type of the attribute display an appropriate server control and act the
same way on postback.

But actualy I still don't know where the problem is? Displaying this isn't a
problem, postbacks aren't either (except if you have problems because of
dynamic control creation, where you have to know the asp.net concept a lot
more than basic knowledge). So apart of all this where seems to be the
problem?
 

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,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top