Can't reference iFrame in code behind file - asp.net

I've got an iFrame in an aspx marked as runat="server". But for some reason I can't reference it in the code behind file.
This is how it's declared in the aspx.
<iframe ID="iFrame" runat="server"></iframe>
This is where it throws an error in the code behind:
iFrame.Attributes["src"] = strUrl + strFile.Split('.')[0] + ".html";
The error is "The name 'iFrame' does not exist in the current context"
Anyone know why I can't reference it?

Where is your iframe embedded?
Having this code
<body>
<iframe id="iFrame1" runat="server"></iframe>
<form id="form1" runat="server">
<div>
<iframe id="iFrame2" runat="server"></iframe>
</div>
</form>
I can access with iFrame1.Attributes["src"] just to iFrame1 and not to iFrame2.
Alternatively, you can access to any element in your form with:
FindControl("iFrame2") as System.Web.UI.HtmlControls.HtmlGenericControl

Related

How to stop a page refresh in angular 2 within aspx

The pagemethod works, however when I use the webmethod it refreshes the page destroying the reason to use angular 2 in the first place.
How do I prevent the form from refreshing the page?
index.aspx
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManagerMain"
runat="server"
EnablePageMethods="true" >
</asp:ScriptManager>
<my-app>Loading...</my-app>
</form>
</body>
index.aspx.cs
[WebMethod]
public static string getString()
{
return "Test";
}
app.component.html
<div>
<Button (click)="btnSubmit_Click">test</Button>
</div>
app.component.ts
btnSubmit_Click()
{
var test = window['PageMethods'].getString(this.onSucces, this.onError);
}
note:
At this moment I'm trying to use angular 2 with aspx for the company if it works it might become a standaard for small frontend api's and since its quite limited on information I appreciate any help.
You should make button type as button, because by default button type is submit. button with type submit may lead to post back a page.
<div>
<Button type="button" (click)="btnSubmit_Click()">test</Button>
</div>

Not retrieving UserControl from a placeholder control

I have a datagrid with dropdown template under .ascx page. I load it on placeholder control on my .aspx page by using:
Control c = Page.LoadControl("~/CreateChecklist/GridWithMultiDropdown.ascx");
PlaceHolder1.Controls.Add(c);
Now i am trying to read datagrid template white in under placeholder from .aspx page but its show me the "ctl00$palceholder$drodownlist" not taking as a control.
Please help me how can i retrieve this datagrid data under placeholder.
Discard your server side code, go to aspx page then register your control and then place ur control tag inside placeholder and bang, it will work :)
Solution:
<%# Register Src="~/WebUserControl.ascx" TagName="myControl" TagPrefix="myControl" %>
<html>
<head runat="server">
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:PlaceHolder ID="phControl" runat="server">
<myControl:myControl ID="ctrlControl" runat="server" />
</asp:PlaceHolder>
</div>
</form>
</body>
</html>
Regards
Ali Muhammad

Issue adjusting iframe height when opened as popup using ModalPopupextender

I am using a ModalPopupextender Ajax Control to open aspx page in a modal Popup. aspx page is opened in a iframe as shown in below code.The content which is loaded in iframe is dynamic hence I cannot give a fixed height to iframe. I want that I should be able to adjust the height as per the content every time popup is being opened. I have a function to resize the iframe height and I am using that successfully on my other pages where iframe gets populated in the window itself but not able to adjust the height when iframe is opened in a POPUP. I have already tried window onload, iframe onload events without any success.
<asp:ModalPopupExtender ID="ModalPopupExtender2" BackgroundCssClass="transparentBG"
runat="server" CancelControlID="ButtonCancel" OkControlID="ButtonDone"
TargetControlID="btnAddNew" PopupControlID="DivAddWindow" Drag="true" >
</asp:ModalPopupExtender>
<div class="popup_Buttons" style="display: none">
<input id="ButtonDone" value="Done" type="button" />
<input id="ButtonCancel" value="Cancel" type="button" />
</div>
<div id="DivAddWindow" style="display: none;">
<iframe id="IframeEdit" scrolling="no" src="MasterPage.aspx" width="700px">
</iframe>
</div>
I would really appreciate if somebody can lead me the solution to resolve this.
Try this:
<iframe id="IframeEdit" onload="iframeLoaded()" scrolling="yes" src="MasterPage.aspx" width="700px">
</iframe>
following javascript function would get height from the iframe content.
<script type="text/javascript">
function iframeLoaded() {
var iFrameID = document.getElementById('IframeEdit');
if (iFrameID) {
iFrameID.height = "";
iFrameID.height = iFrameID.contentWindow.document.body.scrollHeight + "px";
}
}
</script>
I checked it in even ModalPopupextender, it works.

