RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Good morning Erik,

Yes you are right on both counts; I missed uploading the source code
therefore you only found the old demo's source code; and the second one
(namely repeating the CreatTable method) was a typo on my behalf. I guess I
was too much engaged in watching the elections results on TV and listening to
the speech of our new Prime Minister that I was not fully paying attention
when I uploaded the demo. I corrected it now.


--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com


"Erik" wrote:

> Good Morning Phillip,
> I was unable to see in your code where imgBtn.ImageUrl is assigned to
> left.gif. Would you please point out in which function / sub the assignment
> takes place?
> Another question I had was calling the 'CreateTable' function twice in
> Page_Init. Isn't datagrid1.DataSource = CreateTable() sufficient to create
> the table for databinding?
> Thanks, Erik
> "Phillip Williams" wrote:
>
> > Hi Erik,
> >
> > You are quite welcome. To be able to refer to the buttons you need to give
> > them IDs (I noticed that you used the commandName; while I would prefer to
> > use the CommandArgument). I modified the original demo accordingly:
> > http://www.societopia.net/samples/DataGrid_DynamicallyCreatedColumns.aspx
> >
> > As for formatting a number as currency you can use the DataFormatString
> > property of the BoundColumn like this DataFormatString = "{0:C}"
> >
> > --
> > HTH,
> > Phillip Williams
> > http://www.societopia.net
> > http://www.webswapp.com
> >
> >
> > "Erik" wrote:
> >
> > > Good Morning Phillip,
> > > I hope you enjoyed the weekend. I have everything working, and just have
> > > two questions regarding the dynamically created controls (imagebutton), and
> > > formatting of the databound template columns.
> > > When the user clicks the 'plus' button to expand the detail information, the
> > > button originally was updated to a 'minus' button. Below is the adapted code
> > > for the dynamically created controls:
> > > btnDetails =
> > > CType(DataGrid1.Items(e.Item.ItemIndex).FindControl(e.CommandName),
> > > ImageButton)
> > > btnDetails = CType(e.Item.FindControl(e.CommandName.ToString), ImageButton)
> > > If Not (btnDetails Is Nothing) Then
> > > btnDetails.ImageUrl = "images/minus.gif"
> > > Else
> > > lblErrorMsg.Text = "Control " & e.CommandName.ToString & " not found."
> > > End If
> > > The control is not being found, and I can't find the issue with the code.
> > > Next, I was wondering if there is a way to format decimal values for the
> > > databound template columns. I have been able to accomplish some formatting
> > > through the stylesheet, but can't figure out how to explicitly define some
> > > columns value to display as currency (in Sub DataGrid1_DataBinding1). For
> > > example, if dc.ColumnName = "Price" then display the value as currency.
> > > Thank you again for your help. You have again provided wonderful
> > > instruction and patience.
> > > Erik
> > >
> > >
> > > "Erik" wrote:
> > >
> > > > Hi Phillip,
> > > > I have your example working locally, and am working on making it data-driven.
> > > > Thanks,
> > > > Erik
> > > >
> > > > "Phillip Williams" wrote:
> > > >
> > > > > Hi Erik,
> > > > >
> > > > > Can you post the code. (I know this page is getting pretty long, so you
> > > > > might want to chop off the history out of it)
> > > > >
> > > > > Also, have you looked at the demo using the datagrid that I posted. It
> > > > > might prove easier in getting a tidy layout.
> > > > > --
> > > > > HTH,
> > > > > Phillip Williams
> > > > > http://www.societopia.net
> > > > > http://www.webswapp.com
> > > > >
> > > > >
> > > > > "Erik" wrote:
> > > > >
> > > > > > Good Morning Phillip,
> > > > > > Thanks you for the instruction. I have been working through the code, and
> > > > > > can't figure out an exception I am getting on line Dim drv As DataRowView =
> > > > > > e.Item.DataItem() in dlstParent1_ItemDataBound. e is Nothing.
> > > > > > Thanks, Erik
> > > > > > "Phillip Williams" wrote:
> > > > > >
> > > > > > > I managed to put a concept demo that combines the techniques we referred to
> > > > > > > below: 1) creating templates dynamically, and 2) creating controls
> > > > > > > dynamically, based on an undetermined number of columns in a datatable. I
> > > > > > > used here the datagrid instead of the datalist.
> > > > > > >
> > > > > > > http://www.societopia.net/Samples/DataGrid_DynamicallyCreatedColumns.aspx
> > > > > > >
> > > > > > > --
> > > > > > > HTH,
> > > > > > > Phillip Williams
> > > > > > > http://www.societopia.net
> > > > > > > http://www.webswapp.com
> > > > > > >
> > > > > > >
> > > > > > > "Phillip Williams" wrote:
> > > > > > >
> > > > > > > > Hi Erik,
> > > > > > > >
> > > > > > > > We have just displayed any number of controls (instead of HTML table
> > > > > > > > columns) into one label (which is inside one ItemTemplate).
> > > > > > > >
> > > > > > > > This was just a modification to your original code to explain to you the
> > > > > > > > concept of building the controls dynamically. (Instead of concatenating
> > > > > > > > markup text that you started with, you now learned that you could add the
> > > > > > > > controls dynamically within the page initialization stage to maintain their
> > > > > > > > functions)
> > > > > > > >
> > > > > > > > To reach to a full production outcome of the application that you desire,
> > > > > > > > you can build on the concepts of adding dynamic controls that we achieved in
> > > > > > > > this code by refining the way you place the controls.
> > > > > > > >
> > > > > > > > For example, instead of using one label you can build a Table within the
> > > > > > > > DataListItem. You can start by trying a simple markup like this:
> > > > > > > >
> > > > > > > > <asp:DataList ID="datalist1" Runat='server' CssClass="Container">
> > > > > > > > <ItemTemplate>
> > > > > > > > <%--leave it empty to be populated programmatically based on # of
> > > > > > > > columns--%>
> > > > > > > > </ItemTemplate>
> > > > > > > > </asp:DataList>
> > > > > > > >
> > > > > > > > And then move the code where you created the buttons within the stage of
> > > > > > > > processing the DataList.ItemDataBound event like this:
> > > > > > > >
> > > > > > > > Private Sub DataList1_ItemDataBound(ByVal sender As Object, ByVal e As
> > > > > > > > DataListItemEventArgs) Handles datalist1.ItemDataBound
> > > > > > > > Dim drv As DataRowView = e.Item.DataItem()
> > > > > > > > Dim dt As DataTable = drv.Row.Table
> > > > > > > > Dim dc As DataColumn
> > > > > > > > Dim sColumnValue As String
> > > > > > > > Dim sColumnName As String
> > > > > > > > 'each row would be formatted as a table that contains one row and as
> > > > > > > > many cells as there
> > > > > > > > 'are columns on the datatable
> > > > > > > > Dim t As New Table
> > > > > > > > e.Item.Controls.Add(t) 'add the table to the datalist row
> > > > > > > > Dim r As New TableRow
> > > > > > > > t.Rows.Add(r)
> > > > > > > >
> > > > > > > > For Each dc In dt.Columns
> > > > > > > > Dim c As New TableCell
> > > > > > > > r.Cells.Add(c)
> > > > > > > > 'you have to add the imagebutton control as a control object to
> > > > > > > > the
> > > > > > > > 'controls collection of the label so that it would be rendered
> > > > > > > > by the
> > > > > > > > 'asp.net engine as a server control
> > > > > > > > Dim btnImg As New ImageButton
> > > > > > > > sColumnName = dc.ColumnName
> > > > > > > > sColumnValue = drv(sColumnName)
> > > > > > > > btnImg.ID = "Expand" + sColumnName
> > > > > > > > btnImg.CommandName = "Select" + sColumnName
> > > > > > > > btnImg.ImageUrl = "images/plus.gif"
> > > > > > > > c.Controls.Add(btnImg)
> > > > > > > > 'add the description beside the image button also as a label
> > > > > > > > control
> > > > > > > > 'to the original label's controls collection
> > > > > > > > Dim UrlLabel As New Label
> > > > > > > > UrlLabel.Text = sColumnValue
> > > > > > > > c.Controls.Add(UrlLabel)
> > > > > > > > Next
> > > > > > > >
> > > > > > > > End Sub
> > > > > > > >
> > > > > > > > Once you get the hang of this you might try a one-more step complex
> > > > > > > > approach: to dynamically create the ItemTemplates as in this sample:
> > > > > > > > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vbcon/html/vbtskcreatingwebservercontroltemplatesdynamically.asp
> > > > > > > >
> > > > > > > > If you still cannot get it to work, let me know and I might put a demo of it
> > > > > > > > on my website.
> > > > > > > > --
> > > > > > > > HTH,
> > > > > > > > Phillip Williams
> > > > > > > > http://www.societopia.net
> > > > > > > > http://www.webswapp.com
> > > > > > > >
> > > > > > > >
> > > > > > > > "Erik" wrote:
> > > > > > > >
> > > > > > > > > Is there a way I can define the ItemTemplate labels dynamically in code? The
> > > > > > > > > number and definition of columns displayed is selected by the user.
> > > > > > > > > Thanks, Erik
> > > > > > > > > "Phillip Williams" wrote:
> > > > > > > > >
> > > > > > > > > > Hi Erik,
> > > > > > > > > >
> > > > > > > > > > That's alright. Label1 is the id of the label that I placed within the
> > > > > > > > > > DataList markup. You have to give the label an Id to be able to identify it
> > > > > > > > > > using the FindControl method in the Codebehind.
> > > > > > > > > >
> > > > > > > > > > <asp:DataList ID="datalist1" Runat='server' >
> > > > > > > > > > <ItemTemplate>
> > > > > > > > > > <asp:Label ID="Label1" Runat="server"></asp:Label>"
> > > > > > > > > > </ItemTemplate>
> > > > > > > > > > </asp:DataList>
> > > > > > > > > >
> > > > > > > > > > --
> > > > > > > > > > HTH,
> > > > > > > > > > Phillip Williams
> > > > > > > > > > http://www.societopia.net
> > > > > > > > > > http://www.webswapp.com
> > > > > > > > > >
> > > > > > > > > >
> > > > > > > > > > "Erik" wrote:
> > > > > > > > > >
> > > > > > > > > > > Hi Phillip,
> > > > > > > > > > > The error is still occurring in
> > > > > > > > > > > Dim myLabel As Label = e.Item.FindControl("Label1")
> > > > > > > > > > > AddHandler myLabel.DataBinding, AddressOf BindParentLabelColumn
> > > > > > > > > > > with the addition below. The ID for the item is Nothing. I can't figure
> > > > > > > > > > > out what the control 'Label1' refers to since the controls are being created
> > > > > > > > > > > for the DataList. I apologize for my confusion, but this is new territory
> > > > > > > > > > > for me again.
> > > > > > > > > > > Thanks, Erik
> > > > > > > > > > >
> > > > > > > > > > > "Phillip Williams" wrote:
> > > > > > > > > > >
> > > > > > > > > > > > Correction; the condition should be:
> > > > > > > > > > > >
> > > > > > > > > > > > If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
> > > > > > > > > > > > ListItemType.AlternatingItem Then
> > > > > > > > > > > > '....
> > > > > > > > > > > > End IF
> > > > > > > > > > > >
> > > > > > > > > > > > "Phillip Williams" wrote:
> > > > > > > > > > > >
> > > > > > > > > > > > > Hi Erik,
> > > > > > > > > > > > >
> > > > > > > > > > > > > Yes, of course. I exptected you to fill in those tiny details :-) but here
> > > > > > > > > > > > > is a checked code:
> > > > > > > > > > > > >
> > > > > > > > > > > > > Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
> > > > > > > > > > > > > DataListItemEventArgs) Handles datalist1.ItemCreated
> > > > > > > > > > > > > If e.Item.ItemType = ListItemType.Item And e.Item.ItemType =
> > > > > > > > > > > > > ListItemType.AlternatingItem Then
> > > > > > > > > > > > > Dim myLabel As Label = e.Item.FindControl("Label1")
> > > > > > > > > > > > > If Not myLabel Is Nothing Then
> > > > > > > > > > > > > 'this step wires up the label databinding event to a method
> > > > > > > > > > > > > AddHandler myLabel.DataBinding, AddressOf
> > > > > > > > > > > > > BindParentLabelColumn
> > > > > > > > > > > > > End If
> > > > > > > > > > > > > End If
> > > > > > > > > > > > > End Sub
> > > > > > > > > > > > >
> > > > > > > > > > > > > --
> > > > > > > > > > > > > HTH,
> > > > > > > > > > > > > Phillip Williams
> > > > > > > > > > > > > http://www.societopia.net
> > > > > > > > > > > > > http://www.webswapp.com
> > > > > > > > > > > > >
> > > > > > > > > > > > >
> > > > > > > > > > > > > "Erik" wrote:
> > > > > > > > > > > > >
> > > > > > > > > > > > > > Hi Phillip,
> > > > > > > > > > > > > > It appears the following is causing the error, with an itemtype = 'header'.
> > > > > > > > > > > > > > Should there be code added to verify the itemtype = 'item'. Thanks, Erik
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > Private Sub dlstParent1_ItemCreated(ByVal sender As Object, ByVal e As
> > > > > > > > > > > > > > DataListItemEventArgs) Handles dlstParent1.ItemCreated
> > > > > > > > > > > > > > Dim myLabel As Label = e.Item.FindControl("Label1")
> > > > > > > > > > > > > > 'this step wires up the label databinding event to a method
> > > > > > > > > > > > > > AddHandler myLabel.DataBinding, AddressOf BindParentLabelColumn
> > > > > > > > > > > > > > End Sub
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > "Phillip Williams" wrote:
> > > > > > > > > > > > > >
> > > > > > > > > > > > > > > Hi Erik,
> > > > > > > > > > > > > > >
> > > > > > > > > > > > > > > Using VS.Net debugger, set up break points within the methods that are
> > > > > > > > > > > > > > > triggered upon databinding (of both the datalist and the label) and step
> > > > > > > > > > > > > > > through them. Somewhere in the code that you did not post, you will find the
> > > > > > > > > > > > > > > line that threw that exception.
> > > > > > > > > > > > > > > --
> > > > > > > > > > > > > > > HTH,
> > > > > > > > > > > > > > > Phillip Williams
> > > > > > > > > > > > > > > http://www.societopia.net
> > > > > > > > > > > > > > > http://www.webswapp.com
> > > > > > > > > > > > > > >
> > > > > > > > > > > > > > >
> > > > > > > > > > > > > > > "Erik" wrote:
> > > > > > > > > > > > > > >
> > > > > > > > > > > > > > > > Hi Phillip,
> > > > > > > > > > > > > > > > Good to hear from you also.
> > > > > > > > > > > > > > > > I am encountering an error (Object reference not set to an instance of an
> > > > > > > > > > > > > > > > object.) on line dlstParent1.DataBind after populating the Parent_Data_Table
> > > > > > > > > > > > > > > > with the SQL string defined from the user's parameter selection. Do I have
> > > > > > > > > > > > > > > > some redundant code which is causing a conflict?
> > > > > > > > > > > > > > > >
> > > > > > > > > > > > > > > > dlstParent1.HeaderTemplate =
> > > > > > > > > > > > > > > > New DatalistParentLabelColumn(ListItemType.Header)
> > > > > > > > > > > > > > > > dlstParent1.ItemTemplate =
> > > > > > > > > > > > > > > > New DatalistParentLabelColumn(ListItemType.Item)
.



Relevant Pages

  • RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
    ... them IDs (I noticed that you used the commandName; ... As for formatting a number as currency you can use the DataFormatString ... "Erik" wrote: ... >>> Phillip Williams ...
    (microsoft.public.dotnet.framework.aspnet.webcontrols)
  • [OT] Re: painters: The good ole flame wars ...
    ... Hey, Erik, if you read this: Hope you are doing well - as good as it can be!! ... with it tracing the entry, the exit, the source code line (is there a __LINE__ ...
    (comp.lang.lisp)
  • Re: Secure src code management
    ... > some type of source code control system, but it has to be secure. ... CVS can use an SSH tunnel to communicate with a server. ... Erik de Castro Lopo nospam@mega-nerd.com ...
    (comp.security.unix)
  • Re: [9fans] plan 9 regexp
    ... there is very little source code there. ...  why not dump the configure ... goo and use p9p instead? ...
    (comp.os.plan9)
  • Re: [9fans] plan 9 regexp
    ... there is very little source code there. ... why not dump the configure ... goo and use p9p instead? ...
    (comp.os.plan9)