get the iframe element in C# - c#

I'm trying to get the id of the second iframe which is iframe2 in the codebehind to add some css styling. This is my html:
<body>
<form id="Form1" method="post" runat="server">
<table id="Table1">
<tr>
<td>
<iframe id="iframe1">
</iframe>
<uc1 id="uc1"></uc1>
</td>
</tr>
<tr>
<td>
<asp:Panel id="Panel">
</asp:Panel>
<uc1 id="uc2">
</uc1>
</td>
<td>
<iframe id="iframe2">
</iframe>
</td>
</tr>
</table>
</form>
</body>
For some reason I'm not able to get the iframe id in C#. How can I do that please?

Accessing the Iframe
Ensure that you add the runat="server" attribute to the <iframe>:
<iframe id="iframe2" runat="server"></iframe>
This will enable to to access it in your code-behind via the FindControl() method:
// Access your iframe
var iframe = FindControl("iframe2");
iframe.Style.Add("border", "2px solid red");
Or directly by using its assigned id:
iframe2.Style.Add("border", "2px solid red");
Regarding Styling
You mention that you are explicitly trying to style this element. If that is the case, you should likely be using CSS as opposed to setting inline styles on the <iframe> element itself.
For instance, you could apply styles as such to target that element:
<style type='text/css'>
#iframe {
/* Place your styles here (i.e. border: 4px solid red, etc.) */
}
</style>
It's worth noting that actually styling an <iframe> is going to only style the element itself, as the content that is rendered within it is essentially "sandboxed" within the element and won't be affected by some of the assigned styles.

Change the IFrame to have runat="server" you can then get to it on the server side.
<iframe ID="MyIframe" src="" runat="server"></iframe>
You can set set items in C#.
MyIframe.Attributes["src"] = Page.ResolveClientUrl("~/ThisOtherPage.aspx");
MyIframe.Attributes.Add("style", "cursor:pointer;");

Related

How can I hide an HTML table row <tr> in aspx file and enable in code behind?

I have a very small search functionality and I have a table row called "Search Results", I want this table row get displayed whenever i have something to display from the search results. So I want to hide this row by default and enable via code behind when my search is fetching some result.
<div>
<table>
<tr id="srchResultHeader" style="display: none;" class="header">
<td colspan="2" class="tdlogintitle" visible="false">Search Results</td>
</tr>
<tr>
<td>/*Data to display actual result from database*/</td>
</tr>
</table>
</div>
I'm not able to get the reference of the above table id "srchResultHeader" in my code behind? What is wrong here in my code.
An id by itself is just a client-side identifier. In order for this to be referenced as a server-side object it needs to be a server-side control. The easiest way would just be to add runat="server" on the existing element:
<tr runat="server" id="srchResultHeader" style="display: none;" class="header" >
In this case you probably don't even need the style attribute, since you're controlling the hide/show functionality in server-side code. You can just set .Visible on the control to determine whether or not it renders to the client-side markup at all.
You could use server-side <asp:Table> for this very purpose. Otherwise <tr> is a client-side thing and is not directly accessible in the server-side code. <asp:Table> will render <table> tag on the client-side, but you can access it in the code-behind through its ID. The structure looks like this:
<asp:Table ID="MyTable" runat="server">
<asp:TableRow runat="server" ID="MyRow1">
<asp:TableCell>Some value</asp:TableCell>
</asp:TableRow>
</asp:Table>
You can now write something like this in the code-behind:
MyRow1.Visible = False;
.aspx
<tr id="divDriverName1" runat="server" >
<td >
<label class=" ">label1 </label>
<asp:TextBox ID="TextBox1" runat="server" class=" form-control"></asp:TextBox>
</td>
</tr>
.aspx.cs
ContentPlaceHolder myPlaceHolder = (ContentPlaceHolder)Master.FindControl("ContentPlaceHolder1");
HtmlTableRow ct = (myPlaceHolder.FindControl("divDriverName1")) as HtmlTableRow;
divDriverName1.Attributes.Add("style", "display:none");
<div class="row" id="divhiddenInfo2" runat="server" style="display: none">
</div>

Assigning CSS to dynamically created Label in C#