hide div in aspx page and show div in aspx.vb page

I have following code in aspx page:
<div id="a" runat="server" style="display:block;">
abc
</div>
I am trying to show the div in code like this:
a.Visible = True
But this is not working. Can anyone suggest how to do this without using any scripting language?
A div with runat=server becomes a HtmlGenericControl on serverside. This has the Visible-property as every server-control. So you can hide it on serverside. But that means on clientside it won't get rendered at all.
If you instead want it to be rendered invisibly, add the Styledisplay:none:
a.Style.Add("display","none")
Then you can also switch the visibility on clientside.
Apart from that your tag is malformed, change
runat="server
to
runat="server"
You are missing doube quotes after runat Server. It should be like this..
<div id="a" runat="server" style="display:block;">
abc
</div>
Or in code Behind to hide the div
a.Style.Add("display","none")
in code Behind to show the div
a.Style.Add("display","block")
Try this:
a.Attributes.Add("style", "display:block;");
Mode details:
http://msdn.microsoft.com/en-us/library/7512d0d0(v=vs.71).aspx
http://msdn.microsoft.com/en-us/library/system.web.ui.control.visible.aspx
<div id="a" runat="server" style="visibility:hidden" >
</div>
you can try : hidden in code vb.net
a.Attributes("style") = "visibility:hidden"
you can try : visible
a.Attributes("style") = "visibility:visible"
thanx
a.Style.Add("display", "block")
for visible
a.Style.Add("display", "none")
for hide

ContentPlaceHolders: Repeated Content

Scenario
I have an application using asp.net Master Pages in which I would like to repeat some content at the top and bottom of a page. Currently i use something like this:
Master Page
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content -->
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<!-- content repeated -->
</asp:Content>
Maintenance
As you know, repeating things in code is usually not good. It creates maintenance problems. The following is what I would like to do but will obviously not work because of the repeated id attribute:
Master Page
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<!-- content (no repetition) -->
</asp:Content>
Possible?
Is there a way to do this using asp.net webforms? The solution does not necessarily have to resemble the above content, it just needs to work the same way.
Notes
I am using asp.net 3.0 in Visual Studio 2008
Hey, if someone still needs a solution for this here is what I did:
Put it in the Code File of your master page
protected override void RenderChildren(HtmlTextWriter writer)
{
Control mainPlaceHolder = FindControl("MainContentPlaceHolder");
Control doublePlaceHolder = FindControl("ContentDoublePlaceHolder");
StringBuilder sb = new StringBuilder();
using (StringWriter sw = new StringWriter(sb))
using (HtmlTextWriter tw = new HtmlTextWriter(sw))
{
mainPlaceHolder.RenderControl(tw);
}
LiteralControl lc = new LiteralControl(sb.ToString());
doublePlaceHolder.Controls.Add(lc);
base.RenderChildren(writer);
}
I'm not sure about performance issues here, but it certainly works.
In the case where you just want to repeat a string, this worked for me:
<asp:ContentPlaceHolder ID="TitleContent" runat="server" />
Can be referenced elsewhere as:
<% Dim title As LiteralControl = TitleContent.Controls.Item(0)
Response.Write(title.Text)
%>
No warranty on that being the right course of action though :)
As others have said, depending on the content itself there are shortcuts that can be taken depending on the type of content that will be there.
Assuming however you need the "content" to be fully functioning asp.net code then I would suggest a UserControl for each ContentPage that contains the content itself and then you only need to duplicate one line of code.
Example
Master Page
<html>
<body>
<asp:ContentPlaceHolder ID="Foo" runat="server">
</asp:ContentPlaceHolder>
<!-- page content -->
<asp:ContentPlaceHolder ID="Bar" runat="server">
</asp:ContentPlaceHolder>
</body>
</html>
Content Page
<asp:Content ID="Top" ContentPlaceHolderID="Foo" runat="server">
<uc1:Page1Control id="page1Control1" runat="server" />
</asp:Content>
<asp:Content ID="Bottom" ContentPlaceHolderID="Bar" runat="server">
<uc1:Page1Control id="page1Control2" runat="server" />
</asp:Content>
You can create a COMMON user control for the same and add it to your master page in the top & bottom. You can create a property describing whether the control has loaded at the top or bottom.
Depending on the content you may be able to do this in code. If its just HTML just put it in a variable and assign it to the other. If you have controls, you can loop over the controls collection and create a duplicate control and place it in the other area. In either case the second location would not be a contentplaceholder a div should due.

Resources