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

发布于 2024-12-23 16:05:37 字数 444 浏览 0 评论 0原文

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

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

看这个问题作为参考: 用户控件上的自定义属性难住了

If I need access to the value of a user control's property BEFORE PreRender, would I need to base the custom control off of one of the preexisting data controls (repeater, listview, etc.)?

One of my user controls features a gridview control that is configured based on the user control's properties on which it resides. Several of the key properties alter the SQL Statement for the underlying recordsource. I'm now in a situation where the property that sets the WHERE statement for the SQL Statement needs to tied to a value in the user control's parent FormView. Think of the formview as displaying a customer detail record. The user control takes the customer's account number and then displays data from a related table such as Customer Contact Names. Since the gridview is created before the control's prerender event, working within the prerender event doesn't seem efficient.

See this question as a reference:
Stumped With Custom Property on User Control

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

牵你手 2024-12-30 16:05:37

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

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

What I said that you can assign value to the parent control when user control binds.

TestIt.ascx - Markup

<%@ 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 - Markup

<%@ 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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文