I'm trying to figure out how to apply CSS to a Label created in C#. Everything compiles and runs, it just doesn't seem to be applying the CSS. The CSS is in the file linked to in the site master page. Everything else in the CSS file is being applied as it should be.
Codebehind:
...
Label label = new Label();
SqlCommand command = new SqlCommand("SELECT Q_Text FROM HRA.dbo.Questions WHERE QID = 1");
command.Connection = connection;
reader = command.ExecuteReader();
reader.Read();
label.Text = reader["Q_Text"].ToString();
label.ID = "rblabel";
label.CssClass = "rblabel";
reader.Close();
holder.Controls.Add(label);
...
ASP:
<asp:Content runat="server" ID="BodyContent" ContentPlaceHolderID="MainContent">
<asp:PlaceHolder ID="holder" runat="server">
</asp:PlaceHolder>
</asp:Content>
CSS:
.rblabel {
text-align:left;
padding-left: 2em;
font-size: 4em;
}
EDIT: added the HTML as well as the Control.Add() statement in my code (forgot to include that in my copy/pase). This HTML is the etirety of what is put into that PlaceHolder.
HTML:
<section class="content-wrapper main-content clear-fix">
<span id="MainContent_rblabel" class="rblabel">TEST TEST TEST</span>
<table id="MainContent_ctl00" class="radio">
<tr>
<td><input id="MainContent_ctl00_0" type="radio" name="ctl00$MainContent$ctl00"value="1" />
<label for="MainContent_ctl00_0">Excellent</label></td>
</tr><tr>
<td><input id="MainContent_ctl00_1" type="radio" name="ctl00$MainContent$ctl00" value="1" />
<label for="MainContent_ctl00_1">Good</label></td>
</tr><tr>
<td><input id="MainContent_ctl00_2" type="radio" name="ctl00$MainContent$ctl00"value="1" />
<label for="MainContent_ctl00_2">Fair</label></td>
</tr><tr>
<td><input id="MainContent_ctl00_3" type="radio" name="ctl00$MainContent$ctl00"value="1" />
<label for="MainContent_ctl00_3">Poor</label></td>
</tr>
</table>
</section>
There are basically two possibilities:
Your label may not be rendering with the CLASS attribute set to the desired CSS class. That you can verify by looking at the rendered HTML. (If the label doesn't appear at all, your code suggests you may not be adding it to the Controls collection of its parent.)
Everything is rendering right, but it's not appearing correctly in your browser, due to some problem with your CSS or with another stylesheet overriding it. To debug this, you will need to use Firebug or IE's developer tools.
Good luck!
CssClass property is the proper way to go http://www.w3schools.com/aspnet/prop_webcontrol_style_cssclass.asp
You left out the code where you're adding the label to the page/container etc, which may be where an error lies.
Showing an HTML source output of the page to see if the label your generating actually contains a CLASS attribute with a value of "rblabel" would also be helpful.
If worse comes to worse use a Literal instead of a Label fill in the HTML.
It doesn't look like you are actually adding the label where you want it to appear. Try something like:
holder.Controls.Add(label);
in your code-behind.
A span is an inline element, so padding, width, and height aren't always honored in all browsers. Try setting it to inline-block.
.rblabel {
text-align:left;
padding-left: 2em;
font-size: 4em;
display: inline-block;
}

How to access to code viewable only in browser's "View source"?

Ok, this is weird. I am working on a GIS web site, and i have the next problem. I need to tweak one of the main functions in the site (because i constantly get an error about one of its lines); the problem is that i can't see that chunk of code in any way! I know where it is, because when i open the page with "View Source" browser's option (Chrome), i see the code i need to change, but that is the only way i can see it! I tried Dreamweaver, Notepad++, Visual Studio 2008 (because it's an .aspx page), still nothing. Here is the preview:
<body>
<form id="form1" runat="server">
<div align="center">
<asp:PlaceHolder ID="phScript" runat="server"></asp:PlaceHolder>
<hr />
<br />
<table style="width: 50%;">
<tr>
<td style="font-family: 'Times New Roman', Times, serif; font-size: large;font-weight: bold;">
<asp:Label ID="lblTitol" runat="server" Text="CÀLCUL ÀREES D'INFLUENCIA AL VOLTAN D'UNA ENTITAT"></asp:Label>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
And this is that portion of code as seen in browser's source:
<body>
<form name="form1" method="post" action="zi.aspx" id="form1">
<div align="center">
<SCRIPT language="javascript" type="text/javascript">parent.frames['map'].Oper='4';parent.frames['map'].MetodeTreball='ILLES';parent.frames['map'].Iter='1';parent.frames['map'].Entitats='CEIP';parent.frames['map'].RadiFix='400';parent.frames['map'].Cobertura='100';parent.frames['map'].ReloadMap(1200);</SCRIPT>
<br />
<br />
<br />
<hr />
<br />
<table style="width: 50%;">
<tr>
<td style="font-family: 'Times New Roman', Times, serif; font-size: large; font-weight: bold;">
<span id="lblTitol">CÀLCUL ÀREES D'INFLUENCIA AL VOLTAN D'UNA ENTITAT</span>
</td>
</tr>
<tr>
<td>
</td>
</tr>
</table>
It is the JavaScript part with parent.frames bla bla part that i have to change, if i can. As i can see, that portion of the code is in aspPlaceHolder, is there any way i can access that part and change it? Sorry for this much code, but i tried to explain the problem as closely as i can.
Remember, the source code you are seeing in the first part is the source code of the ASPX file. This will be parsed by the ASP.Net runtime and the HTML sent to the client. So what you want to do is look at the code that is updating the asp:PlaceHolder called phScript.
If you have access to the source code, this will be in the code behind for the ASPX page - it will have the same name but the extension .cs.
This should tell you where and how the javascript is being constructed, at least.
Search for something like phScript.Controls / phScript.Controls.Add in the code-behind file of that aspx file.
Search the project for the term phScript to see what's populating the phScript PlaceHolder.
In visual stdio 2010 when ever a page is rendered to client it is also shown in Solution explorer at the top. This is same code which is viewable in View source of browser.

Refresh iframe when refreshing page

i have web page it has a page header content like main menu,logo and so..on..
and in iframe i am loading the side menu and content. while loading the web page i am using an empty home page to display. after some time in mid of some page while refresing the page it reloads the home page. But i need to display the page that i am using as a current page
code is
<form id="form1" runat="server">
<div>
<table>
<tr>
<td style="vertical-align: top; width: 100%" height="115px">
<div>
<uc1:MainMenu ID="ucMainMenu" runat="server" />
</div>
</td>
</tr>
<tr>
<td>
<iframe src="HomePage.aspx" runat="server" width="1250px" height="675px" id="ifSideMenu"
frameborder="0"></iframe>
</td>
</tr>
</table>
</div>
</form>
in that iframe deafult page is Homepage.aspx,it is empty page. When i clicked the sub menu it loads the content like AddMaster.aspx. When i refresh the page it loads Homepage.aspx, but i need to display AddMaster.aspx
help me
this code will help u to refresh the iframe only.. not the whole web page. but., i dont understand your que clearly brother.
<IFRAME id="frame1" src="http://www.google.com/" runat="server">
<script type="text/javascript">
function refreshiframe()
{
parent.frame1.location.href="http://www.google.com/"
setTimeout("refreshiframe()",3000);
}
</script>
<body onload="refreshiframe();">
I finally found the answer for this question.
It may help for others.
Add the below code in HomePage.aspx and other pages that are going to load in the iframe
Session["CurrentPage"] = Request.RawUrl.ToString().Replace(Request.ApplicationPath.ToString() + #"/", "");
in Main Page in page load event.
if (Session["CurrentPage"] == null)
Session["CurrentPage"] = "Exit_Tracker.aspx";
ifSideMenu.Attributes["src"] = Session["CurrentPage"].ToString();

Positioning the starting position of Horizontal Scrollbar slider on the right

I want to Set the Horizontal Scrollbar slider to the right without using css direction:"ltr" or dir="ltr" or an asp:Panel direction="rightToLeft"....
i just want to access the object that controls the Horizontal scrollbar slider to give it the position.
from aspx page or the aspx.cs.
aspx page :
<script type="text/javascript" src="../js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="../js/BeginScrollFromRight.js"></script> . . .
<body id="body" style="overflow:auto; height:100%;width:100%;"> . . . </body>
js page :
function BeginScrollFromRight()
{
$("#body").scrollLeft($(window).width());
}
I need to have the same effect for the direction:rtl but only for the horizontal scrollbar because other object are not supported when using direction:rtl
<td id="tdView" runat="server" dir="ltr" align="center">
<table onclick="hideMenusComplex();" oncontextmenu="hideMenusComplex();" id="tblView" runat="server">
<tr>
<td>
<asp:Label ID="lblView" runat="server" ForeColor="#5E82D6" Visible="false"><%= translate("View : ") %></asp:Label>
</td>
<td>
</td>
<td>
<cc1:Combobox AlignContainer="Center" ID="ddlViews" runat="server" OnClientChange="onChangeValue()"
FolderStyle="../EsStyles/ComboXpBlue" AutoPostbackEnable="false" Width ="200">
</cc1:Combobox>
</td>
<td>
<asp:Label ID="lblViewArabic" runat="server" ForeColor="#5E82D6" Visible="false"><%= translate("View : ") %></asp:Label>
</td>
</tr>
</table>
</td>
This is the code and the ddlViews is the dropdown list that is opening in a wrong way, the dropdown list is not opening under the control is opening on the left of the control.
For languages the read right-to-left, direction: rtl is really the only way that will work well, in the end.
If you just want to scroll all the way to the right, jQuery JavaScript like:
$("#YourContentDiv").scrollLeft($("#YourContentDiv").width());
or:
$(window).scrollLeft($(window).width());
will do it.
.
In case you are new to jQuery, you can add it to your page, like this.:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js" type="text/javascript"></script>
<script type="text/javascript">
function jQueryMain ()
{
$("#YourContentDiv").scrollLeft($("#YourContentDiv").width());
$(window).scrollLeft($(window).width());
}
$(document).ready (jQueryMain);
</script>
.
PS: jQuery takes most of the cross-browser hassle out of JavaScript like this.

Categories

Resources