因为我的客户端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.   

    Request.Form["hidden_Current_Product_Category"]
    应该是有值的,除非你这个input不是放在<form中,除非你不是post提交的页面.
      

  2.   

    没啥问题,是不是没放在<form>表单中?
        <form id="form1" runat="server">
        <div>
        <input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/><br />
            <br />
            &nbsp;<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>");
        }
      

  3.   

    咱俩问题类似.请问解决了没有?
    我想<input type="hidden" id="hidden_Current_Product_Category" name="hidden_Current_Product_Category" value="0"/> 
    应该有runat="server"这一项或许能解决?
    但我的程序是在服务器端用add方法加上这个控件的,它是个服务器控件,但没有runat="server".
      

  4.   

    回楼上,因为这个hidden要在JS中处理,而JS又是事先写好放在客户端的,所以如果加上runat="server",还要在JS中想办法处理控件的ID变化的问题(我用的模块页),所以没有用runat属性。
    对于楼上的楼上几位的说法,我不确定是否用post提交,所以先试一下,谢谢!继续问客户端写好的JS如何和.CS文件相互传值的问题。
      

  5.   

    如果我不是用的POST提交,如何在.cs文件中访问HTML控件?
      

  6.   

    客户端: 
    <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,不会影响到很大的速度,建议加上
      

  7.   

    回楼上,runat=server是好,但因为我的JS是写在单独文件里的,而我的ASPX页面用的是模板页,在JS中引用服务器控件还要考虑ID变化的问题,复杂度高多了,不是吗?
      

  8.   

    顺便问一下8楼的,我的情况和楼主的差不多.
    但我是在服务器端用如下方法加的一个控件:
    HiddenField hf=new HiddenField ();
    this.controls.Add("hf");
    这样加上去的,它怎么没有runat="Server"属情呢? 怎样才能让它有这一属性?
      

  9.   

    顺便靠诉一下楼主,用如下方法加上去的控件在客户端用js处理毫无问题.
    HiddenField hf=new HiddenField (); 
    this.controls.Add("hf"); 
    只是它没有runat="server"属情, 不知怎么才能让它有这一属性.
      

  10.   


    引用MasterPage中的控件很容易, 就是用控件的ClientID而不是ID就可以了.
    现在看来这个动态生成的控件是一定要有runat="server",在服务器端才能取其值的.
      

  11.   

    放在 <form>表单中 并POST 提交
      

  12.   


    Post没有用,楼主不用费心了,我试过了.
      

  13.   

    把我的问题也放这里请高手指教.和楼主的情况几乎一样.很遗憾没分的,刚恢复密码,一切从0开始.
     
    (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);显示的却是新值;