Search button, to search in Repeater - asp.net

I want to create a search button, so that I can search based on what I want to search in repeater table. I do not know how to it, cause I already try repGender.Rebind(), but still wrong... I do not know how to the behind code, as I know that we need:
Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
This is the UI, the table is in repeater
FRONT CODE
<label for="name">Jantina</label>
<asp:DropDownList ID="ddlGender" runat="server"></asp:DropDownList>
<asp:Button ID="BtnSearch" runat="server" Text="Search" />
<br/><br/>
<asp:Repeater ID="repGender" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th>Gender</th>
<th>Total</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("GENDER")%>
</td>
<td style="text-align: center">
<%# Eval("TOTAL")%></a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="font-weight: bold">
<td>Grand Total</td>
<td style="text-align: center">
<asp:Label runat="server" ID="lblTotal"></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
BEHIND CODE
Partial Class Statistics
Inherits App.Main
Dim MyTotal As Integer
Private Property Label As Object
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not IsPostBack Then
BindData()
ddlGender_Bind()
End If
End Sub
Sub BindData()
Dim dal As New DBWrapper.DAL(strConnectionString, strProviderName)
Dim dt As New DataTable
dal.DB.Parameters.Clear()
dal.DB.AddParameter("#GENDER", ddlGender.Text)
If dal.DB.ExecuteProcedure(dt, "GENDER_BB") Then
repGender.DataSource = dt
repGender.DataBind()
End If
End Sub
Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim gData As DataRowView = e.Item.DataItem
MyTotal = MyTotal + gData.Item("TOTAL")
End If
If e.Item.ItemType = ListItemType.Footer Then
' set the total value in footer
Dim lblTotal As Label = e.Item.FindControl("lblTotal")
lblTotal.Text = MyTotal
End If
End Sub
Public Sub ddlGender_Bind()
Dim dal As New DBWrapper.DAL(strConnectionString, strProviderName)
Dim dt As New DataTable
If dal.DB.ExecuteProcedure(dt, "GENDER_R") Then
Share.LoadList(ddlGender, "GENDER", "GENDER_ID", dt, Enums.MsgCode.PleaseSelect.ToString, ListOrder.ByValue)
End If
End Sub
Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
End Sub
End Class

Here I share the answer for people to refer...
Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Dim dal As New DBWrapper.DAL(strConnectionString, strProviderName)
Dim dt As New DataTable
dal.DB.Parameters.Clear()
dal.DB.AddParameter("#GENDER", ddlGender.SelectedValue)
If dal.DB.ExecuteProcedure(dt, "GENDER_B") Then
repGender.DataSource = dt
repGender.DataBind()
End If
End Sub
At Stored Procedure GENDER_B
ALTER PROCEDURE [dbo].[GENDER_B]
#GENDER as INT
AS
BEGIN
declare #sql as varchar(max)
set #sql='SELECT b.GENDER, COUNT(b.GENDER) AS TOTAL FROM USERS a
JOIN GENDER b ON b.GENDER_ID = a.GENDER
WHERE IS_DELETED = 0
GROUP BY b.GENDER'
if #GENDER<>''
begin
set #sql = #sql + 'and a.GENDER=''' + #GENDER + ''' '
end
--select #sql
exec(#sql)
END

Related

total in repeater footer, itemdatabound in VB.NET

I want to sum the total of the gender at the repeater footer using vb.net by using item data bound, the behind code is wrong cause I don't know how to do it...
FRONT CODE
<asp:Repeater ID="repGender" runat="server">
<HeaderTemplate>
<table cellspacing="0" rules="all" border="1">
<tr>
<th>Gender</th>
<th>Total</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# Eval("GENDER")%>
</td>
<td style="text-align: center">
<%# Eval("TOTAL")%></a>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr style="font-weight: bold">
<td>Grand Total</td>
<td style="text-align: center">
<asp:Label runat="server" ID="lblTotal"></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
BEHIND CODE
Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Total += Convert.ToInt32(DataBinder.Eval(e.Item.DataItem, ""))
Else e.Item.ItemType == ListItemType.Footer
End If
End Sub
Ok, so we need to define a simple total var at the form's "class" level. it only needs to persist during the data bind operation.
So, given your above markup, then we have this code to load the repeater.
Dim MyTotal As Integer
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
LoadData
End If
End Sub
Sub LoadData()
Dim strSQL As String =
"SELECT Gender, count(*) as TOTAL
FROM Employee
GROUP BY Gender"
Dim rstData As New DataTable
Using mycon As New SqlConnection(GetConstr)
Using cmdSQL As New SqlCommand(strSQL, mycon)
mycon.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
MyTotal = 0
repGender.DataSource = rstData
repGender.DataBind()
End Sub
And with your markup we see/get this:
And our data row bind event looks like this:
Protected Sub repGender_ItemDataBound(sender As Object, e As RepeaterItemEventArgs) Handles repGender.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or
e.Item.ItemType = ListItemType.AlternatingItem Then
' We don't have lable or text box with an "id" in repeat
' section, (can't use find control unless you assign "id" to these controls).
' so, lets grab the whole data row used for binding
Dim gData As DataRowView = e.Item.DataItem
MyTotal = MyTotal + gData.Item("TOTAL")
End If
If e.Item.ItemType = ListItemType.Footer Then
' set the total value in footer
Dim lblTotal As Label = e.Item.FindControl("lblTotal")
lblTotal.Text = MyTotal
End If
End Sub

