0
votes

Is there a way to read ASP markup from a text file, and render it out onto the page?

I am using the Azure Maps service, and we have popups on the pins. I would like to add an ASP button to the popup so users can "select" a location, but I can't figure out how to do it.

Currently we have the "template" for our popup content stored in a text file that is read at the time the map is created. I can't seem to insert an ASP control into this text file, the markup is rendered exactly as in the text file, so no button renders, but the text <asp:Button ID="Button1" runat="server" Text="Button" /> is added to the HTML output, it just renders as nothing.

Any suggestions on how to add an ASP button would be great.

UPDATE:

The reason the template is stored in a text file is because it is read and parsed into a method that replaces certain HTML elements in that text file with actual data that is used to show and hide certain items in the template depending on what map pin is selected. The Azure Maps variable popupTemplate is a string that contains the html markup to display in a popup. I need to pass that HTML string to the Azure Maps popupTemplate so it can be shown. I don't think a partial view would work for that reason.

The reason I want an ASP button is really for ease of use. The map is inside of a ASCX control. The button, when clicked, needs to update the value of a property on the control, close a Bootstrap modal, and then execute an event handler that is caught by the parent page hosting the control. That parent page then updates an update panel which displays the value of the property in that control. I'm sure it could be done with just javascript, but I have no idea how to accomplish something like that. I'd be open to tutorials on that, but I will say that when I added just a standard HTML button to my template and included an onclick event to just display an alert, the map failed to load. I remove the onclick event from the button, and it loads fine. I'm not sure why, but that's a different problem.

UPDATE 2:

@ADyson, right now all of the processing is done server side, the only processing done client side is when the user clicks on an actual pin on the map. When that happens the JS provided by Azure causes a popup to appear and it replaces placeholders with actual data from a (now static) data source that was created server side before the map was ever shown.

When the button is clicked it wouldn't refresh the entire page. It closes the modal using data attributes that Bootstrap provides (via javascript), then calls a server side event that causes an asyncpostback which updates the text in some controls that reside inside of an update panel and then calls the Update() method on that update panel so that the selected map point is shown as being selected. At no time would the entire page refresh. I know this because in addition to our map points, below the map is a text list of the search results. That text list also shows a button. Those buttons are ASP controls that are added using a Repeater control and they do what I need the buttons in the popup template to do.

Is there a way to call an ASP's click event from another non-ASP button control? That would do it, but because the buttons in the Repeater list are added dynamically, I have no idea how to reference them since I can't set their ID's to something that I can reference. The buttons do have a data attribute that is unique though that I could reference, but I'm not sure how I can get a reference to it. Can I search for a data attribute value and return a reference to the button using javascript and then set my HTML's onclick event to call that button's click() event?

The reason I don't want to just update the HTML client side is because when a user clicks the "Select This Location" button, a property in the ASCX control is updated. That property is used for further processing later.

I was able to figure out why the map stopped working when adding onclick event data. It was an error that failed to escape single quotes when generating the output script.

I could share the code, but I'm not sure how much you would need to see or what code would even be helpful.

The class that contains the methods and properties to generate the map javascript (including the popup template that needs the button) is about 290 lines and has references to 4 other classes and 2 interfaces. It could be difficult to share it all. Some of the code also contains some proprietary and confidential information that would need to be scrubbed.

It appears that you seem to be telling me that this isn't really possible and that I would need to find a different way to do this.

1
why do you need to add a button using <asp: syntax? You realise that all ASP.NET does when it runs is turn that into a HTML <button anyway? (It might do a bit of other clever stuff to do with binding it to some specific postback parameters, but it's not the point). Is your azure map held within an aspx page? In that case why are you storing your templates in text files? Why not in an ASP.NET partial view? That would then be executed by the asp.net engine server side and turned into suitable HTML markup, with server-side bindings. You haven't made the situation 100% clear but that's my takeADyson
@ADyson -- I updated my question with some additional details.Zachary Weber
"The reason the template is stored in a text file is because it is read and parsed into a method that replaces certain HTML elements in that text file with actual data that is used to show and hide certain items" ... That's more or less what the ASP.NET engine does. So if you used a partial (loaded via Ajax on demand, possibly with contextual parameters passed to it in each request) then you'd basically just be moving the processing from client to server I think.ADyson
"The button, when clicked, needs to update the value of a property on the control, close a Bootstrap modal, and then execute an event handler that is caught by the parent page hosting the control. That parent page then updates an update panel ". This really makes very little sense. If you execute a server side handler (outside an update panel) then it refreshes the entire page from the server. So in that case updating controls, closing morals and triggering update panels would be totally redundant because the page would get reloaded from the server.ADyson
If you have an update panel, that allows you to do partial updates (it uses Ajax under the hood). So your button probably just wants to do something client side which would either trigger the update panel or, maybe just directly update the HTML on the page without going to the server at all. It depends if you need to save anything to the database at the same time. If not, then keep it all client side would be my advice. As for the specifics of exactly what you need to do in that regard, without seeing any code it's impossible to give further detailed advice.ADyson

1 Answers

0
votes

Popups generally are generated on the fly. However your ASP.NET code appears to run at server time which means it has to be generated ahead of time. Using the built in popup isn't a good option for your scenario. If you must use ASP.NET controls rather than standard HTML, I recommend creating a panel that is separate from the map and then floating it over top of the map as needed using absolute positioning. This is essentially what the popup does under the covers, but this would allow you to use classic ASP.NET functionality.