|
code
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Repeater and CheckboxsHi,
Can someone give me a code look at how I can create a Dynamic Checkbox within a repeater using VB.NET? Thanks, Kevin. 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. 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.
Charat Control (date Vs date)
Dynamicly setting readonly in through a property Get Datakey Value from Gridview/sqldatasource How to render HTML in a textbox? Help Me WinForm controls in Web Forcing datalist to start new row on demand Question to CreateUserWizard and database Zomm In and Zoom Out Radio Button Required Field Validator |
|||||||||||||||||||||||