简体   繁体   English

以编程方式在背后的代码中更改用户控件的自定义属性

[英]Programmatically change custom attributes of User Control in Code Behind

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. 我正在使用销售系统,当生成销售时,客户数据由Javascript自动完成填充,但是在加载保存的销售时,我需要以编程方式将用户ID粘贴到控件中。

<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 ) 在父文档的标记内的页面上呈现我的控件(在本例中为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. newSale.aspx.vb后面的代码中,我需要能够以编程方式更改Controls customerID newSale.aspx.vb属性的值。 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 这是我在进行各种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; 编辑; just so you know, if I manually set the customerID, I am able to access that attribute within the codebehind for the Control 就是这样,如果您手动设置了customerID,就可以在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: 客户表单控件是一个冗长的HTML文档,因此我不会在此处发布所有内容,但是标题是:

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

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. 也许从后面的控制代码中粘贴一些代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM