簡體   English   中英

在后面的代碼(asp.net VB)中返回“完整” clientID

[英]Returning “full” clientID in code behind (asp.net VB)

因此,我在訪問ASP.NET用戶控件的正確clientID屬性時遇到問題。

我的主要問題是,我試圖使用VB后台代碼返回控件的完整ClientID字段。 我的控件(以及瀏覽器中顯示的內容)的期望值為:

  • ctl00_ContentPlaceHolder1_ctl05_txt答案

但是我只在后面的代碼中得到了“ txtAnswer”。

我在這里找到類似的東西:

ASP.Net:ClientID在用戶控件的代碼背后不正確

這篇文章建議在onPreRender或onPageLoad等過程中添加onClick事件,但是我還有其他情況:

  • 我的用戶控件(文本框)設計用於從數據庫中“預加載”值(如果同一用戶先前曾嘗試訪問該表單),或者isPostBack == true。

  • 因為我正在從數據庫中讀取答案,並且因為存在JS函數(它使用getElementByID() JS函數),所以控件需要訪問用戶數據...

  • 當前,控件具有成員函數loadQuestions(key parameters) ,該成員函數從數據庫中加載特定問題。

  • 更多上下文:我正在更新一些已經硬編碼(在客戶端ASPX文件中)控件元素的舊代碼,但是現在控件是在運行時動態構建的(即,插入到客戶端頁面的占位符中)。

  • 我試過弄亂了不同的ClientIDMode設置(每個http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx ),但到目前為止沒有骰子...我正在使用.NET 4.0,所以我知道我可以通過“靜態” clientIDmode直接編輯ClientID,但是我還沒有嘗試過...

現在,我想我應該將所有定義內容的東西“預先加載”到該對象的構造函數中,然后再應用所有定義內容的東西,然后在控件的數據輸入后建立onClick屬性。人口稠密?

以供參考:

這是控件的ASP標記:

<%@ Control Language="VB" AutoEventWireup="false" CodeFile="RBHorizWithText.ascx.vb" Inherits="Apps_Controls_RBHorizWithText" %>
<%@ Register Assembly="txtBoxLengthValidator" Namespace="txtBoxLengthValidator" TagPrefix="cc1" %>

<asp:Panel ID="pnlPreamble" runat="server">
    <div id="divPreamble" runat="server" style="padding-bottom:15px"></div>
</asp:Panel>

<asp:Panel ID="pnlQuestion" runat="server">
    <div id="divQuestion1" runat="server"></div>



    <div id="divRBL" runat="server"  style="padding-left: 20px"> 
      <asp:Table ID="tblAnswers" runat="server" CellPadding="0" CellSpacing="0">
      </asp:Table>
    </div>

    <div id="divQuestion2" runat="server" style="padding-left: 20px; padding-top: 10px"></div>
    <div id="divAnswer2" runat="server" style="padding-left:20px; padding-top: 5px; text-align: left" align="left">
        <div>
            <cc1:TextBoxLengthValidator 
                ID="tblvAnswer" 
                    ControlToValidate="txtAnswer" 
                    runat="server" 
                    ErrorMessage="Maximum length is 255 Characters" 
                    MaximumLength="255" 
                    Display="Dynamic"
                    ForeColor="Red">
            </cc1:TextBoxLengthValidator>    
        </div>

        <div><asp:TextBox ClientIDMode = "Predictable" ID = "txtAnswer" runat="server" Width="600px" Rows="3" TextMode="MultiLine"></asp:TextBox></div>

    </div>

</asp:Panel>

這是VB后台代碼中的函數(為簡潔起見進行了編輯),該函數調用JS函數...

Public Sub LoadQuestions(ByVal objQRBL As Question, ByVal objQText As Question, ByVal dicAnswers As Dictionary(Of Integer, QuestionAnswer), ByVal LoadAnswers As Boolean)
        'This is a function from RBHorizWithText user control that has two member controls: a radio button and a text box (first 2 params).
        'dicAnswers are the user's answers stored either in HttpContext.Current, or as global memory objects...


        _lstRadioButtons = New List(Of RadioButton)
        Me.tblAnswers.Rows.Clear()
        'txtAnswer is a member control of type textBox...
        Me.txtAnswer.Text = String.Empty

        '.
        'Stuff to build out <tr> and <td> question display elements to format/store control...
        '.


        Dim trw As New TableRow
        Dim iCount As Integer = 0

        For Each objA As QuestionAnswer In objQRBL.AnswerList           

            Dim objRB As New RadioButton

            objRB.ID = objA.QuestID & ";" & objA.AnswerID
            objRB.GroupName = "rb" & objA.QuestID
            objRB.Text = objA.AnswerText

            'This is the main area where I'm having trouble:  
            '   At runtime, Me.txtAnswer.ClientID should evaluate to: "ctl00_ContentPlaceHolder1_ctl05_txtAnswer"
            '   Instead I'm getting:    "txtAnswer"

            If objQText.ParentReqResponseCode.IndexOf(";" & objA.ResponseCode & ";") <> -1 Then
                objRB.Attributes.Add("onclick", "txtBoxEnable('" & Me.txtAnswer.ClientID & "');")
            Else
                objRB.Attributes.Add("onclick", "txtBoxDisable('" & Me.txtAnswer.ClientID & "','" & objQText.InnerText & "');")
            End If


            tcl.Controls.Add(objRB)
            trw.Cells.Add(tcl)

            _lstRadioButtons.Add(objRB)

            iCount += 1
        Next

        'Other stuff to handle formatting and behavior of other controls...

    End Sub

這是JS函數,它們存在於項目的主頁上:

function txtBoxEnable(elID) {                     
    var el = document.getElementById(elID);
    el.style.backgroundColor = '#f4df8d';
    el.value = '';
    el.disabled = '';           
}

function txtBoxDisable(elID, innerText) {
    var el = document.getElementById(elID);
    el.value = innerText;
    el.style.backgroundColor = '#ffffff';
    el.disabled = 'disabled';
}

非常感謝! -劉易斯

我試過弄亂了不同的ClientIDMode設置(每個http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx ),但到目前為止沒有骰子...我正在使用.NET 4.0,所以我知道我可以通過“靜態” clientIDmode直接編輯ClientID,但是我還沒有嘗試過...

試試看,它將完全解決您的問題。 無論您在標記上分配的ID是在服務器端檢索到的ID。 ID之前沒有多余的垃圾。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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