[英]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.