c# - 如何在 asp.net 网站中实现 SOTag 文本框?

标签 c# javascript php asp.net

我正在尝试在 asp.net 网站中使用 SOTag 文本框(这里是 SOTag 文本框的 link)。

PHP 示例是这样的:

<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>SOTag</title>
<style type="text/css">
body {
padding:30px;
width:960px;
margin:0 auto;
}
pre {
border:#ccc 1px solid;
background:#EFEFEF;
padding:10px;
}
</style>
<link rel="stylesheet" type="text/css" href="css/so_tag.css?<?php echo rand(0,1000); ?>">
<script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="js/so_tag.js?<?php echo rand(0,1000); ?>"></script>
</head>
<body>
<h2>jQuery.SO_Tag</h2>
<p>First a little description about what this plugin is for. This plugin is designed to be used with a backend database full of tags, not to be able to tag whatever you or the user wants to. I may add that feature if people want to but you cannot currently let your users add custom tags using this plugin</p>
<p>It's easy to get started, first include the JavaScript files and the CSS</p>
<pre>
<?php echo htmlentities('<link rel="stylesheet" type="text/css" href="css/so_tag.css">
<script type="text/javascript" src="js/jquery.js"></script>
<script type="text/javascript" src="js/so_tag.js"></script>'); ?>
</pre>
<br />
<pre>
<?php echo htmlentities('<form action="result.php" method="post">
<input type="text" value="3_php" name="single_example" id="single_example" />
<input type="submit" value="Submit" />
</form>

<script type="text/javascript">
$(\'#single_example\').sotag({
description : true
});
</script>'); ?>
</pre>
<br />
<p><strong>Here is an example with multiple tag fields</strong></p>
<form action="result.php" method="post">
<p>Programming words (example: PHP)</p>
<input type="text" value="" name="multi_example_programming_tags" class="multi_example_programming_tags" />
<br />
</form>
<script type="text/javascript">
$('.multi_example_programming_tags').sotag({
description : true
});
</script>
</body>
</html>

我的aspx页面是这样的:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
   <head runat="server">
       <script src="js/jquery-1.10.2.min.js" type="text/javascript"></script>
       <script src="js/so_tag.js" type="text/javascript"></script>
       <link rel="stylesheet" href="css/so_tag.css" />
       <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
       <title>Example</title>
   </head>
   <body>
       <form id="form1" runat="server" method="post">
           <input type="text" value="" name="multi_example_programming_tags" class="multi_example_programming_tags" />
       <br />
       </form>
       <script type="text/javascript">
           $('.multi_example_programming_tags').sotag({
              description: true
       });
       </script>
   </body>
</html>

然后我在 js/so_tag.js

中进行了更改
autocomplete_URL: 'SOTag.php'

autocomplete_URL: 'MyPage.cs'

现在我不明白如何在c# 中实现result.php 和SOtag.php。有人可以帮助我吗?

编辑1

我已经完成了您所解释的,但它不起作用。

在我写的web.config中

<urlMappings enabled="true">
    <add url="~/Index.aspx" mappedUrl="~/SoTag.ashx" /> 
  </urlMappings>

但在 index.aspx 中,结果是 json 字符串。

我已将项目示例上传到 MEGA--> 在这里你可以找到 link .

最佳答案

要使其在 asp.net 上运行,您需要执行一些步骤。

  1. 制作自动完成处理程序。
  2. 更改 so_tag.js询问新的处理程序。
  3. 将其附加到任何 asp.net 页面。

处理程序

你是怎么做到的。您创建一个处理程序 SoTag.ashx ,然后您返回 jSon 格式的标签。这里只是简单的返回看返回格式。

public void ProcessRequest (HttpContext context) 
{
    context.Response.ContentType = "text/plain";

    // Simple one TAG format SO_Tag, and one more for test
    string cRet = @"
    [{
        ""id"": 10, 
        ""tag"": ""SO_Tag"",
        ""tag_description"": ""SO_Tag: Tagging system based on StackOverflows tag search""
    },{
        ""id"": 11, 
        ""tag"": ""asp.net"",
        ""tag_description"": ""ASP.NET is a web application framework developed by Microsoft to allow programmers to build dynamic web sites and web applications.""
    }]";

    context.Response.Write(cRet);   
}

在你的代码中,在这个文件中你必须打开你的数据库,得到 q查询字符串,向你的数据库询问

  • 身份证
  • 标签
  • 标签描述

以 json 格式返回它们,插件显示它们。

更改so_tag.js

在 so_tag 上,找到 autocomplete_URL,并更改为 asp.net 处理程序,如:

autocomplete_URL: 'SoTag.ashx',

asp.net 页面

最简单的,只需将其附加到任何输入控件即可。

<head runat="server">
    <script type="text/javascript" src="jquery.min.js"></script>
    <link rel="stylesheet" href="so_tag.css" type="text/css" />
    <script type="text/javascript" src="so_tag.js"></script>


    <script type="text/javascript">
    jQuery( document ).ready(function() 
    {
        jQuery('#<%=txtSoTags.ClientID%>').sotag({
            description : true
        });
    });
    </script>
</head>
<body>
    <form id="form1" runat="server">
        <asp:TextBox runat="server" ID="txtSoTags" Width="300">1_some, 2_example, 3_tags</asp:TextBox>
    </form>
</body>
</html>

最后

这里唯一的难点是完成从数据库中读取数据的处理程序,需要添加很多标签信息,并以json格式返回。

这里的代码已经过测试并且可以工作,下面是截图:


(来源:planethost.gr)

小改进

SO_tag.js每次按键时调用太多,最好给一些时间来完成输入,因此在 keyup 上添加使用/更改此代码绑定(bind)。

    var cKeyPressTimer = null;
    // Now if the user starts typing show results
    elem.bind('keyup', function(e) 
    {
        if(cKeyPressTimer)
            clearTimeout(cKeyPressTimer);
        cKeyPressTimer = setTimeout(SO_update_results, 500);
    });

关于c# - 如何在 asp.net 网站中实现 SOTag 文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21804870/

相关文章:

javascript - 从对象数组中删除某些对象

php - 将perl关联数组转换为JSON并在PHP中读取

c# - Asp.net mvc 5 在添加内容时添加时间戳?

c# - 具有类型 T 的通用构造函数的通用存储库

javascript - a && b && c VS if - 性能上有什么区别?

php - ExpressionEngine 不关闭 MySQL 和 Apache 连接

php - 在 PHP 中传递带有参数的回调

c# - 无法捕获的异常,第 2 部分

c# - 从 asp.net mvc 站点中的外部站点获取帖子响应

javascript - 同位素数据过滤器在 Ajax 回调后不过滤