vbFace Posted January 23, 2004 Posted January 23, 2004 I am attempting a dynamic web form. I have a table with 4 columns. In each column is a PlaceHolder object. It goes like this: Column1 Column2 Column3 Column4 ------------------------------------------------------------ Labels RadioBtnLists ReqFldValidators TextBoxes When I dynamically create a Label control, I set the properties like this: myLabel = new Label myLabel.ID = "LabelQ" & QNum '<--- Passed Long myLabel.Text = LabelText myLabel.BorderStyle = 2 plhLabels.Controls.Add(myLabel) ' Add to the Labels' PlaceHolder The problem is that the labels all bunch up and continue left-to-right as wide as the table detail (< td >). I tried to add & "< br >< br >" to the myLabel.Text property. It DOES separate the labels, but not exactly in a way that I can control the spacing with the objects in the rest of the Table Row. It is only the text of the control. Its odd. I am trying to find out how to properly align controls inside the PlaceHolder Object. Quote
Moderators Robby Posted January 23, 2004 Moderators Posted January 23, 2004 I find the best way to place controls on the fly is to use Table/TR/TD tags combined with Styles, whether the styles are in-line or in a CSS file that's up to you. Quote Visit...Bassic Software
samsmithnz Posted January 23, 2004 Posted January 23, 2004 You you post the table source code? It looks like you may need to specifiy a couple more TD tags...? Quote Thanks Sam http://www.samsmith.co.nz
vbFace Posted January 23, 2004 Author Posted January 23, 2004 (edited) Yes, I am putting all of the like-objects into a placeholder in one table cell. I wouldn't know where to start to mix the creation of dynamic controls with the creation of dynamic table rows.... :( That's what you guys are for! :) Seriously, I could use help on that. This is the code that I have for the page: Sub Page_Load Dim oConn as OleDbConnection dim oComm as OleDbCommand Dim Reader as OleDbDataReader Dim SQL as String Dim i as Integer oConn = MyConn.GetConnection() SQL = "SELECT ParentQuestions.QParentText, Questions.QNum, Questions.LabelText, ControlText.ControlText " SQL = SQL & "FROM (ParentQuestions INNER JOIN Questions ON ParentQuestions.QParentNum = Questions.QParentNum) INNER JOIN ControlText ON Questions.ControlTextID = ControlText.ControlTextID " SQL = SQL & "WHERE ParentQuestions.QParentNum = 1;" oComm = New OleDbCommand(SQL, oConn) Reader = oComm.ExecuteReader() i = 1 While Reader.Read() AddControls(i,Reader("Qnum"),Reader("LabelText")) End While End Sub Sub AddControls(Index as Integer, QNum as Long, LabelText as String) Dim myLabel as Label Dim myRadio as RadioButtonList Dim myRFV as RequiredFieldValidator Dim myText as TextBox Dim myUnits as System.Web.UI.WebControls.Unit ' Add Label myLabel = new Label myLabel.ID = "LabelQ" & QNum myLabel.Text = LabelText & "<br><br>" myLabel.BorderStyle = 2 plhLabels.Controls.Add(myLabel) ' Add Radio myRadio = new RadioButtonList myRadio.ID = "RadioQ" & Qnum myRadio.Items.Add("0") myRadio.Items.Add("1") myRadio.Items.Add("2") myRadio.Items.Add("3") myRadio.Items.Add("4") myRadio.Items.Add("5") myUnits = new System.Web.UI.WebControls.Unit(402) myRadio.Width = myUnits myRadio.RepeatDirection = 0 plhRadios.Controls.Add(myRadio) ' Add RFV myRFV = new RequiredFieldValidator myRFV.ID = "RFVQ" & QNum myRFV.ErrorMessage = "Unanswered" myRFV.ControlToValidate = "RadioQ" & Qnum plhRFVs.Controls.Add(myRFV) End Sub <table class="MyText" height="1000" cellspacing="0" cellpadding="1" width="800" bgcolor="white" border="0"> <tbody> <tr> <td valign="top" width="800" colspan="9" height="75"> <!--#include file="include/header.inc"--></td> </tr> <tr> <td class="QText" valign="top" width="800" colspan="9" height="50"> <br /> <asp:Label id="lblQParent" runat="server" cssclass="MyText" font-bold="True" forecolor="Blue">Please indicate the degree to which the following companies provide "value for the cost of their services."</asp:Label></td> </tr> <tr> <td valign="bottom" width="200" height="25"> </td> <td valign="bottom" align="left" width="67" height="25"> <font color="black"><u>No</u> <br /> <u>Opinion</u> </font></td> <td valign="bottom" align="left" width="67" height="25"> <font color="black"><u>Low </u> <br /> <u>Value</u></font> </td> <td valign="bottom" width="67" colspan="1" height="25"> <font color="black"><u>High </u> <br /> <u>Value</u></font> </td> <td width="198" colspan="2"> </td> </tr> <tr bgcolor="#e0e0e0"> <td valign="center" align="left" width="200" height="25"> <asp:PlaceHolder id="plhLabels" runat="server"></asp:PlaceHolder> </td> <td valign="center" width="402" colspan="6" height="25"> <asp:PlaceHolder id="plhRadios" runat="server"></asp:PlaceHolder> </td> <td valign="center" width="100"> <asp:PlaceHolder id="plhRFVs" runat="server"></asp:PlaceHolder> </td> <td valign="center" width="100"> <asp:PlaceHolder id="plhQNums" runat="server"></asp:PlaceHolder> </td> </tr> <tr> <td valign="center" width="200" height="25"> </td> <td valign="top" width="402" colspan="6" height="25"> </td> </tr> <tr> <td colspan="9" height="25"> </td> </tr> <tr> <td valign="bottom" align="middle" width="800" colspan="9" height="25"> <asp:Button id="btnContinue" onclick="btnContinue_Click" runat="server" Text="Continue"></asp:Button> </td> </tr> <tr> <td valign="bottom" width="800" colspan="9"> <!--#include file="include/footer.inc"--></td> </tr> </tbody> </table> Edited January 23, 2004 by vbFace Quote
samsmithnz Posted January 23, 2004 Posted January 23, 2004 Thats what I thought. I think that you should look at creating a table cell for each control. Then you'll have better control over alignments and spacing. Its a bit harder to help you more than that, as I'm really not sure what you final end goal is. There are undoubtly going to be exceptions. Quote Thanks Sam http://www.samsmith.co.nz
vbFace Posted January 23, 2004 Author Posted January 23, 2004 If I want to dynamically add table columns/rows to an HTML table, will I have to incorporate Response.Write("<TR>") and similar .Writes in the code section to build it? I am not sure how to do this. EDIT: Oh, are you saying abandon the use of the PlaceHolder and dynamically load the controls into the dynamcially created table rows? Quote
samsmithnz Posted January 23, 2004 Posted January 23, 2004 I think that dynamically building the table using response.writes or the HTTPStream is a better idea. Then your output will be truely dynamic (that is completely generated on the server). I wasn't saying that about placeholders, but I do wonder about them (and the person that invented them). You don't really need them, unless you're designing a page and you really do need a placeholder. Quote Thanks Sam http://www.samsmith.co.nz
mr relaxo Posted January 23, 2004 Posted January 23, 2004 Have you considered using the asp:repeater control instead? It seems like it would meet your requirements i.e dynamically create html tables based on a database query and make it alot easier to obtain the properties of the controls. This would also keep alot of presentation logic out of your code. Just a thought, best of luck Kris. Quote You can not get ye flask.
Moderators Robby Posted January 24, 2004 Moderators Posted January 24, 2004 PlaceHolders and Panels are a good idea, however Response.Write is Not. You may want to consider Kris's idea on a Repeater or even a DataList. Quote Visit...Bassic Software
samsmithnz Posted January 24, 2004 Posted January 24, 2004 PlaceHolders and Panels are a good idea, however Response.Write is Not. You may want to consider Kris's idea on a Repeater or even a DataList. Robby, you can't say that ("Response.Write is Not") without a reason. And we still don't have an idea of the scale of the project...? Quote Thanks Sam http://www.samsmith.co.nz
Moderators Robby Posted January 25, 2004 Moderators Posted January 25, 2004 If the project has more than a single web page than that's scale enough for me. I cannot see any valid reason for using Response.Write aside from doing some lazy debugging (as I sometimes do). Quote Visit...Bassic Software
samsmithnz Posted January 25, 2004 Posted January 25, 2004 whats so bad about using response.write though? Why should I use something else? And if response is so bad, should I still be using request too? Quote Thanks Sam http://www.samsmith.co.nz
Moderators Robby Posted January 25, 2004 Moderators Posted January 25, 2004 Response.Write is slower than using Literals, also, they have no ViewState, literals do. Quote Visit...Bassic Software
*Gurus* Derek Stone Posted January 25, 2004 *Gurus* Posted January 25, 2004 The problem with Response.Write extends far beyond basic issues such as speed and object persistence. The more important issue is that Response.Write creates spaghetti code-- code that is less readable due to it's spattering across content. Which is more readable and more manageable? <script runat="server"> Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Dim identifier As Integer = 7 ctlHyperLink.NavigateUrl = "page.aspx?id=" & identifier.ToString() ctlHyperLink.Text = "Foobar" End Sub </script> <asp:HyperLink id="ctlHyperLink" NavigateUrl="" Text="" runat="server" /> <% Dim identifier As Integer = 7 %> <a href="<% Response.Write("page.aspx?id=" & identifier.ToString()) %>"><% Response.Write("Foobar") %></a> The latter is clearly a mess. Quote Posting Guidelines
samsmithnz Posted January 25, 2004 Posted January 25, 2004 <% Dim identifier As Integer = 7 %> <a href="<% Response.Write("page.aspx?id=" & identifier.ToString()) %>"><% Response.Write("Foobar") %></a> The latter is clearly a mess. This is all wrong. I'd do it like this: It looks a lot better. <% Dim identifier As Integer = 7 %> <a href="page.aspx?id=<%=identifier.ToString()%>">Foobar</a> This is all wrong. I'd do it like this: It looks a lot better. I think you'll need a better example than this to convince me. I'd never use response.write like that. Quote Thanks Sam http://www.samsmith.co.nz
*Gurus* Derek Stone Posted January 25, 2004 *Gurus* Posted January 25, 2004 It doesn't matter if you use Response.Write or the "=" shortcut. The point is they both look absolutely horrible. If you have a whole page of these try finding and correcting all of them. Then do the same thing with the preferred method as I posted above. You need to try it to understand the differences and benefits. It's clear that you won't be convinced otherwise. Quote Posting Guidelines
samsmithnz Posted January 25, 2004 Posted January 25, 2004 Everything in moderation. I don't use comments like response.write or <%=[string]%> in my ASP.NET pages anyway, but when I do, I still think its better to have one simple line , than 4 lines in a script tag. By ASp.NET pages still look neat as and are very maintainable... Personal judgement. Quote Thanks Sam http://www.samsmith.co.nz
*Gurus* Derek Stone Posted January 26, 2004 *Gurus* Posted January 26, 2004 You don't have 4 lines. You have one per property assignment. I think that's the big point you're missing. You can't include the Page_Load declaration in your argument. It applies only once to all controls on the page. What if you have logic (If/Then, Do/While, etc.) spattered in inline code tags? Calling that neat would be a complete fallacy. What if your code makes a method call that returns a value? Are you checking the value in inline code? Do you use error handling when it truly needs to be present? Are you properly HTML encoding all entity references and potentially harmful characters (something which Web control properties do for you)? The list goes on, but I'll leave it at that. Quote Posting Guidelines
vbFace Posted January 26, 2004 Author Posted January 26, 2004 The scope of the project is a multi-page questionnaire. I will have to look into the data repeater control... Quote
samsmithnz Posted January 26, 2004 Posted January 26, 2004 how interesting... I just finished a survey ASP.NET application. It used to be a Delano (bet there aren't many here who know what that is) application, and it used a xsl file to parse the result of an XML file. I've posted the code here, maybe it will interest you...: <?xml version="1.0"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <xsl:for-each select="//question[not(section_name = preceding-sibling::question/section_name)]"> <TABLE width="100%" border="1" cellspacing="0" cellpadding="2" align="center" class="verdana" bordercolor="dimgray"> <TR bgcolor="gainsboro"><TD colspan="9" height="4"><B><xsl:value-of select="section_name"/></B></TD></TR> <TR bgcolor="gainsboro"> <TD height="46" width="1%"> </TD> <TD width="64%"><DIV align="center"> </DIV></TD> <TD width="7%" bgcolor="whitesmoke"><DIV align="center"><FONT size="2"><I>no data</I></FONT></DIV></TD> <TD width="7%"><DIV align="center"><FONT size="2"><I>strongly disagree</I></FONT></DIV></TD> <TD width="7%"><DIV align="center"><FONT size="2"><I>disagree</I></FONT></DIV></TD> <TD width="7%"><DIV align="center"><FONT size="2"><I>agree</I></FONT></DIV></TD> <TD width="7%"><DIV align="center"><FONT size="2"><I>strongly agree</I></FONT></DIV></TD> </TR> <xsl:call-template name="ordinal_questions"> <xsl:with-param name="ordinal_section" select="section_name"/> </xsl:call-template> </TABLE> <BR></BR> </xsl:for-each> </xsl:template> <xsl:template name="ordinal_questions"> <xsl:param name="ordinal_section"/> <xsl:for-each select="//question[section_name=$ordinal_section][question_text!=$ordinal_section][question_type='O']"> <TR> <TD width="1%" height="46" bgcolor="gainsboro"> </TD> <TD width="64%" bgcolor="gainsboro"><xsl:value-of select="question_text"/></TD> <TD width="7%" align="center" bgcolor="whitesmoke"><INPUT type="radio" value="0"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute></INPUT></TD> <TD width="7%" align="center" bgcolor="gainsboro"><INPUT type="radio" value="1"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute></INPUT></TD> <TD width="7%" align="center" bgcolor="gainsboro"><INPUT type="radio" value="2"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute></INPUT></TD> <TD width="7%" align="center" bgcolor="gainsboro"><INPUT type="radio" value="3"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute></INPUT></TD> <TD width="7%" align="center" bgcolor="gainsboro"><INPUT type="radio" value="4"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute></INPUT></TD> </TR> </xsl:for-each> <xsl:for-each select="//question [section_name=$ordinal_section][question_text!=$ordinal_section][question_type='C']"> <TR bgcolor="gainsboro"> <TD width="1%" height="46"> </TD><TD><xsl:value-of select="question_text"/></TD> <TD width="99%" colspan="6"><textarea rows='3' style='width:100%'><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute> </textarea></TD> </TR> </xsl:for-each> <xsl:for-each select="//question [section_name=$ordinal_section][question_text=$ordinal_section]"> <TR bgcolor="gainsboro"> <TD width="1%" height="46"> </TD> <TD width="99%" colspan="6"><B>Comments:</B> (For actionable team feedback, try to structure your comments using the Stop/Start/Continue format) <BR><textarea rows='3' style="width:100%"><xsl:attribute name="name"><xsl:value-of select="controlName"/></xsl:attribute> </textarea></BR></TD> </TR> </xsl:for-each> </xsl:template> </xsl:stylesheet> Quote Thanks Sam http://www.samsmith.co.nz
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.