child controls inside a user control inside a datalist

Discussion in 'ASP .Net Web Controls' started by Roman, Jan 3, 2005.

  1. Roman

    Roman Guest

    I have built a simple user control that contains 2 buttons, a text box and a
    dropdownlist. When a button is clicked it sets the visible property
    of the textbox/dropdownlist and the button. ie the buttons allow me to
    toggle between the textbox and the dropdownlist. This works fine when the
    control
    is placed on the page but when it is placed inside a datalist, ie the
    itemtemplate, the onclick events for the buttons fire but the visibility is
    not set.

    How do I change the visibility of the child controls of a user control when
    it is placed inside a datalist?

    I have placed some code below in the hope that it helps.Thanks in
    advance.***************this is my user control************************


    <%@ Control Language="vb" AutoEventWireup="false"
    Codebehind="ComboBox.ascx.vb" Inherits="globalDocWeb.ComboBoxUserControl"
    TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    <nobr>
    <asp:textbox id="cmbTxt" runat="server" ></asp:textbox>
    <asp:button id="btnDdl" runat="server" Text="ddl"
    Width="32px"></asp:button>
    </nobr>

    <nobr><asp:dropdownlist visible=False id="cmbDdl" runat="server" >
    </asp:dropdownlist><asp:button visible=False id="btnTxt" runat="server"
    Text="txt" Width="32px"></asp:button>
    </nobr>



    ***********this is the code behind of the user
    control****************************



    Public Class ComboBoxUserControl
    Inherits System.Web.UI.UserControl


    <System.Diagnostics.DebuggerStepThrough()> Private Sub
    InitializeComponent()

    End Sub
    Protected WithEvents cmbDdl As System.Web.UI.WebControls.DropDownList
    Protected WithEvents cmbTxt As System.Web.UI.WebControls.TextBox
    Protected WithEvents btnDdl As System.Web.UI.WebControls.Button
    Protected WithEvents btnTxt As System.Web.UI.WebControls.Button

    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    System.EventArgs) Handles MyBase.Init
    InitializeComponent()
    End Sub

    Private Sub btnDdl_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnDdl.Click
    cmbTxt.Visible = False
    btnDdl.Visible = False
    cmbDdl.Visible = True
    btnTxt.Visible = True
    End Sub

    Private Sub btnTxt_Click(ByVal sender As Object, ByVal e As
    System.EventArgs) Handles btnTxt.Click
    cmbTxt.Visible = True
    btnDdl.Visible = True
    cmbDdl.Visible = False
    btnTxt.Visible = False
    End Sub


    End Class

    *********this is how I access the control within the itemdatabound event of
    the datalist*******

    Dim list As ComboBoxUserControl = CType(e.Item.FindControl("Name"),
    ComboBoxUserControl)
     
    Roman, Jan 3, 2005
    #1
    1. Advertising

  2. Hi Roman,

    I'm not exactly sure why you're needing to do, but your problem may be that
    you're not getting references to the controls inside your user control.
    Below is a quick sample that fills the textboxes with data.

    Let us know if this helps?

    Ken
    Microsoft MVP [ASP.NET]
    Toronto

    Private Sub Page_Load _
    (ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles MyBase.Load
    If Not IsPostBack Then
    DataList1.DataSource = CreateDataSource()
    DataList1.DataBind()
    End If
    End Sub
    Private Sub DataList1_ItemDataBound _
    (ByVal sender As Object, ByVal e As _
    System.Web.UI.WebControls.DataListItemEventArgs) _
    Handles DataList1.ItemDataBound
    ' By Ken Cox - Microsoft MVP
    ' January 3/05
    Dim txtbx As TextBox
    Dim ddl As DropDownList
    Dim drv As DataRowView
    ' Get a reference to the user control
    Dim list As ComboBox = CType(e.Item.FindControl("Name"), ComboBox)
    ' Check that we got the reference and proceed
    If Not IsNothing(list) Then
    ' Get a reference to the textbox
    txtbx = list.FindControl("cmbTxt")
    ' Check that we got the reference and proceed
    If Not IsNothing(txtbx) Then
    ' Cast the DataItem to a DataRowView
    drv = CType(e.Item.DataItem, DataRowView)
    ' Pull the StringValue member out and put into the textbox
    txtbx.Text = drv.Row.Item("StringValue")
    End If
    End If
    End Sub
    Function CreateDataSource() As DataTable
    Dim dt As New DataTable
    Dim dr As DataRow
    dt.Columns.Add(New DataColumn _
    ("IntegerValue", GetType(Int32)))
    dt.Columns.Add(New DataColumn _
    ("StringValue", GetType(String)))
    dt.Columns.Add(New DataColumn _
    ("CurrencyValue", GetType(Double)))
    dt.Columns.Add(New DataColumn _
    ("Boolean", GetType(Boolean)))
    Dim i As Integer
    For i = 0 To 8
    dr = dt.NewRow()
    dr(0) = i
    dr(1) = "Item " + i.ToString()
    dr(2) = 1.23 * (i + 1)
    dr(3) = (i = 4)
    dt.Rows.Add(dr)
    Next i
    Return dt
    End Function 'CreateDataSource

    <form id="Form1" method="post" runat="server">
    <asp:datalist id="DataList1" runat="server">
    <itemtemplate>
    <p>
    <uc1:combobox id="Name"
    runat="server"></uc1:combobox></p>
    </itemtemplate>
    </asp:datalist>
    </form>


    "Roman" <> wrote in message news:crccqc$715$...
    >I have built a simple user control that contains 2 buttons, a text box and
    >a dropdownlist. When a button is clicked it sets the visible property
    > of the textbox/dropdownlist and the button. ie the buttons allow me to
    > toggle between the textbox and the dropdownlist. This works fine when the
    > control
    > is placed on the page but when it is placed inside a datalist, ie the
    > itemtemplate, the onclick events for the buttons fire but the visibility
    > is not set.
    >
    > How do I change the visibility of the child controls of a user control
    > when it is placed inside a datalist?
    >
    > I have placed some code below in the hope that it helps.Thanks in
    > advance.***************this is my user control************************
    >
    >
    > <%@ Control Language="vb" AutoEventWireup="false"
    > Codebehind="ComboBox.ascx.vb" Inherits="globalDocWeb.ComboBoxUserControl"
    > TargetSchema="http://schemas.microsoft.com/intellisense/ie5" %>
    > <nobr>
    > <asp:textbox id="cmbTxt" runat="server" ></asp:textbox>
    > <asp:button id="btnDdl" runat="server" Text="ddl"
    > Width="32px"></asp:button>
    > </nobr>
    >
    > <nobr><asp:dropdownlist visible=False id="cmbDdl" runat="server" >
    > </asp:dropdownlist><asp:button visible=False id="btnTxt" runat="server"
    > Text="txt" Width="32px"></asp:button>
    > </nobr>
    >
    >
    >
    > ***********this is the code behind of the user
    > control****************************
    >
    >
    >
    > Public Class ComboBoxUserControl
    > Inherits System.Web.UI.UserControl
    >
    >
    > <System.Diagnostics.DebuggerStepThrough()> Private Sub
    > InitializeComponent()
    >
    > End Sub
    > Protected WithEvents cmbDdl As System.Web.UI.WebControls.DropDownList
    > Protected WithEvents cmbTxt As System.Web.UI.WebControls.TextBox
    > Protected WithEvents btnDdl As System.Web.UI.WebControls.Button
    > Protected WithEvents btnTxt As System.Web.UI.WebControls.Button
    >
    > Private designerPlaceholderDeclaration As System.Object
    >
    > Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
    > System.EventArgs) Handles MyBase.Init
    > InitializeComponent()
    > End Sub
    >
    > Private Sub btnDdl_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btnDdl.Click
    > cmbTxt.Visible = False
    > btnDdl.Visible = False
    > cmbDdl.Visible = True
    > btnTxt.Visible = True
    > End Sub
    >
    > Private Sub btnTxt_Click(ByVal sender As Object, ByVal e As
    > System.EventArgs) Handles btnTxt.Click
    > cmbTxt.Visible = True
    > btnDdl.Visible = True
    > cmbDdl.Visible = False
    > btnTxt.Visible = False
    > End Sub
    >
    >
    > End Class
    >
    > *********this is how I access the control within the itemdatabound event
    > of the datalist*******
    >
    > Dim list As ComboBoxUserControl = CType(e.Item.FindControl("Name"),
    > ComboBoxUserControl)
    >
     
    Ken Cox [Microsoft MVP], Jan 4, 2005
    #2
    1. Advertising

  3. Roman

    Roman Guest

    "Ken Cox [Microsoft MVP]" <> wrote in message
    news:%...
    > Hi Roman,
    >
    > I'm not exactly sure why you're needing to do, but your problem may be
    > that you're not getting references to the controls inside your user
    > control. Below is a quick sample that fills the textboxes with data.
    >
    > Let us know if this helps?
    >
    > Ken
    > Microsoft MVP [ASP.NET]
    > Toronto



    Thanks for your reply Ken.

    I dont have any problems locating controls from within the itemdatabound
    event but rather handling events, ie onclick, raised by the child controls
    of my user control. If one of the buttons is clicked I need to locate the
    user control the event was raised from and then change the visibility of the
    child controls. The whole reason for this is that I am trying to simulate a
    ComboBox control wthout using any DHTML. When the user first opens the page
    they are presented with a textbox and a button. If they would rather choose
    items from a list they can click on the button and the textbox is hidden and
    the dropdownlist is displayed. So in other words I am allowing the user to
    toggle between a dropdown list and textbox. The problem is that I am unable
    to capture the click event properly to allow me to achieve this.

    Does that clear things up?

    Thanks again.
     
    Roman, Jan 4, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nevyn Twyll
    Replies:
    8
    Views:
    1,972
  2. Roman
    Replies:
    2
    Views:
    656
    =?Utf-8?B?UHJha2FzaC5ORVQ=?=
    Jan 4, 2005
  3. Replies:
    0
    Views:
    2,283
  4. Jeff Rodriguez
    Replies:
    23
    Views:
    1,210
    David Schwartz
    Dec 9, 2003
  5. Replies:
    0
    Views:
    220
Loading...

Share This Page