|
 |
aspx_beginners thread: Problem creating dynamic server controls inside DataGrid
Message #1 by "Jon Maz" <jonmaz@s...> on Wed, 27 Feb 2002 20:11:12
|
|
Hi,
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.
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").
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?
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.
This makes no sense, and I'm sure someone out there can explain what's
going on!
Thanks,
JON
*******************************************************************
The Code
*******************************************************************
<%@ Page Language="VB" Debug="true" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Oledb" %>
<script language="VB" runat="server">
Dim objConn as New OleDbConnection (
ConfigurationSettings.AppSettings("ConnectionString"))
Dim ds as Dataset = New DataSet()
Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM tblItems",
objConn )
Sub Page_Load(sender as object, e as eventargs)
objConn.Open()
objAdapter.Fill(ds, "tblItems")
dg.DataSource = ds
dg.DataMember = "tblItems"
If Not Page.IsPostBack Then
dg.Databind()
End If
End Sub
Sub myDataGrid_ItemDataBound(sender As Object, e As
DataGridItemEventArgs)
'Only perform this code if we are looking at the Item or
Alternating Item
If (e.Item.ItemType = ListItemType.Item) or
(e.Item.ItemType = ListItemType.AlternatingItem)
Dim myTextBox As TextBox = new Textbox()
'*****************************************************
'THESE COMMANDS SEEM TO WORK:
'*****************************************************
myTextBox.Text = "bla bla bla bla bla"
myTextBox.Columns = "5"
'*****************************************************
'THESE COMMANDS HAVE NO EFFECT:
'*****************************************************
'myTextBox.MaxLength = "5"
'myTextBox.Rows = "4"
'*****************************************************
'THIS COMMAND PRODUCES AN ERROR
'*****************************************************
'myTextBox.TextMode = "MultiLine"
e.Item.Cells(4).Controls.Add(myTextBox)
End if
End Sub
</script>
<html><body>
<form runat="server">
<asp:DataGrid id="dg" runat="server"
Bordercolor="black"
gridlines="vertical"
font-names="Arial"
font-size="10pt"
cellpadding="4"
cellspacing="0"
width="100%"
ShowFooter="True"
HeaderStyle-BackColor="#cccc99"
FooterStyle-BackColor="#cccc99"
ItemStyle-BackColor="#ffffff"
AlternatingItemStyle-Backcolor="#cccccc"
AutoGenerateColumns="False"
onitemdatabound="myDataGrid_ItemDataBound"
>
<Columns>
<asp:boundcolumn readonly="true"
HeaderText="ItemID" DataField="ItemID" />
<asp:boundcolumn readonly="true"
HeaderText="ItemTypeID" DataField="ItemTypeID" />
<asp:boundcolumn readonly="true"
HeaderText="ItemTitle" DataField="ItemTitle" />
<asp:boundcolumn readonly="true"
HeaderText="ItemDescription" DataField="ItemDescription" />
<asp:TemplateColumn>
<ItemTemplate>
</ItemTemplate>
</asp:TemplateColumn>
<asp:boundcolumn readonly="true"
HeaderText="PostedBy" DataField="PostedBy" />
<asp:boundcolumn readonly="true"
HeaderText="PostedDate" DataField="PostedDate" />
<asp:boundcolumn readonly="true"
HeaderText="ItemParent" DataField="ItemParent" />
<asp:boundcolumn readonly="true"
HeaderText="SortOrder" DataField="SortOrder" />
<asp:boundcolumn readonly="true"
HeaderText="ItemRating" DataField="ItemRating" />
<asp:boundcolumn readonly="true"
HeaderText="ModeratedYN" DataField="ModeratedYN" />
<asp:boundcolumn readonly="true"
HeaderText="JPStillToReplyYN" DataField="JPStillToReplyYN" />
<asp:boundcolumn readonly="true"
HeaderText="ArticleOriginalTitle" DataField="ArticleOriginalTitle" />
</Columns>
</asp:dataGrid>
</form>
</body></html>
Message #2 by "Alvin Ling" <alvin.ling@i...> on Thu, 28 Feb 2002 12:03:39 -0500
|
|
The problem is how you're setting the properties.
myTextBox.Text = "bla bla blah"
myTextBox.TextMode = TextBoxMode.MultiLine
myTextBox.MaxLength = 5 (do not use quotes since MaxLength expects an
integer)
Alvin
> -----Original Message-----
> From: Jon Maz [mailto:jonmaz@s...]
> Sent: Wednesday, February 27, 2002 8:11 PM
> To: aspx_beginners
> Subject: [aspx_beginners] Problem creating dynamic server
> controls inside DataGrid
>
>
> Hi,
>
> 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.
>
> 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").
>
> 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?
>
> 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.
>
> This makes no sense, and I'm sure someone out there can
> explain what's
> going on!
>
> Thanks,
>
> JON
>
>
>
>
>
> *******************************************************************
> The Code
> *******************************************************************
>
>
> <%@ Page Language="VB" Debug="true" %>
> <%@ Import Namespace="System.Data" %>
> <%@ Import Namespace="System.Data.Oledb" %>
>
> <script language="VB" runat="server">
> Dim objConn as New OleDbConnection (
> ConfigurationSettings.AppSettings("ConnectionString"))
> Dim ds as Dataset = New DataSet()
> Dim objAdapter as New OleDbDataAdapter ( "SELECT * FROM
> tblItems",
> objConn )
>
> Sub Page_Load(sender as object, e as eventargs)
> objConn.Open()
> objAdapter.Fill(ds, "tblItems")
> dg.DataSource = ds
> dg.DataMember = "tblItems"
>
> If Not Page.IsPostBack Then
> dg.Databind()
> End If
> End Sub
>
> Sub myDataGrid_ItemDataBound(sender As Object, e As
> DataGridItemEventArgs)
>
> 'Only perform this code if we are looking at
> the Item or
> Alternating Item
> If (e.Item.ItemType = ListItemType.Item) or
> (e.Item.ItemType = ListItemType.AlternatingItem)
>
> Dim myTextBox As TextBox = new Textbox()
>
>
> '*****************************************************
> 'THESE COMMANDS SEEM TO WORK:
> '*****************************************************
> myTextBox.Text = "bla bla bla bla bla"
> myTextBox.Columns = "5"
>
>
> '*****************************************************
> 'THESE COMMANDS HAVE NO EFFECT:
> '*****************************************************
> 'myTextBox.MaxLength = "5"
> 'myTextBox.Rows = "4"
>
>
> '*****************************************************
> 'THIS COMMAND PRODUCES AN ERROR
> '*****************************************************
> 'myTextBox.TextMode = "MultiLine"
>
>
>
> e.Item.Cells(4).Controls.Add(myTextBox)
>
> End if
>
> End Sub
>
>
>
> </script>
>
> <html><body>
> <form runat="server">
> <asp:DataGrid id="dg" runat="server"
> Bordercolor="black"
> gridlines="vertical"
> font-names="Arial"
> font-size="10pt"
> cellpadding="4"
> cellspacing="0"
> width="100%"
> ShowFooter="True"
> HeaderStyle-BackColor="#cccc99"
> FooterStyle-BackColor="#cccc99"
> ItemStyle-BackColor="#ffffff"
> AlternatingItemStyle-Backcolor="#cccccc"
> AutoGenerateColumns="False"
> onitemdatabound="myDataGrid_ItemDataBound"
> >
>
>
>
> <Columns>
> <asp:boundcolumn readonly="true"
> HeaderText="ItemID" DataField="ItemID" />
> <asp:boundcolumn readonly="true"
> HeaderText="ItemTypeID" DataField="ItemTypeID" />
> <asp:boundcolumn readonly="true"
> HeaderText="ItemTitle" DataField="ItemTitle" />
> <asp:boundcolumn readonly="true"
> HeaderText="ItemDescription" DataField="ItemDescription" />
>
> <asp:TemplateColumn>
> <ItemTemplate>
>
> </ItemTemplate>
> </asp:TemplateColumn>
>
> <asp:boundcolumn readonly="true"
> HeaderText="PostedBy" DataField="PostedBy" />
> <asp:boundcolumn readonly="true"
> HeaderText="PostedDate" DataField="PostedDate" />
> <asp:boundcolumn readonly="true"
> HeaderText="ItemParent" DataField="ItemParent" />
> <asp:boundcolumn readonly="true"
> HeaderText="SortOrder" DataField="SortOrder" />
> <asp:boundcolumn readonly="true"
> HeaderText="ItemRating" DataField="ItemRating" />
> <asp:boundcolumn readonly="true"
> HeaderText="ModeratedYN" DataField="ModeratedYN" />
> <asp:boundcolumn readonly="true"
> HeaderText="JPStillToReplyYN" DataField="JPStillToReplyYN" />
> <asp:boundcolumn readonly="true"
> HeaderText="ArticleOriginalTitle" DataField="ArticleOriginalTitle" />
> </Columns>
>
>
> </asp:dataGrid>
>
>
>
> </form>
> </body></html>
> $subst('Email.Unsub').
>
>
Message #3 by "Jon Maz" <jonmaz@s...> on Thu, 28 Feb 2002 17:18:22
|
|
|
|
 |