简体   繁体   English

如何设定价值 <input type=“hidden”> 在asp .net page_load中不使用runat =“server”

[英]How to set value of <input type=“hidden”> in asp .net page_load without using runat=“server”

i need to do the following two things... 我需要做以下两件事......

  1. i want to set value of in asp .net page_load. 我想在asp .net page_load中设置值。 the problem is that i dont want to use runat="server". 问题是我不想使用runat =“server”。 i have tried this the following but it does not work: 我试过以下但是它不起作用:

HtmlInputHidden hiddenControl = (HtmlInputHidden) FindControl("a"); HtmlInputHidden hiddenControl =(HtmlInputHidden)FindControl(“a”);

is there a way to access in asp .net page_load without using runat="server"? 有没有办法在asp .net page_load中访问而不使用runat =“server”? ? ?

  1. i can do this if i use but in this case i cannot access it in master page's javascript function. 如果我使用,我可以这样做,但在这种情况下,我无法在母版页的javascript函数中访问它。 i have tried this but it does not work... 我试过这个,但它不起作用......
    • var hdnField = document.getElementById('<%= hdnIdentity.ClientId%>'); var hdnField = document.getElementById('<%= hdnIdentity.ClientId%>');
    • var hdnField = document.getElementById("hdnIdentity").getAttribute("value"); var hdnField = document.getElementById(“hdnIdentity”)。getAttribute(“value”);
    • var hdnField = document.getElementById("hdnIdentity").value var hdnField = document.getElementById(“hdnIdentity”)。value

what i need... i want to access content page's hidden field value in javascript in master page. 我需要什么...我想在母版页的javascript中访问内容页面的隐藏字段值。 is there a way ? 有办法吗? ? ? thnx in advance regards Haroon haroon426@yahoo.com thnx提前问候Haroon haroon426@yahoo.com

I sometimes do the following, especially when I want control over my ids (especially when using jquery). 我有时会做以下操作,特别是当我想控制我的id时(特别是在使用jquery时)。

<asp:literal id="literal1" runat="server"><input type="hidden" id="someid" value="{0}"/></asp:literal>

Then, in codebehind you can set the value with the following: 然后,在代码隐藏中,您可以使用以下内容设置值:

literal1.Text = string.Format(literal1.Text, "somevalue");

This doesn't really get around using runat="server", but you haven't specified why you don't want to do that. 这并没有真正使用runat =“server”,但你没有说明为什么你不想这样做。 Also, you'd have to get the value with a request.form 此外,您必须使用request.form获取值

Update 更新

In .net 4.0 you have much more control over your IDs. 在.net 4.0中,您可以更好地控制ID。 See this for more information: 有关更多信息,请参阅此

http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx http://weblogs.asp.net/asptest/archive/2009/01/06/asp-net-4-0-clientid-overview.aspx

IIRC,你需要在那里的某个地方查看HttpRequest.Forms

如果该值是POST表单的一部分,那么如果它是GET表单,则需要检查Request.Forms或Request.QueryString。

ad 1) in aspx file just write <input type="hidden" value="<%=GetHiddenValue%>" /> . ad 1)在aspx文件中只写<input type="hidden" value="<%=GetHiddenValue%>" /> And in your code behind define protected property 并在您的代码后面定义protected属性

public class MyPage : Page {
  protected GetHiddenValue { get { /*...*/ } }

You can use it in your master page javascript how ever the control name is not what you expect it to be you'd need to use ClientID to get that. 您可以在主页面中使用它javascript如何控制名称不是您所期望的,您需要使用ClientID来获取它。 If you do not apply runat=server you can only get a hold of the control as text by either traversing the .aspx file or as some one mentioned embedding it in a named tag and then doing string manipulation on the inner HTML. 如果你不应用runat = server,你只能通过遍历.aspx文件或者某人提到将它嵌入到一个命名标签然后对内部HTML进行字符串操作来获得控件作为文本。 That is for setting it. 这是为了设置它。 If you need to get the value use Request[tagName] or similar 如果需要获取值,请使用Request [tagName]或类似的

ad 2) You can use simple html code in your content page with specified id <input type="hidden" id="myHiddenField" /> . ad 2)您可以在内容页面中使用指定id <input type="hidden" id="myHiddenField" />简单html代码。 Then in master page javascript use document.getElementById('myHiddenField') . 然后在母版页面javascript中使用document.getElementById('myHiddenField')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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