0

I'm trying to create a simple store locator on an ASP.NET page. I have the user enter their zipcode, then C# creates a variable with it and appends it to the end of a url to search for that store on Google maps near them. I then need it to dynamically add an iframe tag with it's source as that url to the page.

Something like:

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

And:

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);

}

I believe it's correct until the last line, any thoughts?

4
  • 1
    You could add it the iframe control to a Placeholder control. This is also a one-liner with jQuery and wouldn't require a postback, if you have that option. Commented Aug 6, 2013 at 18:02
  • 1
    Why not just put it into a div and then display the div instead of trying to add the iframe to the page? Commented Aug 6, 2013 at 18:03
  • We do, how would it be done with jQuery? Commented Aug 6, 2013 at 18:03
  • 2
    Having never done this before, what's wrong with the iframe? It's what Google recommends for embedding maps. support.google.com/maps/answer/72644?hl=en Commented Aug 6, 2013 at 18:12

1 Answer 1

2

You can actually add runat="server" to any standard HTML tag on the page in ASP.NET webforms. Try this on your page:

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

This will give you access to your iframe by name in code behind. You'll then be able to manipulate things by writing statements like:

MyIframe.Visible = true;

and

MyIframe.Attributes.Add("src", "https://www.google.com/maps/preview#!q=gnc+near%3A+" + zipcode);
Sign up to request clarification or add additional context in comments.

2 Comments

That looks like a great solution! I tried it but it's saying that my iframe is a field but it's being used as a type. Am I doing something wrong?
Oops, my bad, had an extra bracket.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.