how to add onclick event to a programmatically created link button in vb.net

I want to display the products which i get them from my DB using dataset in a table dynamically because of the number of products in each store is vary , and when the user click on a product from the table i will display the clicked product details on the screen .
the problem is that i have added a link button programmatically in each cell of the table but the click event handler not working .
Please how can i add a link button , and click event handler dynamically? and how can i get the id of the clicked button inside the event handler method(ShowProductDetails)?
this is my code
products.aspx
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<table style=" width: 700px;" align="center">
<tr style="width:100%;">
<td align="center" width="50%" >Stores </td>
<td align="right" width="50%" >
<asp:DropDownList ID="StoresDDL" runat="server" AutoPostBack="True" Width="120px">
</asp:DropDownList>
</td>
</tr>
<tr>
<td >
<asp:table id="ProductsTBL" runat ="server" style="width: 700px;"
align="center" BorderStyle="Groove" BorderWidth="3px">
</asp:table>
</td>
</tr>
<tr>
<td>
<asp:Panel runat ="server" id="ProductDetails_Panel" visible ="false">
<asp:Label ID="selectedProduct" runat ="server" Visible="false" ></asp:Label>
</asp:Panel>
</td>
</tr>
</table>
</asp:Content>
Products.aspx.vb
Imports System.Data
Partial Class Admin_ViewProducts
Inherits System.Web.UI.Page
Dim con As New Conection
Dim ds_Products As New DataSet
Dim dr_Products As DataRow
Dim ds_Stores As New DataSet
Dim dr_Stores As DataRow
Dim sB As New LinkButton()
Protected Sub StoresDDL__Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles StoresDDL.Init
StoresDDL.Items.Clear()
StoresDDL.Items.Add("Select a store")
StoresDDL.DataValueField = 0
Try
ds_Stores = con.get_data("select storeid , storeName from Store ")
End If
Dim x As Integer = 1
For Each Me.dr_stores In ds_stores.Tables(0).Rows
If (dr_stores.IsNull(0) = False) Then
StoresDDL.Items.Add(dr_stores.Item(1))
StoresDDL.Items(x).Value = dr_stores.Item(0)
x = x + 1
End If
Next
Catch ex As Exception
End Try
End Sub
Protected Sub StoresDDL_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles StoresDDL.SelectedIndexChanged
ds_products = con.get_data("select productid ,productName from Product where storeid=" & StoresDDL.SelectedValue ")
Dim y As Integer = 1
Dim NOClmns As Integer = 3
Dim NOCells As Integer = ds_products.Tables(0).Rows.Count
Dim NORows As Integer = Math.Round(NOCells / 3, MidpointRounding.AwayFromZero)
' Current row count
Dim rowCtr As Integer
' Current cell counter.
Dim cellCnt As Integer
Dim productCount As Integer = 0
While productCount <> ds_products.Tables(0).Rows.Count
For rowCtr = 1 To NORows
Dim tRow As New TableRow()
For cellCount = 1 To 4 'For NOCells = 1 To cellCnt
Dim tCell As New TableCell()
'Dim s As New HyperLink()
sB = New LinkButton()
sB.ID = ds_products.Tables(0).Rows(productCount).Item("productName").ToString
sB.Text = ds_products.Tables(0).Rows(productCount).Item("productName").ToString
AddHandler sB.Click, AddressOf Me.ShowProductDetails
tCell.Controls.Add(sB)
tRow.Cells.Add(tCell)
productCount += 1
Next cellCount 'NOCells
' Add new row to table.
ProductsTBL.Rows.Add(tRow)
Next rowCtr
End While
End Sub
Protected Sub ShowProductDetails(ByVal sender As System.Object, ByVal e As System.EventArgs)
ProductDetails_Panel.Visible = True
selectedProduct.Visible = True
selectedProduct.Text = "test"
End Sub
End Class
In order to make this work, you will want to use the Command Event for the link button and pass a CommandArgument, the product ID.
So:
sb.CommandArgument = ds_products.Tables(0).Rows(productCount).Item("productID")
sb.CommandName = "clicked"
sb.OnCommand="LinkButton_Command"
Then, add an event to the code behind:
Sub LinkButton_Command(sender As Object, e As CommandEventArgs)
If e.CommandName = "clicked" Then
Process Request
End IF
End Sub
But, if it were me, I would bind to a DataGrid or Repeater, rather than create my own table...

Dynamically generated Linkbutton does not fire Onclick event VB.Net

I have followed some of the instructions to create a new LinkButton at runtime with an event trigger and add an event handler for the LinkButton generated. I have no idea why the new LinkButton does not trigger the event added to it. Anyone please guide/correct me.
This is what I have right now.
ASPX:
<form id="myForm" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<table class="mel-table" style="text-align: center" runat="server" id="mytab1">
<tbody>
<tr>
<td>Case Number :</td>
<td>
<asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
</td>
</tr>
</tbody>
</table>
<asp:Button OnClick="btnOK1_Click" ID="btnOK1" runat="server" Text="Save" />
</ContentTemplate>
</asp:UpdatePanel>
</form>
ASPX.VB:
Protected Overloads Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session("tablecontent") IsNot Nothing Then
mytab1 = CType(Session("tablecontent"), HtmlTable)
End If
End Sub
Public Function checking() As Boolean
Dim caseNo As Double
Try
caseNo = Convert.ToDouble((caseNumber.Text).Trim())
Catch ex As Exception
caseNo = 0
End Try
Dim r As New HtmlTableRow
Dim c0 As New HtmlTableCell
r.Cells.Add(c0)
Dim but As New LinkButton
but.ID = caseNo
but.Text = caseNo.ToString()
AddHandler but.Click, AddressOf LinkButton1_Click
c0.Controls.Add(but)
mytab1.Rows.Add(r)
Session("tablecontent") = mytab1
Return True
End Function
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
'My Code here
End Sub
Protected Sub btnOK1_Click(sender As Object, e As EventArgs)
If (checking()) Then
ScriptManager.RegisterStartupScript(Page, Page.GetType, "Alert", "<script type='text/javascript'>alert('Success');</script>", False)
End If
End Sub
When you click on the "Save" button, save the data in either local storage or database and try to load the page again to fill the page with the content from the storage. In this case, you can have the content on the page and so, you can utilize the events also.
Whenever you create dynamic control you have to reload it on postback. I am using method call ReloadRows() for reloading added case which i am saving in ViewState:
<asp:UpdatePanel ID="UpdatePanel1" runat="server" ChildrenAsTriggers="true" UpdateMode="Conditional" >
<ContentTemplate>
<asp:Table class="mel-table" style="text-align: center" runat="server" id="mytab1">
<asp:TableRow>
<asp:TableCell>Case Number :</asp:TableCell>
<asp:TableCell>
<asp:TextBox ID="caseNumber" runat="server"></asp:TextBox>
</asp:TableCell>
</asp:TableRow>
</asp:Table>
<asp:Button OnClick="btnOK1_Click" ID="btnOK1" runat="server" Text="Save" />
</ContentTemplate>
</asp:UpdatePanel>
--------------------------------------------------------------------------------------------------------
Public Partial Class _Default
Inherits System.Web.UI.Page
Private listCaseNo As List(Of Double) = Nothing
Protected Sub Page_Init(sender As Object, e As EventArgs)
End Sub
Protected Sub Page_Load(sender As Object, e As EventArgs)
If ViewState("ListCases") IsNot Nothing Then
listCaseNo = DirectCast(ViewState("ListCases"), List(Of Double))
Else
listCaseNo = New List(Of Double)()
End If
ReloadRows()
End Sub
Public Function checking() As Boolean
Dim caseNo As Double = 0
Try
caseNo = Convert.ToDouble((caseNumber.Text).Trim())
Catch ex As Exception
caseNo = 0
End Try
Dim r As New TableRow()
Dim c0 As New TableCell()
r.Cells.Add(c0)
Dim but As New LinkButton()
but.ID = Convert.ToString(caseNo)
but.Text = caseNo.ToString()
AddHandler but.Click, AddressOf LinkButton1_Click
c0.Controls.Add(but)
mytab1.Rows.Add(r)
listCaseNo.Add(caseNo)
ViewState("ListCases") = listCaseNo
Return True
End Function
Private Sub ReloadRows()
For Each objCase As var In listCaseNo
Dim r As New TableRow()
Dim c0 As New TableCell()
r.Cells.Add(c0)
Dim but As New LinkButton()
but.ID = Convert.ToString(objCase)
but.Text = objCase.ToString()
AddHandler but.Click, AddressOf LinkButton1_Click
c0.Controls.Add(but)
mytab1.Rows.Add(r)
Next
End Sub
Protected Sub LinkButton1_Click(sender As Object, e As EventArgs)
'My Code here
End Sub
Protected Sub btnOK1_Click(sender As Object, e As EventArgs)
If (checking()) Then
End If
End Sub
End Class

how to make a label in a nested repeater visible programmatically

I have two repeaters, one nested in the other. I'm outputting a list of course titles in the parent repeater and the dates of those courses in the child repeater. This part is working fine. But, not all the course titles necessarily have dates available at any given time, so I want to be able to put a message up under each Course title that currently has no dates saying to check back regularly, blah, blah, blah. I've put the label in the footer template of the nested repeater with visibility=false and am trying to set the visibility=true at the appropriate time in the ItemDataBound Sub. Unfortunately, this is not working. I'm not getting any errors, the label just isn't showing up. I'm really hoping someone can help me out with my code or suggest an alternate way to do this. I'm kinda new to asp.net(VB) and am still struggling a bit. I've never tried nesting repeaters before. Here is my code:
.aspx
<asp:Repeater ID="rptTech" runat="server" >
<HeaderTemplate>
<table class="tableCourses">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<strong><asp:Label runat="server" ID="lblCourseName" Text='<%# Eval("CourseName") %>'></asp:Label></strong>
<br />
<asp:Label runat="server" ID="lblCourseSummary" Text='<%# Eval("Summary") %>'></asp:Label>
<br />
<asp:HyperLink ID="hpCourseMaterial" Visible='<%# CheckCourseMaterial(Eval("CourseMaterial")) %>' NavigateUrl='<%# "/files/Portal_Course_Material/" & Eval("CourseMaterial")%>' Text="Download Course Material" runat="server"></asp:HyperLink>
</td>
</tr>
<tr>
<td>
<asp:Repeater ID="rptTechDates" DataSource='<%#Eval("relCourses") %>' runat="server" >
<HeaderTemplate>
<table>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<strong><asp:Label runat="server" ID="lblDate" Text='<%# CDate(Eval("dteDate")).ToString("dd/MM/yyyy") %>'></asp:Label></strong><br />
<asp:Label runat="server" ID="dteTime" Text='<%# "Time: " & CDate(Eval("dteTime2")).ToString("HH:mm") & " EST" %>'></asp:Label><br />
<asp:HyperLink ID="Register" NavigateUrl='<%# "/Test.aspx?intMeetingID2=" & Eval("pkWebinarID")%>' Text="Register" runat="server"></asp:HyperLink>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
<tr>
<td>
<asp:Label ID="lblNoCourses" Text="There are no course dates available at this time. Please check back regularly to see any updates." runat="server" Visible="False"></asp:Label>
</td>
</tr>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
And Codebehind:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim curDate As Date = CDate(Now)
'Create the connection
Dim connstring As String
connstring = ConfigurationManager.ConnectionStrings("LPISQLConn").ConnectionString
Using conn As New SqlConnection(connstring)
Dim cmd As New SqlCommand("SELECT foo1, foo2, foo3 FROM tblFoo", conn)
cmd.CommandType = CommandType.Text
Dim ad As New SqlDataAdapter(cmd)
Dim ds As New DataSet()
ad.Fill(ds)
ds.Relations.Add(New DataRelation("relCourses", ds.Tables(0).Columns("fkcrsID"), ds.Tables(1).Columns("webinarID"), False))
rptTech.DataSource = ds.Tables(0)
rptTech.DataBind()
End Using
End Sub
Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
rptTechDates.DataSource = drv.CreateChildView("relCourses")
If rptTechDates.Items.Count < 1 And rptTechDates IsNot Nothing Then
If e.Item.ItemType = ListItemType.Footer Then
Dim lblNoCourses As Label = TryCast(e.Item.FindControl("lblNoCourses"), Label)
If lblNoCourses IsNot Nothing Then
lblNoCourses.Visible = True
End If
End If
Else
rptTechDates.DataBind()
End If
End If
End Sub
I would do the following:
Change your ItemDataBoundEvent for the parent repeater to:
Protected Sub rptTech_ItemDataBound(ByVal sender As Object, ByVal e As RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Item OrElse e.Item.ItemType = ListItemType.AlternatingItem Then
Dim drv As DataRowView = TryCast(e.Item.DataItem, DataRowView)
Dim rptTechDates As Repeater = TryCast(e.Item.FindControl("rptTechDates"), Repeater)
rptTechDates.DataSource = drv.CreateChildView("relCourses")
rptTechDates.DataBind()
End If
End Sub
Add this to the markup of your child repeater:
OnItemDataBound="rptTechDates_ItemDataBound"
Now add this:
Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataTable).Rows.Count = 0 Then
DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
End If
End If
So, if the number of rows in the child repeater's datasource is 0, then show the label.
EDIT After comment from OP
Protected Sub rptTechDates_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
If e.Item.ItemType = ListItemType.Footer Then
If DirectCast(DirectCast(e.Item.NamingContainer, Repeater).DataSource, DataView).Count = 0 Then
DirectCast(e.Item.FindControl("lblNoCourses"), Label).Visible = True
End If
End If
You want to search rptTechDates repeater for the lblNoCourses control. The e.Item.FindControl("lblNoCourses") is searching the rptTech repeater which doesn't exist. See this question to search the control in the rptTechDates footer.
Also, you'll want to call Databind() on the repeater after setting it's DataSource
rptTechDates.DataSource = drv.CreateChildView("relCourses")
rptTechDates.Databind()

button event in nested repeater

I am using nested repeater and child repeater has a user control in it.
<asp:repeater>
<asp:repeater>
<uc:userControl />
</asp:repeater>
</asp:repeater>
UserControl saves the information in database and raise the itemsaved event passing success message as event args.
I do have public event in usercontrol that will be raised but there is no way I can bind that event in the main page. (it would be really good if I can do that).
But I found another way to handle it in parent repeater's itemcommand. First it will fire usercontrol's button event and then repeater's itemcommand. I can recognize that usercontrol's event in itemcommand but how do I pass whether the information saved successfully or not? Is there any way I can pass true/false(successful save or not) from usercontrol's button event to itemcommand event?
You can handle your UserControl's event in the page. You have to add the handler in ItemCreated-event of your inner repeater.
Here is a working example(VB.Net):
Main-Page codebehind:
Public Class RepeaterTest
Inherits System.Web.UI.Page
Sub Page_Load(ByVal Sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim comps As New List(Of Company)
comps.Add(New Company("Microsoft"))
comps.Add(New Company("Intel"))
comps.Add(New Company("Dell"))
Repeater1.DataSource = comps
Repeater1.DataBind()
End If
End Sub
Protected Sub Repeater1_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim company As Company = DirectCast(e.Item.DataItem, Company)
Dim repeater2 As Repeater = DirectCast(e.Item.FindControl("Repeater2"), Repeater)
Dim deps As New List(Of Department)
deps.Add(New Department("purchasing", company))
deps.Add(New Department("management", company))
deps.Add(New Department("marketing", company))
deps.Add(New Department("logistics ", company))
repeater2.DataSource = deps
repeater2.DataBind()
End Select
End Sub
Protected Sub Repeater2_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
Select e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim uc_department As UC_Department = DirectCast(e.Item.FindControl("UC_Department1"), UC_Department)
'*** Here is all the "magic" ***'
AddHandler uc_department.NameChanged, AddressOf DepartmentSaved
End Select
End Sub
Protected Sub Repeater2_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs)
Select Case e.Item.ItemType
Case ListItemType.Item, ListItemType.AlternatingItem
Dim department As Department = DirectCast(e.Item.DataItem, Department)
Dim uc_department As UC_Department = DirectCast(e.Item.FindControl("UC_Department1"), UC_Department)
uc_department.Department = department
End Select
End Sub
Private Sub DepartmentSaved(ByVal uc_Department As UC_Department)
' do something f.e. save to database '
End Sub
End Class
<Serializable()>
Public Class Company
Public Sub New(ByVal name As String)
Me.Name = name
End Sub
Public Property Name As String
Public Overrides Function ToString() As String
Return Me.Name
End Function
End Class
<Serializable()>
Public Class Department
Public Sub New(ByVal name As String, ByVal company As Company)
Me.Name = name
Me.Company = company
End Sub
Public Property Name As String
Public Property Company As Company
Public Overrides Function ToString() As String
Return Me.Name
End Function
End Class
aspx:
<asp:Repeater id="Repeater1" OnItemDataBound="Repeater1_ItemDataBound" runat="server">
<HeaderTemplate>
<table border="1">
<tr>
<td><b>Company</b></td>
<td><b>Departments</b></td>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<%# DataBinder.Eval(Container.DataItem, "Name")%>
</td>
<td>
<asp:Repeater ID="Repeater2" OnItemCreated="Repeater2_ItemCreated" OnItemDataBound="Repeater2_ItemDataBound" runat="server">
<HeaderTemplate>
<table border="1">
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<uc1:UC_Department ID="UC_Department1" runat="server" />
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
</td>
</tr>
</ItemTemplate>
<FooterTemplate>
</table>
</FooterTemplate>
</asp:Repeater>
The usercontrol's codebehind:
Public Class UC_Department
Inherits System.Web.UI.UserControl
Public Event NameChanged(ByVal ucDepartment As UC_Department)
Public Property Department As Department
Get
If ViewState("Company") Is Nothing Then Return Nothing
Return New Department(Me.TxtName.Text, DirectCast(ViewState("Company"), Company))
End Get
Set(ByVal value As Department)
Me.TxtName.Text = value.Name
ViewState("Company") = value.Company
End Set
End Property
Private Sub BtnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles BtnSave.Click
RaiseEvent NameChanged(Me)
End Sub
End Class
and its ascx:
<%# Control Language="vb" AutoEventWireup="false" CodeBehind="UC_Department.ascx.vb" Inherits="DemoProject.UC_Department" %>
<asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
<asp:Button ID="BtnSave" runat="server" Text="Save" />

Resources