我正在使用嵌套转发器,子转发器中有一个用户控件。
<asp:repeater>
<asp:repeater>
<uc:userControl />
</asp:repeater>
</asp:repeater>
UserControl将信息保存在数据库中,并将itemsaved事件传递成功消息提升为事件参数。
我在usercontrol中有公共事件会被引发但是我无法在主页面中绑定该事件。 (如果我能做到这一点真的很棒)。
但我找到了另一种方法来处理父转发器的itemcommand。首先,它将触发usercontrol的按钮事件,然后触发repeater的itemcommand。我可以在itemcommand中识别usercontrol的事件,但是如何传递信息是否成功保存?有没有办法可以将user / control按钮事件中的true / false(成功保存或不成功)传递给itemcommand事件?
答案 0 :(得分:3)
您可以在页面中处理UserControl的事件。您必须在ItemCreated中添加处理程序 - 内部转发器的事件。
这是一个工作示例(VB.Net):
主页代码隐藏:
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>
usercontrol的代码隐藏:
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
及其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" />