繁体   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