简体   繁体   English

C#使用文本框作为“发布” /“便笺”客户端事件

[英]C# using textbox as “post it” / “sticky memo” client side event

I want a textbox to act like a "post it" or "Sticky memo" just like widget Igoogle or Windows 7 widget. 我希望文本框像小部件Igoogle或Windows 7小部件一样充当“张贴”或“粘滞便笺”。

The idea: 这个想法:

 <asp:TextBox ID="TextBox1" runat="server"> 
 </asp:TextBox>

Every time that user types into the textbox it calls Javascript to save the text into cookies. 每次用户在文本框中键入内容时,都会调用Javascript将文本保存到Cookie中。

Could somebody give me a hint? 有人可以给我提示吗?

This is somewhat quick and dirty but will get you going. 这有点快速又肮脏,但可以助您一臂之力。

There's plenty of setCookie/getCookie JS snippets around the web. 网上有很多setCookie / getCookie JS片段。 I used these: 我用了这些:

http://www.dotnetspark.com/kb/1480-use-cookies-javascript-getcookie-setcookie.aspx http://www.dotnetspark.com/kb/1480-use-cookies-javascript-getcookie-setcookie.aspx

Teh code now: 现在代码:

<input type="text" id="txtMemo" />

<script type="text/javascript">

function setCookie(CookieName, CookieVal, CookieExp, CookiePath, CookieDomain, CookieSecure)
{
     var CookieText = escape(CookieName) + '=' + escape(CookieVal); //escape() : Encodes the String
    CookieText += (CookieExp ? '; EXPIRES=' + CookieExp.toGMTString() : '');
    CookieText += (CookiePath ? '; PATH=' + CookiePath : '');
    CookieText += (CookieDomain ? '; DOMAIN=' + CookieDomain : '');
    CookieText += (CookieSecure ? '; SECURE' : '');

    document.cookie = CookieText;
}

// This functions reads & returns the cookie value of the specified cookie (by cookie name) 
function getCookie(CookieName)
{
    var CookieVal = null;
    if(document.cookie)       //only if exists
    {
           var arr = document.cookie.split((escape(CookieName) + '=')); 
           if(arr.length >= 2)
           {
               var arr2 = arr[1].split(';');
               CookieVal  = unescape(arr2[0]); //unescape() : Decodes the String
           }
    }
    return CookieVal;
}

var memoCookieName = "txtMemo_value";
var memoElementId = "txtMemo";

var memoElement = document.getElementById(memoElementId);

memoElement.value=getCookie(memoCookieName);
memoElement.onkeyup = function() {
    setCookie(memoCookieName,this.value, new Date(new Date().getTime()+1000*60*60*24*30));
};

 </script>

This will work with plain HTML. 这将适用于纯HTML。 In your case with ASP.NET markup and controls the ID property has a different meaning, so you need to make your JS aware of the actual client ID. 在使用ASP.NET标记和控件的情况下,ID属性具有不同的含义,因此您需要使JS知道实际的客户端ID。 This way for example: 这样,例如:

(...)
var memoCookieName = "txtMemo_value";
var memoElementId = "<%= TextBox1.ClientID %>";

var memoElement = document.getElementById(memoElementId);
(...)

Of course. 当然。 Play with "change" event: 玩“更改”事件:

It's just about using this event and update some cookie that you previously created with JavaScript too. 这只是有关使用此事件并更新您先前使用JavaScript创建的某些cookie。

暂无
暂无

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

相关问题 如何使用客户端的HTML5 / Javascript和控制器的C#代码将MVC中的录制视频作为blob发布/上传? - How to post/upload recorded video as blob in MVC using HTML5/ Javascript at client side and C# code at controller? 使用javascript文本框进行客户端验证 - client side verification using javascript textbox MongoDB C#以及如何使用JavaScript从客户端更新 - MongoDB C# and how to update from Client Side using javascript 使用WebAPI的Webforms C#客户端验证CustomValidator - Webforms C# client side validation CustomValidator using WebAPI 当客户端使用Java脚本时,文本框将触发autopostback事件 - autopostback event will fire for textbox when java script desable at client side 如何在客户端(使用javascript)和服务器端验证mac地址和ipaddress(使用c#) - How to Validate mac address & ipaddress in client side(using javascript) & server side(using c#) 使用xval的C#mvc2客户端表单验证,防止发布 - C# mvc2 client side form validation with xval, prevent post 使用Ajax调用.net,C#获取客户端的服务器端字符串值 - Get server side string value in client side using ajax call .net,C# C#-MVC-JSON-使用ViewBag从服务器端到客户端返回列表 - C# - MVC - JSON - Return List From Server Side to Client Side Using ViewBag 如何使用 Jquery 客户端或 C# 服务器端验证泛卡号格式 - How to Validate Pan Card Number Format Using Jquery Client Side Or C# Server side
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM