如何在Javascript中从UserControl访问asp hiddenfield值(getElementByID不工作)
因此,我一直在尝试从JavaScript访问UserControl中的HiddenField值 在我的用户控件中,我有:如何在Javascript中从UserControl访问asp hiddenfield值(getElementByID不工作),javascript,asp.net,user-controls,hiddenfield,Javascript,Asp.net,User Controls,Hiddenfield,因此,我一直在尝试从JavaScript访问UserControl中的HiddenField值 在我的用户控件中,我有: <asp:HiddenField ID="HiddenField1" runat="server" /> 但这显然给可重用性带来了问题 编辑: 由此生成的html为: <input type="hidden" name="ctl00$ctl00$MainContent$MainContent$$ctl00$SomeUserControl$someOth
<asp:HiddenField ID="HiddenField1" runat="server" />
但这显然给可重用性带来了问题
编辑:
由此生成的html为:
<input type="hidden" name="ctl00$ctl00$MainContent$MainContent$$ctl00$SomeUserControl$someOtherUserControl1$HiddenField1" id="MainContent_MainContent_SomeRepeater_SomeUserControl_0_someOtherUserControl1_0_HiddenField1_0" value="353">
该值通过其他函数在代码隐藏中设置
EDIT2:概括了我的代码示例我找不到一种方法来使用隐藏字段,所以我放弃了,只是将我的对象更改为带有CSSClass的asp:Label,并将值存储在text属性中
这不是我所知道的最好的解决方案,但如果有人有更好的建议,请告诉我。在代码隐藏中,您的代码可能是这样的
public void test()
{
hdnvalue.Value = "Test";
//After assign the value you should call client side function
ClientScript.RegisterStartupScript(GetType(), "Script", "<script
language='javascript'>gethidden()</script>", false);
}
<script language="javascript" type="text/javascript">
function gethidden() {
var hdn = document.getElementById('<%=hdnvalue.ClientID%>');
alert(hdn.value);
}
</script>
公共无效测试()
{
hdnvalue.Value=“测试”;
//赋值后,您应该调用客户端函数
RegisterStartupScript(GetType(),“Script”,“gethidden()”,false);
}
在javascript函数中应该这样使用
public void test()
{
hdnvalue.Value = "Test";
//After assign the value you should call client side function
ClientScript.RegisterStartupScript(GetType(), "Script", "<script
language='javascript'>gethidden()</script>", false);
}
<script language="javascript" type="text/javascript">
function gethidden() {
var hdn = document.getElementById('<%=hdnvalue.ClientID%>');
alert(hdn.value);
}
</script>
函数gethidden(){
var hdn=document.getElementById(“”);
警报(hdn.值);
}
我希望它能对您有所帮助。右键单击并查看网页的
源代码。检查是否能够看到hiddenfield输入项。然后现在从那里复制id
,并尝试获取它的getElementById代码>?你能粘贴你的控件生成的html吗?在问题中,我们有很多可以使用的jquery选择器。我已经编辑了原始帖子Gagan@Matt:关于document.getElementById(“”)如何代码>没有给出预期输出?我很感激这个建议,但是我需要在客户端访问它。这样做的目的是执行AJAX调用并提高页面性能(此项目中的视图状态非常大,因此需要尽量减少回发)。您好,您已经提到“值是通过其他函数在代码中设置的。”您正在服务器端指代中赋值,上面的代码对您很有用。Hello Angapan.S,上面的代码不适用于我的目的,因为该值仅在“代码隐藏”中的“页面加载”处设置,但需要在用户与页面上的其他对象交互后在客户端访问。这一切都是为了避免回发并提高页面性能。回帖是不可行的。没有其他的建议,很遗憾,这必须是答案。