-2

コード ビハインドで作成して入力した Table オブジェクトがあります。さまざまな理由から、このようにする必要がありました。

クライアント側から AJAX 経由で WebMethod を呼び出して、ドロップダウン リストから変数を送信すると、コード ビハインドの静的メソッドにテーブルが設定されます。テーブルを ASP.Net プレースホルダーに入れる必要があります。静的メソッドから PlaceHolder を呼び出すことができません。

4

1 に答える 1

2

クライアント側の AJAX 呼び出しからデータを返すために ASP.NET AJAX ページ メソッドを使用する代わりに、ASP.NET 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;
        }
    }
}

クライアント側で、次のように jQuery.ajax()関数を使用して HTTP ハンドラを呼び出します。

$(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);
        }
    });
}); 

注: 上記のセレクターでは、次のようにCssClass属性をPlaceHolderコントロールに追加する必要があります。

<asp:PlaceHolder id="PlaceHolder1" runat="server" CssClass="ThePlaceHolder" />

アップデート:

classjQuery セレクターで名前を使用する代わりに、 の を使用IDできますPlaceHolder。これは、ASP.NET でマスター ページを使用するときに名前マングルが発生する可能性があるためです。とにかくClientIDMode、次のように使用できる属性があります。

<asp:PlaceHolder id="PlaceHolder1" runat="server" ClientIDMode="Static" />

jQuery セレクターは次のようになります。

// Select the placeholder control by ID here and put HTML into it
$('#<%= PlaceHolder1.ClientID %>').html(result);

注: 山かっこ ( <%= %>) 表記はできるだけ避けたいと思います。

于 2013-12-11T16:26:07.747 に答える