簡體   English   中英

使用C#動態地將HTML添加到ASP.NET頁面

[英]Dynamically adding HTML to ASP.NET page with C#

我正在嘗試在ASP.NET頁面上創建一個簡單的商店定位器。 我讓用戶輸入他們的郵政編碼,然后C#用它創建一個變量並將其附加到網址的末尾,以便在他們附近的Google地圖上搜索該商店。 然后我需要它動態添加一個iframe標記,其源代碼為該頁面的url。

就像是:

<asp:TextBox ID="TextBox1" placeholder="Zip code" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Find locations" onclick="Button1_Click" />

和:

protected void Button1_Click(object sender, EventArgs e)
{
    var zipCode = TextBox1.Text;

    HtmlGenericControl iframe = new HtmlGenericControl();
    iframe.ID = "iframe";
    iframe.TagName = "iframe";
    iframe.Attributes["class"] = "container";
    iframe.Attributes["src"] = "https://www.google.com/maps/preview#!q=gnc+near%3A+" + zipCode;
    this.Add(iframe);

}

我相信這是正確的,直到最后一行,任何想法?

您實際上可以將runat="server"添加到ASP.NET webforms中頁面上的任何標准HTML標記。 在您的頁面上試試這個:

<iframe id="MyIframe" runat="server"></iframe>

這樣,您就可以在代碼后面按名稱訪問iframe。 然后,您可以通過編寫如下語句來操縱事物:

MyIframe.Visible = true;

MyIframe.Attributes.Add("src", "https://www.google.com/maps/preview#!q=gnc+near%3A+" + zipcode);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM