ASP.NET用户控件的绑定属性到父控件的一个字段

时间:2011-12-30 01:12:50

标签: asp.net vb.net controls

如果我需要访问BEFORE PreRender之前的用户控件属性的值,我是否需要将自定义控件基于其中一个预先存在的数据控件(转发器,列表视图等)?

我的一个用户控件具有gridview控件,该控件根据用户控件所在的属性进行配置。几个关键属性改变了底层记录源的SQL语句。我现在处于这样的情况:设置SQL语句的WHERE语句的属性需要绑定到用户控件的父FormView中的值。将formview视为显示客户详细记录。用户控件获取客户的帐号,然后显示相关表中的数据,例如客户联系人姓名。由于gridview是在控件的prerender事件之前创建的,因此在prerender事件中工作似乎效率不高。

请参阅此问题作为参考: Stumped With Custom Property on User Control

1 个答案:

答案 0 :(得分:0)

我说过,当用户控件绑定时,您可以为父控件赋值。

TestIt.ascx - 标记

<%@ Control Language="VB" AutoEventWireup="false" 
            CodeFile="TestIt.ascx.vb" Inherits="usercontrols_TestIt" %>
<asp:Label 
         ID="lblOne"
         runat="server">
</asp:Label>

TestIt.ascx.vb

Partial Class usercontrols_TestIt
    Inherits System.Web.UI.UserControl
    Public Property No As Integer
        Get
            Dim mno As Integer = 0
            Integer.TryParse(lblOne.Text, mno)
            Return mno
        End Get
        Set(value As Integer)
            lblOne.Text = value.ToString()
        End Set
    End Property

    Public ReadOnly Property Square As Integer
        Get
            Return No * No
        End Get
    End Property
    Protected Sub Page_PreRender(sender As Object, e As System.EventArgs) Handles Me.PreRender
        'Get ref. of parent control
        Dim row As FormViewRow = CType(Parent.Parent, FormViewRow)
        'Find control in parent control
        Dim sqLabel As Label = row.FindControl("Label2")
        'Assign value
        sqLabel.Text = Square.ToString()
    End Sub
End Class

ASPX - 标记

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="VbDefault2.aspx.vb" Inherits="usercontrols_VbDefault2" %>
<%@ Register src="TestIt.ascx" tagname="TestIt" tagprefix="uc1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:FormView ID="FormView1" runat="server" AllowPaging="True">
            <ItemTemplate>
                No :
                <uc1:TestIt ID="TestIt1" runat="server" No='<%#Eval("No") %>' 
                    ClientIDMode="AutoID" />
                <br />
                Square :
                <asp:Label ID="Label2" runat="server"    ></asp:Label>
            </ItemTemplate>
        </asp:FormView>
    </div>
    </form>
</body>
</html>

ASPX.vb

Partial Class usercontrols_VbDefault2
    Inherits System.Web.UI.Page
    Public Class TestData
        Public Property No As Integer
    End Class
    Protected Sub Page_Load(sender As Object, e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            BindData()
        End If
    End Sub
    Sub BindData()
        Dim nos As New List(Of TestData)
        nos.Add(New TestData() With {.No = 10})
        nos.Add(New TestData() With {.No = 20})

        FormView1.DataSource = nos
        FormView1.DataBind()
    End Sub
    Protected Sub FormView1_PageIndexChanging(sender As Object, e As System.Web.UI.WebControls.FormViewPageEventArgs) Handles FormView1.PageIndexChanging
        FormView1.PageIndex = e.NewPageIndex
        BindData()
    End Sub
End Class