1

<asp:ContentPlaceHolder>プログラムでコンテンツを設定する最も簡単な方法は何ですか? 私はMaster.FindControl電話をする必要があると思いますか?

4

3 に答える 3

4

ページが MasterPage から継承する場合、次のように、ページに asp:Content コントロールが必要です。

<asp:Content runat="server" ID="myContent" ContentPlaceHolderID="masterContent">
</asp:Content>

コードビハインドでこれを参照し、必要なものを追加できるはずです。

public void Page_Load( object sender, EventArgs e )
{
    HtmlContainerControl div = new HtmlGenericControl( "DIV" );
    div.innerHTML = "....whatever...";
    myContent.Controls.Clear();
    myContent.Controls.Add(div);
}
于 2008-11-15T04:08:48.323 に答える
0

コントロール (プレースホルダーなど) を再帰的に検索するカスタム拡張メソッドを使用して、探しているコントロールを Id で見つけて返します。その後、返されたコントロールを必要に応じて設定できます。入力するコントロールのリストを反復処理する foreach ループでこれを呼び出します。

public static class ControlExtensions
{
    /// <summary>
    /// recursive control search (extension method)
    /// </summary>
    public static Control FindControl(this Control control, string Id, ref Control found)
    {
        if (control.ID == Id)
        {
            found = control;
        }
        else
        {
            if (control.FindControl(Id) != null)
            {
                found = control.FindControl(Id);
                return found;
            }
            else
            {
                foreach (Control c in control.Controls)
                {
                    if (found == null)
                        c.FindControl(Id, ref found);
                    else
                        break;
                }

            }
        }
        return found;
    }
}
于 2008-11-15T09:07:02.293 に答える
0

空白のページにコントロールを追加する場合は、Page.Controls.Add()....いいえ?

于 2008-11-14T19:06:41.257 に答える