RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: "Phillip Williams" <Phillip.Williams@xxxxxxxxxxxx>
- Date: Thu, 19 Jan 2006 12:06:03 -0800
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)
> > > > > >
> > > > > > Try
> > > > > > dvwSelect = New DataView(Parent_Data_Table(sCmd))
> > > > > > If dvwSelect.Table.Rows.Count <> 0 Then
> > > > > > dlstParent1.DataSource = dvwSelect
> > > > > > dlstParent1.DataBind()
> > > > > > Else
> > > > > > Session("DataList_ParentDetailControls") = Nothing
> > > > > > pnlDetail.Visible = False
> > > > > > End If
> > > > > > Catch ex As SqlException
> > > > > > lblErrorMsg.Text = ex.message.ToString
> > > > > > Catch ex As Exception
> > > > > > lblErrorMsg.Text = ex.Message.ToString
> > > > > > Finally
> > > > > > dvwSelect.Dispose()
> > > > > > End Try
> > > > > >
> > > > > > "Phillip Williams" wrote:
> > > > > >
> > > > > > > Hi Erik,
> > > > > > >
> > > > > > > Good morning to you too and welcome back to the newsgroup. Regarding your
> > > > > > > code:
> > > > > > > 1- Server control markup renders into HTML markup when it is processed by
> > > > > > > the ASP.NET engine. But when you placed it inside the label text, the label
> > > > > > > control will assume that is a regular HTML markup and would not translate it
> > > > > > > from being server control markup to HTML
> > > > > > > 2- The BindParentLabelColumn would not execute unless you add an event
> > > > > > > hanlder to the lable.databinding event. You could have also wrote the logic
> > > > > > > within the DataList.ItemDataBound event to achieve the same outcome.
> > > > > > > 3- If you want your page to handle the click events of the dynamically
> > > > > > > created image buttons you should databind the list during the Page.Init stage.
> > > > > > >
> > > > > > > Here is how I would modify your code:
> > > > > > >
> > > > > > > 'starting during the initialization phase of the page's lifecycle
> > > > > > > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
> > > > > > > System.EventArgs) Handles MyBase.Init
> > > > > > > InitializeComponent()
> > > > > > > 'Dynamically created controls should be created during the
> > > > > > > 'page initializtion stage for the control to maintain their viewstate
> > > > > > > BindControls()
> > > > > > > End Sub
> > > > > > >
> > > > > > > Private Sub BindControls()
> > > > > > > 'Explicity cast the session variable to its appropriate type before
> > > > > > > using it
> > > > > > > datalist1.DataSource =
> > > > > > > CType(Session("DataList_ParentDetailControls"), DataTable)
> > > > > > > datalist1.DataBind()
> > > > > > >
> > > > > > > End Sub
> > > > > > >
> > > > > > > Private Sub DataList1_ItemCreated(ByVal sender As Object, ByVal e As
> > > > > > > DataListItemEventArgs) Handles datalist1.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
> > > > > > >
> > > > > > > Public Sub BindParentLabelColumn(ByVal sender As Object, ByVal e As
> > > > > > > EventArgs)
> > > > > > > Dim lbl As Label = CType(sender, Label)
> > > > > > > Dim container As DataListItem = CType(lbl.NamingContainer,
> > > > > > > DataListItem)
> > > > > > > 'the datalistitem has a pointer to the DataItem (which is datarowview)
> > > > > > > 'of your datatable. Therefore you do not need to retrieve the
> > > > > > > datatable
> > > > > > > 'from the session
> > > > > > > Dim drv As DataRowView = container.DataItem
> > > > > > > Dim dt As DataTable = drv.Row.Table
> > > > > > > Dim dc As DataColumn
> > > > > > > Dim sColumnValue As String
> > > > > > > Dim sColumnName As String
> > > > > > >
> > > > > > >
> > > > > > > For Each dc In dt.Columns
> > > > > > > '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"
> > > > > > > lbl.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
> > > > > > > lbl.Controls.Add(UrlLabel)
> > > > > > >
> > > > > > > Next
> > > > > > >
> > > > > > > End Sub
> > > > > > > Private Sub DataList1_ItemCommand(ByVal sender As Object, ByVal e As
> > > > > > > DataListCommandEventArgs) Handles datalist1.ItemCommand
> > > > > > > 'This is the method that would handle the click event of the
> > > > > > > dynamically created imagebuttons
> > > > > > > End Sub
> > > > > > > --
> > > > > > > HTH,
> > > > > > > Phillip Williams
> > > > > > > http://www.societopia.net
> > > > > > > http://www.webswapp.com
> > > > > > >
> > > > > > >
> > > > > > > "Erik" wrote:
> > > > > > >
> > > > > > > > Good Morning,
> > > > > > > > I am trying to dynamically generate an ImageButton in a datalist label
> > > > > > > > column. When I have a static reference to the image 'image/plus.gif', the
> > > > > > > > image is displayed in the label column.
> > > > > > > >
> > > > > > > > However, when I try to adapt the html code to be dynamically defined in the
> > > > > > > > binding of the label column, the image is not displayed. Below is the sub
> > > > > > > > BindParentLabelColumn where the label text is assigned.
> > > > > > > > Any insight on the issue is greatly appreciated - I am stumped.
> > > > > > > > Thank you, Erik
> > > > > > > >
> > > > > > > > Public Sub BindParentLabelColumn(ByVal sender As Object, ByVal e As EventArgs)
> > > > > > > > Dim lbl As Label = CType(sender, Label)
> > > > > > > > Dim strvals As String
> > > > > > > > Dim container As DataListItem = CType(lbl.NamingContainer,
> > > > > > > > DataListItem)
> > > > > > > > Dim dt As DataTable = Session("DataList_ParentDetailControls")
> > > > > > > > Dim dc As DataColumn
> > > > > > > > Dim sColumnValue As String
> > > > > > > > Dim sColumnName As String
> > > > > > > >
> > > > > > > > strvals = "<tr>"
> > > > > > > >
> > > > > > > > For Each dc In dt.Columns
> > > > > > > > sColumnName = dc.ColumnName
> > > > > > > > sColumnValue =
> > > > > > > > Convert.ToString(DataBinder.Eval(CType(container, DataListItem).DataItem,
> > > > > > > > dc.ColumnName))
> > > > > > > >
> > > > > > > > strvals &= "<TD align='center' width='10'><asp:ImageButton
> > > > > > > > ID='Expand" + sColumnName + "' runat='server' CommandName='Select" +
> > > > > > > > sColumnName + "' ImageUrl='images/plus.gif'></asp:ImageButton></TD>" & _
> > > > > > > > "<TD align='center' width='80'>" + sColumnValue +
> > > > > > > > "</TD>"
> > > > > > > > End If
> > > > > > > > Next
> > > > > > > >
> > > > > > > > strvals &= "</tr>"
> > > > > > > >
> > > > > > > > lbl.Text = strvals
> > > > > > > > End Sub 'BindParentLabelColumn
.
- Follow-Ups:
- References:
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
- From: Phillip Williams
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Erik
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Phillip Williams
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Erik
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Phillip Williams
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Phillip Williams
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- From: Erik
- RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataList
- Prev by Date: RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- Next by Date: RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- Previous by thread: RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- Next by thread: RE: ITemplate - Dynamic ImageButton in BindLabelColumn for DataLis
- Index(es):
Relevant Pages
|