iparsigold.you5.net
New Member
Hi,<BR><BR>I am trying to create dynamic server controls inside a Template Column in a DataGrid, and I can't understand why I can only set some properties of the control and not others. <BR><BR>In the example code below, you can see that I am experimenting with a dynamic TextBox "myTextBox". I successfully managed to set some properties of this TextBox (eg myTextBox.Text = "bla bla bla bla bla"), but sometimes my code seemed to make no difference at all (eg myTextBox.MaxLength = "5") and sometimes it just caused a parser error (eg myTextBox.TextMode = "MultiLine").<BR><BR>But if you look in the SDK documentation under Textbox, you find that Text, MaxLength and TextMode are all listed as properties of a TextBox. So why don't they all work the same way?<BR><BR>I have been experimenting with setting properties of other server controls (Label and Hyperlink) dynamically and have found that they too respond in this incomprehensible way - only a few of their properties can (apparently) be set dynamically.<BR><BR>This makes no sense, and I'm sure someone out there can explain what's going on! <BR><BR>Thanks,<BR><BR>JON<BR><BR><BR><BR><BR><BR>************************************************** ****************************<BR> The Code<BR>************************************************** ****************************<BR><BR><BR><%@ Page Language="VB" Debug="true" %><BR><%@ Import Namespace="System.Data" %><BR><%@ Import Namespace="System.Data.Oledb" %><BR><BR><script language="VB" runat="server"><BR> Dim objConn as New OleDbConnection ( ConfigurationSettings.AppSettings("ConnectionString"))<BR> Dim ds as Dataset = New DataSet()<BR> Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems", objConn )<BR> <BR> Sub Page_Load(sender as object, e as eventargs)<BR> objConn.Open()<BR> objAdapter.Fill(ds, "tblItems")<BR> dg.DataSource = ds<BR> dg.DataMember = "tblItems"<BR> <BR> If Not Page.IsPostBack Then<BR> dg.Databind()<BR> End If<BR> End Sub<BR> <BR> Sub myDataGrid_ItemDataBound(sender As Object, e As DataGridItemEventArgs)<BR><BR> 'Only perform this code if we are looking at the Item or Alternating Item <BR> If (e.Item.ItemType = ListItemType.Item) or (e.Item.ItemType = ListItemType.AlternatingItem)<BR><BR> Dim myTextBox As TextBox = new Textbox()<BR><BR> '************************************************* ****<BR> 'THESE COMMANDS SEEM TO WORK:<BR> '************************************************* ****<BR> myTextBox.Text = "bla bla bla bla bla"<BR> myTextBox.Columns = "5"<BR> <BR> '************************************************* ****<BR> 'THESE COMMANDS HAVE NO EFFECT:<BR> '************************************************* ****<BR> myTextBox.MaxLength = "5"<BR> myTextBox.Rows = "4"<BR><BR> '************************************************* ****<BR> 'THIS COMMAND PRODUCES AN ERROR<BR> '************************************************* ****<BR> myTextBox.TextMode = "MultiLine"<BR><BR><BR> <BR> e.Item.Cells(4).Controls.Add(myTextBox)<BR> <BR> End if<BR> <BR> End Sub<BR><BR> <BR><BR></script> <BR><BR><html><body><BR> <form runat="server"><BR> <aspataGrid id="dg" runat="server"<BR> Bordercolor="black" <BR> gridlines="vertical"<BR> font-names="Arial" <BR> font-size="10pt"<BR> cellpadding="4"<BR> cellspacing="0"<BR> width="100%"<BR> ShowFooter="True"<BR> HeaderStyle-BackColor="#cccc99"<BR> FooterStyle-BackColor="#cccc99"<BR> ItemStyle-BackColor="#ffffff"<BR> AlternatingItemStyle-Backcolor="#cccccc"<BR> AutoGenerateColumns="False"<BR> onitemdatabound="myDataGrid_ItemDataBound"<BR> ><BR> <BR> <BR> <BR> <Columns><BR> <asp:boundcolumn readonly="true" HeaderText="ItemID" DataField="ItemID" /><BR> <asp:boundcolumn readonly="true" HeaderText="ItemTypeID" DataField="ItemTypeID" /><BR> <asp:boundcolumn readonly="true" HeaderText="ItemTitle" DataField="ItemTitle" /><BR> <asp:boundcolumn readonly="true" HeaderText="ItemDescription" DataField="ItemDescription" /><BR> <BR> <asp:TemplateColumn><BR> <ItemTemplate><BR> <BR> </ItemTemplate><BR> </asp:TemplateColumn><BR> <BR> <asp:boundcolumn readonly="true" HeaderText="PostedBy" DataField="PostedBy" /><BR> <asp:boundcolumn readonly="true" HeaderText="PostedDate" DataField="PostedDate" /><BR> <asp:boundcolumn readonly="true" HeaderText="ItemParent" DataField="ItemParent" /><BR> <asp:boundcolumn readonly="true" HeaderText="SortOrder" DataField="SortOrder" /><BR> <asp:boundcolumn readonly="true" HeaderText="ItemRating" DataField="ItemRating" /><BR> <asp:boundcolumn readonly="true" HeaderText="ModeratedYN" DataField="ModeratedYN" /><BR> <asp:boundcolumn readonly="true" HeaderText="JPStillToReplyYN" DataField="JPStillToReplyYN" /><BR> <asp:boundcolumn readonly="true" HeaderText="ArticleOriginalTitle" DataField="ArticleOriginalTitle" /><BR> </Columns><BR> <BR> <BR> </asp:dataGrid><BR><BR><BR> <BR> </form> <BR></body></html>