Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/282.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 是否在ASP.NET(c)中将表格转换为HTML?_C#_Asp.net_Jquery_Html Table_Placeholder Control - Fatal编程技术网

C# 是否在ASP.NET(c)中将表格转换为HTML?

C# 是否在ASP.NET(c)中将表格转换为HTML?,c#,asp.net,jquery,html-table,placeholder-control,C#,Asp.net,Jquery,Html Table,Placeholder Control,我有一个表对象,它是我在代码隐藏中创建和填充的。出于各种原因,必须这样做 我从客户端通过AJAX调用WebMethod,从下拉列表中发送一个变量,然后在代码隐藏的静态方法中填充该表。我需要将表放入ASP.Net占位符中。我无法从静态方法调用占位符。请尝试使用ASP.NET HTTP处理程序,而不是使用ASP.NET AJAX页面方法从客户端AJAX调用返回数据,因为这样可以更好地控制通过HTTP头返回的内容,而不必对ASP.NET AJAX页面方法调用的结果进行编码,像这样: public cl

我有一个表对象,它是我在代码隐藏中创建和填充的。出于各种原因,必须这样做


我从客户端通过AJAX调用WebMethod,从下拉列表中发送一个变量,然后在代码隐藏的静态方法中填充该表。我需要将表放入ASP.Net占位符中。我无法从静态方法调用占位符。

请尝试使用ASP.NET HTTP处理程序,而不是使用ASP.NET AJAX页面方法从客户端AJAX调用返回数据,因为这样可以更好地控制通过HTTP头返回的内容,而不必对ASP.NET AJAX页面方法调用的结果进行编码,像这样:

public class GetHtmlHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Grab the drop down list variable value from the query string

        // Use controls to build table or do it via StringWriter

        // Create a table row with three cells in it
        TableRow theTableRow = new TableRow();
        for (int theCellNumber = 0; theCellNumber < 3; theCellNumber++)
        {
            TableCell theTableCell = new TableCell();
            tempCell.Text = String.Format("({0})", theCellNumber);
            theTableRow.Cells.Add(theTableCell);
        }

        // Create writers to render contents of controls into
        StringWriter theStringWriter = new StringWriter();
        HtmlTextWriter theHtmlTextWriter = new HtmlTextWriter(theStringWriter);

        // Render the table row control into the writer
        theTableRow.RenderControl(theHtmlTextWriter);

        // Return the string via the StringWriter
        context.Response.Write(theStringWriter.ToString());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "PATH_TO_HANDLERS/GetHtmlHandler.ashx?id=YOUR_DROP_DOWN_VALUE",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            // Select the placeholder control by class here and put HTML into it
            $('.ThePlaceHolder').html(result);
        }
    });
}); 
<asp:PlaceHolder id="PlaceHolder1" runat="server" ClientIDMode="Static" />
// Select the placeholder control by ID here and put HTML into it
$('#<%= PlaceHolder1.ClientID %>').html(result);
注意:上述选择器要求向占位符控件添加CssClass属性,如下所示:

public class GetHtmlHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Grab the drop down list variable value from the query string

        // Use controls to build table or do it via StringWriter

        // Create a table row with three cells in it
        TableRow theTableRow = new TableRow();
        for (int theCellNumber = 0; theCellNumber < 3; theCellNumber++)
        {
            TableCell theTableCell = new TableCell();
            tempCell.Text = String.Format("({0})", theCellNumber);
            theTableRow.Cells.Add(theTableCell);
        }

        // Create writers to render contents of controls into
        StringWriter theStringWriter = new StringWriter();
        HtmlTextWriter theHtmlTextWriter = new HtmlTextWriter(theStringWriter);

        // Render the table row control into the writer
        theTableRow.RenderControl(theHtmlTextWriter);

        // Return the string via the StringWriter
        context.Response.Write(theStringWriter.ToString());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "PATH_TO_HANDLERS/GetHtmlHandler.ashx?id=YOUR_DROP_DOWN_VALUE",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            // Select the placeholder control by class here and put HTML into it
            $('.ThePlaceHolder').html(result);
        }
    });
}); 
<asp:PlaceHolder id="PlaceHolder1" runat="server" ClientIDMode="Static" />
// Select the placeholder control by ID here and put HTML into it
$('#<%= PlaceHolder1.ClientID %>').html(result);
现在,您的jQuery选择器如下所示:

public class GetHtmlHandler : IHttpHandler
{
    public void ProcessRequest(HttpContext context)
    {
        // Grab the drop down list variable value from the query string

        // Use controls to build table or do it via StringWriter

        // Create a table row with three cells in it
        TableRow theTableRow = new TableRow();
        for (int theCellNumber = 0; theCellNumber < 3; theCellNumber++)
        {
            TableCell theTableCell = new TableCell();
            tempCell.Text = String.Format("({0})", theCellNumber);
            theTableRow.Cells.Add(theTableCell);
        }

        // Create writers to render contents of controls into
        StringWriter theStringWriter = new StringWriter();
        HtmlTextWriter theHtmlTextWriter = new HtmlTextWriter(theStringWriter);

        // Render the table row control into the writer
        theTableRow.RenderControl(theHtmlTextWriter);

        // Return the string via the StringWriter
        context.Response.Write(theStringWriter.ToString());
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
$(document).ready(function() {
    $.ajax({
        type: "POST",
        url: "PATH_TO_HANDLERS/GetHtmlHandler.ashx?id=YOUR_DROP_DOWN_VALUE",
        data: "{}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            // Select the placeholder control by class here and put HTML into it
            $('.ThePlaceHolder').html(result);
        }
    });
}); 
<asp:PlaceHolder id="PlaceHolder1" runat="server" ClientIDMode="Static" />
// Select the placeholder control by ID here and put HTML into it
$('#<%= PlaceHolder1.ClientID %>').html(result);
注意:在可能的情况下,我宁愿避免使用角括号符号


请用更多信息展开您的问题。目前还不清楚您到底遇到了什么问题-要么无法将表控件放入占位符,要么无法将数据从表转换为WebMethod输出。我已尝试将表添加到占位符,但正如我所说,我无法从静态方法访问占位符。我目前正在尝试将表对象转换为一种格式,我可以通过WebMethod/AjaxI将其传回客户端。我需要将表插入占位符,但表是在静态方法中创建的……共享您尝试过的代码,以便使用TableRow和TableCell控件构建farMy表对象。如何将其转换为字符串?@arcadeRob-answer已更新,以显示示例一个表行,其中三个单元格通过StringWriter和HtmlTextWriter对象呈现为字符串。它正在工作,但我正在丢失在中添加的CSS和其他属性C@arcadeRob-您可以切换jQuery选择器以使用占位符的ID,但是,如果使用母版页,则需要小心名称损坏。ClientID解决了这个问题。请参阅更新的答案。