We help IT Professionals succeed at work.

adding some data from my databind into a javascript function

scm0sml
scm0sml asked
on
I have my data list:
<asp:DataList runat="server" ID="dlCabins" RepeatDirection="horizontal" RepeatColumns="10">
                                <HeaderTemplate></HeaderTemplate>
                                <ItemTemplate>
                                    <table border="1" style="text-align:center;">
                                        <tr>
                                            <td>
                                                <i><asp:Label runat="server" ID="lblDisplayCount" Text="<%# Container.ItemIndex + 1 %>"></asp:Label></i><br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td width="35px">
                                                <asp:Label runat="server" ID="lblCabinNumber" Text="<%# Container.Dataitem().CabinNumber %>"></asp:Label><br />
                                            </td>
                                        </tr>
                                        <tr>
                                            <td>
                                                <asp:CheckBox runat="server" ID="chkCabinSelected" Checked="false" />                                                
                                            </td>
                                        </tr>
                                    </table>
                                </ItemTemplate>
                            </asp:DataList>

In my code behind I have:
Private Sub dlCabins_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlCabins.ItemDataBound
            Select Case e.Item.ItemType
                Case ListItemType.Header
                Case ListItemType.AlternatingItem, ListItemType.Item
                    Select Case _enmMode
                        Case enumScreenMode.modeEditBasket
                            Dim checkbox As CheckBox
                            checkbox = e.Item.FindControl("chkCabinSelected")

                            checkbox.Attributes.Add("onclick", "CheckSingleCheckbox(this)")

                        Case enumScreenMode.modeToursBooking
                            'todo add script to constrain number of selections to _intNoCabinsRequired
                    End Select
            End Select
        End Sub

As you can see I add on the onclick attribute to calling my function passing in the checkbox itself.

What I need to do is also pass in a value from the data that I am binding,

So that whereas in my datalist i use:
<asp:Label runat="server" ID="lblCabinNumber" Text="<%# Container.Dataitem().CabinNumber %>"></asp:Label><br />
for instance.....

I need to pass - <%# Container.Dataitem().CruiseCabin_ID %> into the javascript function.

So how can i do this in the code behind at line:
checkbox.Attributes.Add("onclick", "CheckSingleCheckbox(this)")


Comment
Watch Question

Oliver AmayaEntrepeneur

Commented:
Hi, I think this should be what you need:
Private Sub dlCabins_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataListItemEventArgs) Handles dlCabins.ItemDataBound
 
  Dim drv as DataRowView = CType(e.Item.DataItem, DataRowView) ' <-- Added this '
 
  Select Case e.Item.ItemType
    Case ListItemType.Header
    Case ListItemType.AlternatingItem, ListItemType.Item
      Select Case _enmMode
        Case enumScreenMode.modeEditBasket
          Dim checkbox As CheckBox
          checkbox = e.Item.FindControl("chkCabinSelected")
 
          checkbox.Attributes.Add("onclick", "CheckSingleCheckbox(this, " & drv("CruiseCabin_ID") & ")") ' <-- Added this '
 
        Case enumScreenMode.modeToursBooking
          'todo add script to constrain number of selections to _intNoCabinsRequired '
      End Select
  End Select
End Sub

Open in new window

Author

Commented:
I am binding a list of my own objects to the data list.

So I am getting the erro:
System.InvalidCastException: Unable to cast object of type 'Comtec.Travelink82.Libraries.TLDataObjects.ComponentSetUp.Tours.Cruise.CruiseCabinOnToursInd' to type 'System.Data.DataRowView'.
   at Comtec.Travelink82.Application.Booking.TailorMade.Cruise.CharterCruiseCabinSelect.dlCabins_ItemDataBound(Object sender, DataListItemEventArgs e)
   at System.Web.UI.WebControls.DataList.OnItemDataBound(DataListItemEventArgs e)
   at System.Web.UI.WebControls.DataList.CreateItem(Int32 itemIndex, ListItemType itemType, Boolean dataBind, Object dataItem)
   at System.Web.UI.WebControls.DataList.CreateControlHierarchy(Boolean useDataSource)
   at System.Web.UI.WebControls.BaseDataList.OnDataBinding(EventArgs e)
   at System.Web.UI.WebControls.BaseDataList.DataBind()
   at Comtec.Travelink82.Application.Booking.TailorMade.Cruise.CharterCruiseCabinSelect.LoadScreen()"

Author

Commented:
any ideas?
Entrepeneur
Commented:
ok, maybe this would work?
Dim drv as CruiseCabinOnToursInd = CType(e.Item.DataItem, CruiseCabinOnToursInd)}
checkbox.Attributes.Add("onclick", "CheckSingleCheckbox(this, " & drv.CruiseCabin_ID & ")") '

Open in new window

Author

Commented:
yep worked fine, thanks for that!!

Explore More ContentExplore courses, solutions, and other research materials related to this topic.