[英]Programmatically change custom attributes of User Control in Code Behind
我有一个用户输入表单的用户控件。
我正在使用销售系统,当生成销售时,客户数据由Javascript自动完成填充,但是在加载保存的销售时,我需要以编程方式将用户ID粘贴到控件中。
<controls:customerDataForm ID='customerForm1' partExchangeMenu="true" showBankDetails="false" customerID="****" runat='server' />
在父文档的标记内的页面上呈现我的控件(在本例中为newSale.aspx
)
在newSale.aspx.vb
后面的代码中,我需要能够以编程方式更改Controls customerID
newSale.aspx.vb
属性的值。 我似乎做不到。
这是我在进行各种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
编辑; 就是这样,如果您手动设置了customerID,就可以在Control背后的代码中访问该属性
=========
编辑,通过在父页面后面的代码中将控件添加到页面中,我现在可以正常工作了,但这并不是我真正想要的。 这就是我现在正在做的。 唯一的区别是控件的占位符,并将其添加到代码中。
父页面如下所示:
<%@ 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
我的猜测是您遇到页面生命周期问题。 您要在后面的控制代码中的什么时候尝试访问该属性? 如果进行调试,则可能会看到该值尚未准备好。
也许从后面的控制代码中粘贴一些代码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.