ASP.NET Img tag - asp.net

I have a problem with how ASP.Net generates the img tag.
I have a server control like this:
<asp:Image runat="server" ID="someWarning" ImageUrl="~/images/warning.gif" AlternateText="Warning" />
I expect it to generate this:
<img id="ctl00_ContentPlaceHolder1_ctl00_someWarning" src="../images/warning.gif" />
but instead it generates this:
<img alt="" src="/Image.ashx;img=%2fimages%2fwarning.gif"</img>
This give me errors when I execute the following js:
document.getElementById('ctl00_ContentPlaceHolder1_someWarning')
Any idea why it won't generate the expected html?

Looks like it's trying to use a custom handler (ashx) to deliver the image. Do you have any additional modules that may be overriding the default behaviour of the asp:Image?
Your JavaScript won't work because the image tag has not been given an ID in the HTML that was generated.

You can get the actual ID that is generated by using ClientID. I use this to get the ID of a control for use in JavaScript using syntax similar to the following:
document.getElementById('<%=ddlCountry.ClientID%>').style.display = "block";
However you can also use it in your code-behind to get the same thing.

Related

how to get attribute value of div in asp.net

I have a div like this:
<div id="1" class="2" no="3" type="4"></div>
i want get property no and type in ASP.net.
please help me.
Add the runat="server" attribute to the div and access the other attributes in code-behind like this: string attr = divId.Attributes["attrName"];
If you are adding the attributes with javaScript, make sure to run this code after the javaScrip has added them. Otherwise you might get a null refference exception.
add tag runat="server" and then in c# divid.yourproperty

Printing a string from a resource file outside of an asp control

I have a resource file in the App_GlobalResources folder and i would like to print some of the string in things like the alt text of images and the title of a href links. I know this can be done using asp.net controls for asp:Image and asp:Hyperlink then print the striong with the <%$ but what I would like to do is use the normal HTML a href and img tags and then print the resource string in that. Can anyone tell me how to accomplish that?
yes , using
<img src="..." runat="server" id="myUniqueID" />
can be accessed in code-behind just like <asp:Image /> would be
most HTML elements with runat attribute and a unique id can be accessed from the viewstate just like an asp: element would be
Use CSS Media Selectors to distinguish between screen CSS and print CSS.
An example for hyperlinks is given in this article. This may not work for all browsers, though.

Javascript won't work on Html server control?

I am writing a website with ASP.Net.
I will have lots of html generic controls like <div> <span> and so on..
I have some onclick javascript functions, onmouseover javascript functions..
They are working fine..
Then I need to control them on the server side.
So, I add runat="server"..
After that, all the javascripts aren't working anymore..
I understand they aren't working coz all the events are now going back to server side.
So, is there anyway to make them work??
For eg,
<div id="myDiv1" onclick="myfunction(para1)"><img src="..." /></div>
the above code is working..
<div id="myDiv1" runat="server" onclick="myfunction(para1)"><img src="..." /></div>
the above code is not working...
I can make it work, probably by
<div id="externalDiv1" onclick="myfunction(para1)"><div id="myDiv1" runat="server" ><img src="..." /></div></div>
Is there any other way?
I assume that you used document.getElementById() to get an element by its id. If you are using master pages, the IDs of server controls will be changed after rendering to the page, in that case, you have to use its ClientID
for e.g.
var myDiv1 = document.getElementById("<%= myDiv1.ClientID %>");
Server-side or client-side controls makes no difference as far as javascript is concerned. ALL server-side controls end up being rendered as normal HTML controls. If your javascript functions are not working might be because you are accessing them by the wrong id since by making them server-side controls they can now have ids that follow a pattern like <parent_id>_<control_id>.
For example, a span element declared like this:
<span id="mylabel" runat="server"> testing</span>
may end up being rendered as:
<span id="MainContent_mylabel"> testing</span>
ASP.NET 4.0 has a feature called CliendIDMode which can be set to static, meaning, that your ids on the markup will stay unchanged after the page is rendered.

How to control usercontrol from javascript

I have an usercontrol with an attribute targetUrl. I add this user control to a page and write targetUrl attribute from this page like below:
<PBG:Modal ID="Modal1"
runat="server"
Height="180"
Width="500"
src="pop_adres_giris.aspx"/>
This worked properly, but I want to change the targetUrl attribute from javascript. And I can't do it. I write code like below, but it didn't work.
var frm = document.getElementById('Modal1');
frm.targetUrl = 'pop_adres_giris.aspx';
How can I do it?
The UserControl object, which generates HTML on the client side, are not accessible as the rich objects which are available when handling server side calls.
Depending what the UserControl is, you will need to use a different method to get it and set the "targetUrl".
In addition, to ease your accessing of elements within the DOM you may want to consider using a library such as jQuery or prototype
Once you have declared your control, for instance, if you were using an asp:Hyperlink control:
<div id="hyperlink_holder">
<asp:Hyperlink ... NavigateUrl="http://someurl" />
</div>
You know that asp:Hyperlink generates html like <a href="http://someurl" ... />
So we can access the element and change the link like:
$('#hyperlink_holder a').attr("href", "http://newurl");
In addition, note that the ID you give an item in ASP.NET is not necessarily the ID which will render in the id element in the HTML; it is instead a concatenation of a number of ids; therefore use selectors based on non runat="server" controls where possible, or pass the ClientID of the UserControl through to the client to use for selection if absolutely necessary.

RegisterClientScriptBlock without form tag

After trying to understand why client code is not rendered in a page (injected by user control) I found this link, it turns out you must have a form tag for it to work (Page.RegisterClientScriptBlock did declare this but ClientScriptManager.RegisterClientScriptBlock which I use does not say anything regarding this).
I am using Visual studio 2005.
Does anyone know if this has been solved?
Edit:
To clarify, I want my control to add javascript code to the head section of the page without having to use the
<form runat="server"
I have tried adding it using:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = "alert('123');";
Page.Header.Controls.Add(x);
But this did not work for me.
As far as I know this functions the same in current versions, you can test it very simply though.
Update
per discussion in the comments, the only "workaround" that I could think of would be for your to manually insert the script into the "head" section of the page on your own, using a runat="server" declaration on the Head element.
Got it!
My mistake was not doing it in the OnPreRender method (I used the Render method).
Now all that is needed is - like Mitchel Sellers wrote, set the header to runat server and than add to it's controls:
HtmlGenericControl x = new HtmlGenericControl("script");
x.InnerText = GetScriptSection();
Page.Header.Controls.Add(x);
Thanks for pointing me to the right direction!
The MSDN Page for registerclientscriptblock here says:
The client-side script is emitted just
after the opening tag of the Page
object's <form runat= server> element.
The script block is emitted as the
object that renders the output is
defined, so you must include both tags
of the <script> element.
If you do not want to include a form, than you will basically need to build your own implementation of it.
Minor clarification for anyone seeing this:
The form tag must have the runat="server" attribute set, e.g.
<form id="theform" runat="server">
Just placing a regular HTML form tag in the page will not help.

Resources