简体   繁体   中英

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.

<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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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