| Javascript调用C#代码的方法网上介绍了很多种方法,也很详细,但没有向C#传递参数的方法。今天刚好用到,搞了半 天才搞出来(其实我很笨)。下面说一下具体实现的方法。一、使用HiddenField 控件。  HiddenField控件顾名思义就是隐藏输入框的服务器控件,它能让你保存那些不需要显示在页面上的且对安全性要求不高的数据。也许这个时候应该有这 么一个疑问,为什么有了ViewState、Session和Cookie等状态保存机制,还需要用起HiddenField呢?     增加HiddenField,其实是为了让整个状态管理机制的应用程度更加全面。因为不管是ViewState、Cookie还是Session,都有  其失效的时候,比如用户因某种需求要求设置ViewState为false,或者环境条件限制使用Cookie,或者用户长时间没有动作导致  Session过期等等,那这个时候HiddenField无疑是最佳选择。   HiddenField控件的作用简单的说是用于存  储需要在向服务器的发送间保持的值。它作为 <input type= "hidden"/>  元素呈现,并且通过添加runat=”server”就可以使它成为标准的HTML服务器控件。下面列出的是ASP.NET HiddenField  Web服务器控件可以使用的属性和事件。 view plaincopy to clipboardprint? <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Button
 
 <a href="javascript:TestPostValue('test')">Test Post By HiddenField</a><br/>
 
 <asp:HiddenField ID="hidden" runat="server" />
 
 <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /><asp:Button
 
 <a href="javascript:TestPostValue('test')">Test Post By HiddenField</a><br/>
 
 <asp:HiddenField ID="hidden" runat="server" />
 
 
 
 ...
 
 view plaincopy to clipboardprint?
 <script type="text/javascript">
 
 function TestPostValue(value)
 
 {
 
 document.getElementById("hidden").value=value;
 
 document.getElementById("Button1").click();
 
 }
 
 </script>
 
 <script type="text/javascript">
 
 function TestPostValue(value)
 
 {
 
 document.getElementById("hidden").value=value;
 
 document.getElementById("Button1").click();
 
 }
 
 </script>view plaincopy to clipboardprint?
 protected void Button1_Click(object sender, EventArgs e)
 
 {
 
 Response.Write(hidden.Value);
 
 }
 
 
 二、利用Cookies
 
 protected void Button1_Click(object sender, EventArgs e)
 
 {
 
 Response.Write(hidden.Value);
 
 }
 
 
 二、利用Cookies
 Cookie的用法也和ASP中差不多。
 
 比 如我们建立一个名为aspcn,值为飞刀的cookie HttpCookie cookie = new HttpCookie["aspcn"];  cookie.Value = "飞刀"; Response.AppendCookie(cookie); 我们取出Cookie值也很简单  HttpCookie cookie = Request.Cookies["aspcn"]; cookieValue =  cookie.Value; 有时候我们想在一个Cookie中储存多个信息,那也没有问题。比如我们在名为aspcn的cookie下加多个信息  HttpCookie cookie = new HttpCookie("aspcn");  cookie.Values.Add("webmaster","飞刀");  cookie.Values.Add("writer","beige");  cookie.Values.Add("LinkColor","blue"); Response.AppendCookie(cookie);  取出信息也一样简单 HttpCookie cookie = Request.Cookies["aspcn"]; value1 =  cookies.Values["webmaster"]; value2 = cookies.Values["writer"];
 
 View State
 
 这 是一个新出来的东东,用法和Session一样,他的主要用途是记录Web  Control的状态。虽然是新出来的,但是和Application、Session的用法没有什么区别,所以也不想详细讲解了。  State["DropLoadIndex"] = 0 ;  基本用法如上:),但是请记住,他保存在的信息只能在一个aspx文件中使用。出去后,这个就没有用了,因为他的用途只是保存WEB控件的状态。
 
 我的实现代码:
 <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" /> 
 <a href="javascript:TestPostByCookies('Test')">Test Post By Cookies</a>
 
 <asp:Button ID="Button2" runat="server" Text="Button" OnClick="Button2_Click" />
 
 <a href="javascript:TestPostByCookies('Test')">Test Post By Cookies</a>
 
 view plaincopy to clipboardprint?
 function TestPostByCookies(value)
 
 {
 
 SetCookie("myCookie",value);
 
 document.getElementById("Button2").click();
 
 }
 
 
 
 
 
 //写cookies函数 作者:翟振凯
 
 function SetCookie(name,value)//两个参数,一个是cookie的名子,一个是值
 
 {
 
 var Days = 30; //此 cookie 将被保存 30 天
 
 var exp = new Date(); //new Date("December 31, 9998");
 
 exp.setTime(exp.getTime() + Days*24*60*60*1000);
 
 document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
 |