I have an Html image which I need to update dynamically but without runat="server" my code behind cannot see it.
<img name="MDI" id="detailimg" src="" alt="blankimage.jpg" />
I can access it using Page.Request.Form["MDI"]; but not update it
I need to load the src image at run time so can someone please tell me how I can update the control in my code.
Do with inline syntax like below
<img name="MDI" id="detailimg" src="<%= getImageSource() %>" alt="blankimage.jpg" />
In codebehind
public string getImageSource()
{
return "urlpath/img.jpeg";
}
More about Inline Syntax
If you want to update via code behind, you have to set runat="server"
If inline is fine, use src="<%= something%>
Otherwise use javascript for example
as you know if you want to access an element/control from code behind,you should set runat="server" but there is 2 other ways to set properties from code behind
use inline syntax as other guys shows (<%= YourVoidToGetImgSrc %>
use Ajax and get value from code behind in your html page.
Related
<td>
<a runat="server" href="~/url.aspx">
<img src="<%= ResolveClientUrl("~/images/image1") %>" id="submissions"
border="0" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"></a></td>
When I try to run this code with runat="server" added to my img tag, I get a Parser Error that says "Server tags cannot contain <%...%> constructs." The C# code I tried in my code behind's Page_Load is:
if (Request.Url.AbsoluteUri.Contains("submissions"))
submissions.Attributes["src"] = "~/images/image3";
The goal is to highlight the part of the navigation bar that corresponds to the page the user is already visiting. The problem is that it doesn't allow me to access the img tag's src attribute.
Try
<img src='<%= ResolveClientUrl("~/images/image1") %>' id="submissions" ...
Pay attention to ' not "
If you need access to this tag from CodeBehind - the easiest way is to replace <img/> to <asp:Image runat="server" id="submissions"/> and in Master codebehind access by ID, but in child page - using
Image submissions = (Image)this.Master.FindControl("submissions");
Pay attention to right escaping here:
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl("~/images/image2") %>',1)"
onmouseover="MM_swapImage('submissions','','<%= ResolveClientUrl(\'~/images/image2\') %>',1)"
I have a asp label that I need to be able to change according to the code behind. How can I do this?
ASPX: (The first part works correctly only for "TestA#abc.com" and the second part dynamically changes the label (EmailLabel) according to the "if" statement in the code behind. How can I integrate these two so the label is mailto? Thanks.
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:Label ID="EmailLabel" runat="server"></asp:Label>.</p>
Code Behind:
public changeLabel()
{
if (//Some Condition Here)
{
this.EmailLabel.Text = "TestA#abc.com";
}
else
{
this.EmailLabel.Text = "TestB#abc.com";
}
}
What you are trying to do there won't work. Label's render out as <span> tags, so it will never be "clickable". You want to do something more like this:
<p>Email at TestA#abc.com.</p>
<p>Email at <asp:LinkButton ID="EmailLabel" runat="server"></asp:LinkButton>.</p>
And then instead of changing the Text property, change the NavigateUrl property.
You could also use an HtmlControl, which is basically a standard HTML tag that you add the runat="server" attribute to.
<p>Email at <a id="EmailLabel" runat="server" href=""></a>.</p>
You would then be able to modify this <a> tag via server side code, the properties will be slightly different, but now you've got a real live anchor tag to work with.
This other SO question might also be helpful: How to launching email client on LinkButton click event?
<html>
<body>
<span id="foo"
onclick="document.getElementById('foo').innerHTML = 'never say never'"
>
Click Me!
</span>
</body>
</html>
For your Label, just remember that .Text is the server equivalent of .innerHTML so you can put whatever HTML you want right into the asp:Label when setting .Text. Just watch out for cross-site-scripting exploits.
Right, please don't point me there:
Disable a HyperLink from code behind
My problem is as follows.
I have a hyperlink on my aspx page:
<asp:HyperLink Visible='<%= _myUser.hasPermission("Intranet Management")%>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
_myUser.hasPermission("Intranet Management") returns boolean with value of TRUE or FALSE depends if a current user has that permission or not. _myUser is declared in aspx.cs file as protected member so I am able to access it from aspx file.
On my page I am getting following error:
Parser Error Message: Cannot create an object of type 'System.Boolean'
from its string representation '<%= _myUser.hasPermission("Intranet
Management") %>' for the 'Visible' property.
Is there any other way of doing that in aspx file? Please don't ask me to do it in the code behind, I have my reasons to do it here...
Thanks for any help.
The problem you're facing is that asp:Hyperlink is a server control, and these wont evaluate code inside <%= %> for their properties. They will databind though IIRC, so you could try
<asp:HyperLink Visible='<%# _myUser.hasPermission("Intranet Management")%>'...
And make sure to call Page.DataBind().
In order to do it this way, you cannot have runat="server". The idea is that server-side controls will be modified using code behind.
If you don't want to use code behind, use a regular <a> tag without runat="server". There doesn't seem to really be any reason why you need a server control here anyway.
use the following instead:
<%# _myUser.hasPermission("Intranet Management") %>
Got it from here
This should work ...
<asp:HyperLink ID="HyperLink1" Visible='<%# Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) %>' Text="Intranet Management" runat="server" NavigateUrl="/Apps/Admin/Default.aspx" />
Perhaps this is better after all
<a style='<%= Convert.ToBoolean(_myUser.hasPermission("Intranet Management")) ? "" : "display:none;" %>' href="Apps/Admin/Default.aspx"> Intranet Management </a>
I need to initialize the text attribute of the text box element with a property from some where else when actually I can simply do this from code but it will be much more convenient if it possible to do it like this:
<asp:TextBox runat="server" Text="<%= new ContextItem("title").Value %>" />
Unfortunately the above can't be done..
The issue is that this text box element repeats it self several times in the page and my question is:
Are there any suggestions how to make it cleaner then to write it again and again in the code behind?
Thank,
Adler
OK so the basic problem here is that if you use an inline expression you can NOT use it to set a property of a server-side control outside of a binding context (using a binding expression). I have inferred that this is probably because of the timing of the evaluation of these inline expressions. You can, however, render client-side markup in this way. If you want to keep the functionality purely in your aspx file, this is the way to do it.
Edit: Based on input from Justin Keyes, it appears it IS possible to use a binding expression to set the property. You need to manually invoke Page.DataBind() to trigger the textbox to evaluate the expression (see answer below).
For instance this:
<asp:Label ID="lbl" runat="server" Text="<%= Now.ToShortDateString() %>" />
Will produce this output:
<%= Now.ToShortDateString() %>
On the other hand this:
<%= "<span>" & Now.ToShortDateString() & "</span>"%>
Will produce this output:
7/27/2011
The "normal" way to solve this problem is just to set the Label.Text properties in a Page.Load event handler or another appropriate event handler depending on your needs, as below. This is the way I believe most people would prefer to do it, and is most easily understandable in my opinion.
Markup:
<asp:Label ID="lbl" runat="server" />
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
lbl.Text = Now.ToShortDateString()
End Sub
Option 1: don't use server controls
If you aren't accessing the value on the server, just use plain HTML instead of an ASP.NET server control:
<input ID="Textbox1" Type="Text"
Value='<%= new ContextItem("title").Value %>' />
Option 2: use Page.DataBind()
If you change your code to use <%# instead of <%= (as below) and call Page.DataBind(), it will work (I've tested it). Change your markup to this:
<asp:TextBox runat="server" Text="<%# new ContextItem("title").Value %>" />
And in your logic, call Page.DataBind() in the Load event like this:
protected void Page_Load(object sender, EventArgs e) {
Page.DataBind();
}
Even though the TextBox is not contained in a typical "data bound" control such as a Repeater or GridView, calling DataBind() on a control will force it to evaluate <%# ... %> statements.
The Moof's comment (below) is correct. This post also mentions Page.DataBind().
You can set the text on a page in a similar way.
<asp:TextBox id="TextBox1" runat="server" Text='<%#GetValue('Title)%>' />
But in order for this to work, you will need to DataBind the control on Page_Load. For multiple TextBox controls you could just loop through each and databind them so that you do not have to hard code the databinding of each.
I am not sure what your ContextItem is though, so you would have to modify my code.
The short answer is NO, you can only use this kind of code with databindings, that means inside a GridView for example. But you can use this in the head section.
I use it to prefix my urls sometimes with something predefined. Example
<script src="<%=Utils.GetGeneralPrefix()%>/Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
In that case it works.
Hope it helps.
90% of the time when I try this I have to use single quotes ('') instead of double quotes ("") around the <%%>. Give that a try before you spend too much time on anything else.
I am using a button that has to be invible and should be used by a javascript function.
<asp:Button ID ="btnDummy1" runat="server" Visible ="true" OnClick="btnSubmit1_Click" width="0px" height="0px"/
I cannot keep visible = false as it the javascript will not use invible content in the poage. I havetried to give width=0 and height=0, still it showws up in Chrome. What do you guys think i should do?
Thanks in advance :)
A pretty clean approach in ASP.Net it give it a "hidden" class:
<asp:Button ID ="btnDummy1" runat="server" CssClass="hidden" />
Then in your stylesheet:
.hidden { display: none; }
If you set it to Visible="False" then the code will not be executed.
Instead I think you should wrap it in a <div> and set display:none via css:
<div style="display: none;">
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" />
</div>
adding style="display:none" would hide the button till you make it visible again
<asp:Button ID ="btnDummy1" runat="server" OnClick="btnSubmit1_Click" style="display:none"/>
How about
style="display:none"
for the button instead of Visible = "true".
Can you just use a hidden form field in this case? This is generally the preferred method of passing information along.
See http://www.tizag.com/htmlT/htmlhidden.php
<asp:Button ID="btnMyOrders" runat="server" Text="my orders" CssClass="dropdown-item" OnClick="btnMyOrders_Click" Visible="False" />
Example then in Form(){
btn.Visible = true; to show it again
}
I think the first question you should ask yourself is : why would I put in my HTML doc a button that should not be visible ?
An HTML document should be used ONLY TO DESCRIBE A CONTENT'S SEMANTICS. So an HTML doc should ONLY contain the content you want to publish and extra data to explain the content's SEMANTIC !! NOTHING about the way it is displayed, NOTHING about the way it behaves !!
All displaying and behaviors questions should be managed with CSS and Javascript, NEVER within HTML itself.
Any element needed for Javascript only purpose should be added in the doc by Javascript itself !
You should never find, in an HTML doc, such things as prev/next buttons for a javascript picture gallery for example. The HTML doc should only contain the pictures list, than your JS script will change the way this list is shown, making it a eye candy gallery and add buttons for navigation.
So in your example, your saying you want to add in your HTML doc an invisible button with some Javascript actions on it.... that's probably an example of some content that should never be in the HTML doc.