2011-07-10 78 views
2

我有一个文件夹结构树,我想在几个页面上重复使用,我正在为此构建一个用户控件(webForms),它带有一个小的jquery,它可以从httphandler显示html。到现在为止还挺好。我希望用户能够点击我从我的http处理程序返回的HTMl文件夹上的链接,但似乎我的页面无法将页面上的jquery与http处理程序返回的jquery连接起来。在我的ascx文件中,我有文件夹链接的事件处理程序,并且链接在我的http处理程序中生成。问题与asp.net和jQuery AJAX

public void ProcessRequest(HttpContext context) 
     { 
      List<MultimediaType> types = typeRepository.LoadAll(); //load all the types for displaying in the list 
      types.ForEach(x => 
       { 
        multimediaTypes.Add(x.Id, x.Name); 
       }); 
      int folderId = string.IsNullOrEmpty(context.Request["folderid"]) ? 0 : int.Parse(context.Request["folderid"]); 
      //load folders, from query string, if no folderid in querystring, then default to root 
      List<MultimediaFolder> folders = repository.LoadAll(folderId); 


      //load files 
      List<Multimedia> files = fileRepository.LoadAll(folderId); 
      string folderData = BuildFolderList(folders); 
      string fileData = BuildFileList(files); 
      context.Response.ContentType = "text/html"; 
      context.Response.Write("<b>" + DateTime.Now.ToString() + "</b>"); 
      context.Response.Write("<script type='text/javascript'>");    
      context.Response.Write("$('.folderlink').click(function() {"); 
      //context.Response.Write("$.get('FolderStructureHandler.ashx', function (data) {"); 
      context.Response.Write("alert('test');"); 
      context.Response.Write("});"); 
      context.Response.Write("});"); 
      context.Response.Write("</script>"); 

      context.Response.Write("<table border='1'>"); 
      context.Response.Write("<th><td>Name</td><td>Type</td></th>"); 
      context.Response.Write(folderData); 
      context.Response.Write(fileData); 
      context.Response.Write("</table>"); 
     } 

和我的用户控件的代码很简单:

<script type="text/javascript"> 
    $(document).ready(function() { 
     $.get("FolderStructureHandler.ashx", function (data) { 
      $("#folderList").html(data); 
     }); 
    }); 
</script> 
<div id="folderList"></div> 

两种方法写的HTML很简单:

private string BuildFolderList(List<MultimediaFolder> folders) 
     { 
      StringBuilder builder = new StringBuilder(); 
      foreach (MultimediaFolder folder in folders) 
      { 
       builder.AppendFormat("<tr><td width='20'><img src='../Images/folder.png'/></td><td colspan='2'><a href='#' class='folderlink'>{0}</a></td></tr>", folder.Name); 
      } 
      return builder.ToString(); 
     } 

我试图拥有jQuery的在.ascx标记中使用.click()事件处理程序也没有成功,现在我试图用http处理程序中的html发送它,但仍然没有成功。有没有人可以解决这个问题。

即时得到装载在第一时间的数据,并认为我的HTML表格,但是当我点击一个链接没有任何反应......

真诚,

编辑 小的修改所产生的JQuery从HttpHandler的,现在让警报(我有一个语法错误),但我现在每次调用处理,但日期时间似乎并没有改变......

新的HttpHandler代码:

//load files 
      List<Multimedia> files = fileRepository.LoadAll(folderId); 
      string folderData = BuildFolderList(folders); 
      string fileData = BuildFileList(files); 
      context.Response.ContentType = "text/html"; 
      context.Response.Write("<b>" + DateTime.Now.ToString() + "</b>"); 
      context.Response.Write("<script type='text/javascript'>");    
      context.Response.Write("$('.folderlink').click(function() {"); 
      context.Response.Write("$.get('FolderStructureHandler.ashx', function (data) {"); 
      context.Response.Write("alert(data);"); 
      context.Response.Write("});"); 
      context.Response.Write("});"); 
      context.Response.Write("</script>"); 

      context.Response.Write("<table border='1'>"); 
      context.Response.Write("<th><td>Name</td><td>Type</td></th>"); 
      context.Response.Write(folderData); 
      context.Response.Write(fileData); 
      context.Response.Write("</table>"); 

这会创建一个带有表格的弹出窗口,但日期总是相同的,就好像它被缓存了一样。

+1

日期后24个小时内将改变:)你可以做'DateTime.Now.ToString( “YYYY.MM.DD HH:MM:SS:FFFF”)'包括毫秒,看看它的缓存或不 – Rafay

回答

0

听起来好像你的元素正在写入事件处理程序分配后。尝试使用

.live("click", function)