以编程方式更改Code Behind中用户控件的自定义属性

时间:2012-01-21 13:05:44

标签: asp.net vb.net user-controls code-behind

我有一个用户控件,它是一个客户输入表单。

我正在开发销售系统,在生成销售时,客户数据由Javascript自动完成填充,但在加载已保存的销售时,我需要以编程方式将用户ID粘贴到控件中。

<controls:customerDataForm ID='customerForm1' partExchangeMenu="true" showBankDetails="false" customerID="****" runat='server' />

在父文档标记内的页面上呈现我的控件(在这种情况下称为newSale.aspx

newSale.aspx.vb后面的代码中,我需要能够以编程方式更改Controls customerID属性的值。我似乎无法做到。

这是我所做的,在各种Google搜索尝试之后,但它始终将customerID保留为零

Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Init
    customerForm1.customerID = "0" '// default Customer ID if no invoice number 
    If Request.QueryString("invno") <> "" Then
        Dim customerID As Integer
        '// open database and get customer ID from the invoice.
        customerForm1.customerID = customerID '// set customerID value
    End If
End Sub

EDIT;只是你知道,如果我手动设置customerID,我可以在Controlbehind的代码隐藏中访问该属性

======

编辑,我现在通过在父页面后面的代码中将控件添加到页面来实现它,但这不是我想要的。这就是我现在正在做的事情。唯一的区别是控件的占位符并将其添加到代码中。

父页面如下所示:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" CodeFile="newSale.aspx.vb" Inherits="newSale_Default"%>
<%@ Register TagPrefix="controls" TagName="customerDataForm" Src="~/Controls/customerForm.ascx" %>
<%@ Register TagPrefix="controls" TagName="itemDataList" Src="~/Controls/itemList.ascx" %>

<asp:content id="content1" ContentPlaceHolderID="mainContent" runat="server">

<div class="content">  
    <form id="newSale" method="post" action="saveSale.aspx">
    <h1>--- New Sale ---</h1>  
    <div class="section blackBoxShadow borderRad5" id="customerSection">        
        <asp:placeholder ID="customerPlaceHolder" runat="server" />
    </div>
</div>
</form>    
</div>
</asp:content>

代码背后:

Partial Class newSale_Default
Inherits System.Web.UI.Page


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim savedCustomerID As Integer
    Dim customerCtrl As Controls_customerData

    customerCtrl = CType(LoadControl("~\Controls\customerForm.ascx"), Controls_customerData)
    customerCtrl.ShowBankDetails = "false"

    If Request.QueryString("invno") <> "" Then
        Using connection As OdbcConnection = Common.getConnection("new")
            connection.Open()
            Dim openDB As OdbcCommand = Common.createCommand("SELECT customerId, id FROM invoices WHERE id='" & Request.QueryString("invNo") & "' LIMIT 1", connection, "new")
            Dim objDataReader As OdbcDataReader = openDB.ExecuteReader(CommandBehavior.CloseConnection)
            While (objDataReader.Read())
                savedCustomerID = objDataReader("customerId")
            End While
        End Using
        customerCtrl.customerID = savedCustomerID
    Else
        customerCtrl.customerID = 0
    End If

    customerPlaceHolder.Controls.Add(customerCtrl)

End Sub

End Class

客户表单控件是一个简单的HTML文档,所以我不会在这里发布所有内容,但标题是:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="customerForm.ascx.vb" Inherits="Controls_customerData" %>

代码背后:

Partial Public Class Controls_customerData
Inherits System.Web.UI.UserControl

Private _customerID As Integer
Public Property customerID() As Integer
    Get
        Return _customerID
    End Get
    Set(ByVal value As Integer)
        _customerID = value
    End Set
End Property
Private _ShowBankDetails As Boolean
Public Property ShowBankDetails() As Boolean
    Get
        Return _ShowBankDetails
    End Get
    Set(ByVal value As Boolean)
        _ShowBankDetails = value
    End Set
End Property

Public Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
    Me.bankDetails.Visible = Me.ShowBankDetails
End Sub

Public Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init

    ' Add required Javascript or CSS for the web Control       
    Dim customerJS As New HtmlGenericControl("script")
    customerJS.Attributes.Add("type", "text/javascript")
    customerJS.Attributes.Add("src", "/Scripts/customerControl.js")
    Page.Header.Controls.Add(customerJS)

    If customerID <> 0 Then
        ScriptManager.RegisterStartupScript(customerControlIDController, GetType(HtmlGenericControl), "findUSer", "$(document).ready(function () { getCustomerByNumeric('id', '" & customerID & "');  });", True)
    End If
End Sub

End Class

1 个答案:

答案 0 :(得分:0)

我的猜测是您遇到了页面生命周期问题。在控制代码后面的什么时候你试图访问该属性?如果您进行调试,您可能会发现该值尚未准备就绪。

也许从控制代码后面粘贴一些代码。