簡體   English   中英

以編程方式在背后的代碼中更改用戶控件的自定義屬性

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM