以编程方式更改代码隐藏中用户控件的自定义属性
我有一个用户控件,它是一个客户输入表单。
我正在开发一个销售系统,在生成销售时,客户数据由 Javascript 自动完成填充,但在加载保存的销售时,我需要以编程方式将用户 ID 粘贴到控件中。
<controls:customerDataForm ID='customerForm1' partExchangeMenu="true" showBankDetails="false" customerID="****" runat='server' />
在父文档标记内的页面上呈现我的控件(在本例中称为 newSale.aspx
)
在 newSale.aspx.vb
后面的代码中,我需要能够以编程方式更改 Controls customerID
属性的值。我好像做不到
这就是我在各种谷歌搜索尝试之后所做的,但它总是将 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
编辑;只是想让你知道,如果我手动设置 customerID,我就可以在控件的代码隐藏中访问该属性
=========
编辑,我现在通过将控件添加到页面中来使其工作父页面后面的代码,但这并不是我真正想要的。这就是我现在正在做的事情。唯一的区别是控件的占位符并将其添加到代码中。
父页面如下所示:
<%@ 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
I have a User Control which is a customer input form.
I'm working on a sales system, when generating a sale, the customer data is populated by a Javascript Auto Complete, but when loading a saved sale, I need to paste a User ID into the control programatically.
<controls:customerDataForm ID='customerForm1' partExchangeMenu="true" showBankDetails="false" customerID="****" runat='server' />
Renders my control on the page within the markup of the parent document (in this case it's called newSale.aspx
)
In the code behind in newSale.aspx.vb
I need to be able to programmtically change the value of the Controls customerID
attribute. I can't seem to do it.
This is what I have done, following various Googling attempts, but it is always leaving the customerID as zero
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; just so you know, if I manually set the customerID, I am able to access that attribute within the codebehind for the Control
=========
EDIT, I have got it working now by adding the Control to the page in the code behind of the parent page, but that's not really what I wanted. Here's what I am doing now. The ONLY difference being the placeholder for the control and adding it in the code.
The parent page looks like this:
<%@ 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>
Code behind:
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
Customer Form Control is a lond HTML document so I wo't post all that here, but the header is:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="customerForm.ascx.vb" Inherits="Controls_customerData" %>
Code behind:
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的猜测是您遇到了页面生命周期问题。您在控制代码隐藏的哪个点尝试访问该属性?如果您进行调试,您可能会发现该值尚未准备好。
也许从控制代码隐藏中粘贴一些代码。
My guess is that you're having a page lifecycle issue. At what point in the control codebehind are you trying to access the property? If you debug, you'll probably see that the value isn't quite ready.
Perhaps paste some code from the control codebehind.