Home All Groups Group Topic Archive Search About
Author
27 Nov 2006 12:37 PM
Kevin Humphreys
Hi,
Can someone give me a code look at how I can create a Dynamic Checkbox
within a repeater using VB.NET?

Thanks,
Kevin.

Author
28 Nov 2006 8:58 AM
Velislav
Ok, here's a quick sample:

this is a snippet out of the aspx page:

    <table>
        <asp:Repeater runat="server" ID="myRepeater">
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:CheckBox runat="server" ID="myCheckBox" />
                    </td>
                </tr>
            </ItemTemplate>
        </asp:Repeater>
    </table>
    <asp:Button ID="doPostBack" runat="server" Text="Do PostBack" />

in the code, I show how to bind the checkboxes and then access whatever
their values are on postback:

Protected Sub myRepeater_ItemDataBound(ByVal sender As Object, ByVal e
As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
myRepeater.ItemDataBound
        If (e.Item.ItemType = ListItemType.Item Or e.Item.ItemType =
ListItemType.AlternatingItem) Then
            Dim myCheckBox As CheckBox =
CType(e.Item.FindControl("myCheckBox"), CheckBox)
            myCheckBox.Checked = CBool(e.Item.DataItem)
        End If
    End Sub

    Public Overrides Sub DataBind()
        Dim checkBoxValues(2) As Boolean
        checkBoxValues(0) = True
        checkBoxValues(1) = False
        checkBoxValues(2) = True
        myRepeater.DataSource = checkBoxValues
        MyBase.DataBind()
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            DataBind()
        End If
    End Sub

    Protected Sub doPostBack_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles doPostBack.Click
        Dim index As Integer
        While (index < myRepeater.Items.Count)
            Dim myCheckBox As CheckBox =
CType(myRepeater.Items(index).FindControl("myCheckBox"), CheckBox)
            Dim output As LiteralControl = New
LiteralControl(String.Format("Item index: {0}; CheckBox Checked:
{1};<br>", index, myCheckBox.Checked.ToString()))
            Page.Controls.Add(output)
            index += 1
        End While
    End Sub

Kevin Humphreys wrote:
Show quoteHide quote
> Hi,
> Can someone give me a code look at how I can create a Dynamic Checkbox
> within a repeater using VB.NET?
>
> Thanks,
> Kevin.
Author
28 Nov 2006 9:06 AM
Velislav
ugh.... sorry, if you want to create the CheckBox programatically, then
all you do is create it in the ItemCreated event handler of the
repeater:

    Protected Sub myRepeater_ItemCreated(ByVal sender As Object, ByVal
e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles
myRepeater.ItemCreated
        Dim myDynamicCheckBox As CheckBox = New CheckBox()
        myDynamicCheckBox.ID = "myDynamicCheckBox"
        myDynamicCheckBox.Checked = False
        e.Item.Controls.Add(myDynamicCheckBox)
    End Sub

then access it the same way as the previous way, just use the correct
ID (ie. myDynamicCheckBox) in as the parameter of the FindControl call.


Kevin Humphreys wrote:
Show quoteHide quote
> Hi,
> Can someone give me a code look at how I can create a Dynamic
Checkbox
> within a repeater using VB.NET?
>
> Thanks,
> Kevin.