C# 使用文本框作为“发布” /“粘性备忘录”客户端事件

发布于 2024-11-08 16:00:28 字数 249 浏览 0 评论 0原文

我想要一个文本框充当“发布”或“粘性备忘录”,就像小部件 Igoogle 或 Windows 7 小部件一样。

想法:

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

每次用户在文本框中键入内容时,它都会调用 Javascript 将文本保存到 cookie 中。

有人可以给我提示吗?

I want a textbox to act like a "post it" or "Sticky memo" just like widget Igoogle or Windows 7 widget.

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.

Could somebody give me a hint?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

铁憨憨 2024-11-15 16:00:29

这有点快速和肮脏,但会让你继续下去。

网络上有大量 setCookie/getCookie JS 片段。我使用了这些:

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

现在的代码:

<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>

这将适用于纯 HTML。在您使用 ASP.NET 标记和控件的情况下,ID 属性具有不同的含义,因此您需要让 JS 知道实际的客户端 ID。例如这样:

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

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

This is somewhat quick and dirty but will get you going.

There's plenty of setCookie/getCookie JS snippets around the web. I used these:

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. 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. This way for example:

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

var memoElement = document.getElementById(memoElementId);
(...)
一世旳自豪 2024-11-15 16:00:29

当然。玩“改变”事件:

这只是使用此事件并更新您之前使用 JavaScript 创建的一些 cookie。

Of course. Play with "change" event:

It's just about using this event and update some cookie that you previously created with JavaScript too.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文