因为我的客户端JS要调用某些不便加上runat属性的HTML元素,回发时在服务器端读取值。
1.我有JS是大段大段的,请别建议我用Page.RegisterStartupScript之类的方法注册;
2.我不想把所有的HTML元素都加上runat属性;
3.帮我看下如下的问题:客户端:
<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/>JS中:
var cpc = document.getElementById("hidden_Current_Product_Category")
if(cpc != null)
{
cpc.value = "123";
}
没有效果;.cs文件中:
无论是用Page.FindControl("hidden_Current_Product_Category"),还是用Request.Form["hidden_Current_Product_Category"],结果都是空的。以上的问题随便解决哪个都有分啊!
1.我有JS是大段大段的,请别建议我用Page.RegisterStartupScript之类的方法注册;
2.我不想把所有的HTML元素都加上runat属性;
3.帮我看下如下的问题:客户端:
<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/>JS中:
var cpc = document.getElementById("hidden_Current_Product_Category")
if(cpc != null)
{
cpc.value = "123";
}
没有效果;.cs文件中:
无论是用Page.FindControl("hidden_Current_Product_Category"),还是用Request.Form["hidden_Current_Product_Category"],结果都是空的。以上的问题随便解决哪个都有分啊!
应该是有值的,除非你这个input不是放在<form中,除非你不是post提交的页面.
<form id="form1" runat="server">
<div>
<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/><br />
<br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" /></div>
</form>
protected void Button1_Click(object sender, EventArgs e)
{
string str1=Request.Form["hidden_Current_Product_Category"];
Response.Write(str1+"<br>");
}
我想<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/>
应该有runat="server"这一项或许能解决?
但我的程序是在服务器端用add方法加上这个控件的,它是个服务器控件,但没有runat="server".
对于楼上的楼上几位的说法,我不确定是否用post提交,所以先试一下,谢谢!继续问客户端写好的JS如何和.CS文件相互传值的问题。
<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/> JS中:
var cpc = document.getElementById("hidden_Current_Product_Category")
if(cpc != null)
{
cpc.value = "123";
} 你的这段代码,好像没有什么问题,值已经赋值了,只是如果你从后台取,应该是一刷新,就丢了,这样少量的runat=server,不会影响到很大的速度,建议加上
但我是在服务器端用如下方法加的一个控件:
HiddenField hf=new HiddenField ();
this.controls.Add("hf");
这样加上去的,它怎么没有runat="Server"属情呢? 怎样才能让它有这一属性?
HiddenField hf=new HiddenField ();
this.controls.Add("hf");
只是它没有runat="server"属情, 不知怎么才能让它有这一属性.
引用MasterPage中的控件很容易, 就是用控件的ClientID而不是ID就可以了.
现在看来这个动态生成的控件是一定要有runat="server",在服务器端才能取其值的.
Post没有用,楼主不用费心了,我试过了.
(1)在服务端利用代码加入服务器控件
.
.
.
HiddenField hf = new HiddenField();
hf.ID = "hf_TestID";
hf.Value="初始值";
this.Controls.Add(hf); //注.这里this是本自定义控件本身. Page.Response.Cookies["hfClientID"].value=hf.ClientID; //本行的目的是将本控件的ClientID写入Cookie
.
.
.
(2)在Javascript块中,对这个控件动态赋值:
<script language="JavaScript">
function Set_NewValue(newValue)
{
var clientID= GetCookie("hfClientID"); //GetCookie是用JavaScript写的一个读cookie的函数,经验证毫无问题
var newTextHiddenObj = document.getElementById( clientID ); //经检验,这个ClientID确实是唯一的,没有任何其它重复的
if(newTextHiddenObj != null)
newTextHiddenObj.value = newValue; //新的动态值;
alert(newTextHiddenObj.value);
}
</script> (3)
上述内容看起来没有问题,而且alert语句也显示确实赋了新值. 但在服务器端,想将这个新值存入数据库时,且无法得到这个值,无论在服务器端用hf.Value
还是通过Page.Request["clientID"]的方法,均得到空值; (4)
查看使用此自定义控件的页面源码, 可以看到,页面适当位置添加了这样的记录:
<input type="hidden" ID="ctl00_ContentPlaceHolder1_MyControl_hf_TestID"
Name="ctl00$ContentPlaceHolder1$MyControl$hf_TestID" Value="初始值" />
应该说,这条记录是正确生成的.
但令人不解的是, 页面上通过按钮调用上述javascript方法给这个控件动态赋值后,再查看网页源码,此控件的Value值仍为"初始值",并未改变.
但JavaScript的alert语句alert(newTextHiddenObj.value);显示的却是新值;