所以我在访问ASP.NET用户控件的正确clientID属性时遇到问题。
我的主要问题是我正在尝试使用VB代码隐藏返回控件的完整ClientID字段。我的控件的预期值(以及浏览器中显示的内容)是:
但我只是在代码隐藏中获得“txtAnswer”。
我在这里发现了类似的东西:
ASP.Net: ClientID not correct in code-behind of a user control
这篇文章建议在onPreRender或onPageLoad等期间添加onClick事件,但我还有其他情况:
我的用户控件(文本框)旨在“预加载”数据库中的值(如果同一用户以前曾尝试访问该表单),或者isPostBack == true。
因为我正在从数据库中读取答案,并且因为有JS函数(使用 getElementByID() JS函数),控件需要访问用户数据... < / p>
目前,该控件具有成员函数 loadQuestions(关键参数),用于从数据库加载特定问题。
更多上下文:我正在更新一些具有硬编码(在客户端ASPX文件中)控制元素的旧代码,但现在控件正在运行时动态构建(即插入到客户端页面中的占位符中) )。
我已尝试使用不同的ClientIDMode设置(每http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx),但到目前为止没有骰子......我正在使用.NET 4.0,所以我知道我可以直接编辑ClientID “静态”clientIDmode,虽然我还没试过......
现在我想我应该将所有内容定义的东西“加载”到对象的构造函数中,然后应用所有内容定义的东西,然后在控件的后面建立onClick属性数据已填充?
供参考:
以下是Control的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 Code-behind(为简洁起见编辑)调用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';
}
非常感谢! -Lewis
答案 0 :(得分:1)
我尝试过使用不同的ClientIDMode设置(per http://msdn.microsoft.com/en-us/library/system.web.ui.clientidmode.aspx) 但到目前为止没有骰子......我正在使用.NET 4.0,所以我明白我可以 通过“静态”clientIDmode直接编辑ClientID,我没有 虽然试过了......
尝试一下,它将彻底解决您的问题。您在标记上分配的任何ID都将是您在服务器端检索的ID。 ID前面没有额外的垃圾。