I have a label and I want to add to it a link.
I want to use javascript like :
MyLabel.Attributes.Add("`onclick`", "javascript:`SOME_CODE`")
What must I add in (SOME_CODE) to redirect the user to another link.
Thanks.
Have you tried: window.location = 'http://google.com' ?
Are the any particular reason you want to use Javascript for this, and not just the HyperLink Control?
Update:
You can either use a normal a-tag link or use the ASP.Net HyperLink control:
This is the markup:
<asp:HyperLink ID="MyHyperLinkControl" NavigateUrl="http://google.com" runat="server" />
This is if you want to add it from the code-behind:
HyperLink link = new HyperLink();
link.NavigateUrl = "http://google.com";
parentControl.Controls.Add(link);
Where parentControl, is the container you want to add it to, for instance a cell in a table or a panel.
See here for more information on how to add a control to a panel
Just use a plain anchor tag (<a >), but put the label inside the anchor (the reverse is not strictly valid html). If you don't want it to show up as a link every time, you can accomplish that by omitting the href attribute. This is easy to do with a normal <asp:HyperLink> server control like so:
<asp:HyperLink id="..." runat="server"><asp:Label ... ></asp:Label></asp:HyperLink>
Now, the href attribute will only render if you actually set the NavigateUrl property in your code. You might also find that using an <asp:HyperLink> completely replaces the need for the label.
<a href="http://google.com" >Go to Google</a>
If this has anything to do with your previous question, use a Hyperlink control instead of a Label:
Dim Hyperlink1 As New Hyperlink
Hyperlink1.Text = "XYZ"
Hyperlink1.NavigateUrl = "http://www.google.com"
Dim Literal1 As New Literal
Literal1.Text = "<br />"
' Add the control to the placeholder
PlaceHolder1.Controls.Add(Hyperlink1)
PlaceHolder1.Controls.Add(Literal1)
Related
i am trying to create a hyperlink from code-behind, but it is not creating it where i want it to be.
if i look at my souce code is creating somewhere else and from .aspx page it seems like everything is in place where it needs to be.
.aspx
<div class="AZ">
<ol class="AZlist">
<asp:PlaceHolder runat="server" ID="AToZ"></asp:PlaceHolder>
</ol>
</div>
.codebehind
HyperLink links = new HyperLink();
links.Text = "<li>" + CheckMe.ToString() + "</li>";
links.NavigateUrl = "<a href='#'" + CheckMe.ToString() + ">";
ph.Controls.Add(links);
source code:
....
....
...
<div class="AZ">
<ol class="AZlist">
// I WANT HYPERLINK HERE....!!!!!!!!!!!
</ol>
<br />
</div>
</li>
but its creating outside the div area here
<li>A</li>
I dont think you should put those tags in the .text and .navigateurl properties. just put the link and the text in them. Put the <li> tags around the placeholder.
Ditch the way you're doing it here. Try making your ol tag runat="server" and give it an id. Then create a new ListItem control, add the hyperlink control, and add the ListItem to the ol.
Something like this in the codebehind:
dim anLi = new ListItem([can't remember exact parameters])
dim aHyperlink = new Hyperlink([whatever to initialize])
anLi.addControl(aHyperlink)
theOl.controls.add(anLi)
Why not use an asp:repeater instead.
You could then put all your Mark up in the aspx template making use of header / detail / footer templating. Next just get the text and URL values into a bindable format in the code behind and data bind.
Saves doing markup in code behind and the need to dynamically insert controls which can cause headaches on post backs.
I have a hyper link control and I set the NavigateURL and the ImageURL property at runtime. I also need to set the class of the image tag that it generates but I cannot figure out how I can do that. The solution mentioned here
Apply CSS Class to Image in asp:Hyperlink?
does not work because the image url is hard coded.
any ideas?
You should still be able to use that solution and just dynamically assign the image:
<asp:HyperLink runat="server" CssClass="linkclass" NavigateUrl="http://example.com">
<asp:Image runat="server" Id="ImageLink" CssClass="imgClass" ImageUrl="paceholder.jpg" />
</asp:HyperLink>
Then in code behind you can easily set:
ImageLink.ImageUrl = "MyDynamicImage.jpg";
I'm not sure if there is a way to set the CssClass directly like this, but a workaround is to dynamically create an Image, and add it to the HyperLink's control collection, like so:
Image _img = new Image();
_img. ImageUrl = "image.jpg";
_img.CssClass = "myClass";
HyperLink1.Controls.Add(_img);
I am looking for the best solution for my problem. I am currently using a asp.net gridview with a link in the first column and the id to the item it relates too.
Details href=.../page.aspx?ID=25
I have 3 hidden fields that I would like to bring with me to the next page.
I want the url to look something like this. Details href=.../page.aspx?ID=25&HDN1=3&HDN2=5&HDN3=76
I am able to set the values of the hidden text. I need to retrieve the values and add them to the url in the Details link.(onclick?). What is the best way to get this done??
If these hidden fields are part of the databinding source collection then you can pass multiple parameters to a HyperLinkField GridView column. eg:
<asp:HyperLinkField DataNavigateUrlFields="id, field1, field2" DataNavigateUrlFormatString="/page.aspx?id={0}&hdn1={1}&hdn3={2}" />
Edit:
OK if it's client side, then you will have to do this via javascript.
I would add an onclick handler to each of your links:
<a href="details.aspx?id=123" onclick="detailsHandler(this.href); return false;" />
then a javascript function to handle the redirect:
function detailsHandler(href) {
var hiddenField = document.getElementById('eleID').value;
//get any other hidden fields and append them.
href = href + "&hnd1=" + hiddenfield;
//then redirect to the revised url
window.location = href;
}
I have a variable with this value
var= "http://www.coolsite.com"
this variable will change, its a dynamic value
i want insert this variable in my href attribute of tag.
after insertion it should look like this
http://www.coolsite.com
i want to do this in asp.net c#
does any one have an idea, how can i do this?
Thanks
In the markup, this can be achieved with the following:
<asp:HyperLink ID="HyperLink1" NavigateUrl="http://www.coolsite.com" runat="server">http://www.coolsite.com</asp:HyperLink>
Notice the NavigateUrl attribute. This is the URL that will be placed inside of the href. The inner text is the text that is rendered to the client. Knowing this, you can achieve the same results with this code in your code behind:
string yourUrl = "http://www.coolsite.com";
this.HyperLink1.NavigateUrl = yourUrl;
this.HyperLink1.Text = yourUrl;
If you variable is on the server side use an asp:Hyperlink instead and set it when the value gets changed.
If the var="http://www.coolsite.com" is a javascript variable. you could use javascript to modify the href attribute of a anchor tag. If you use jQuery you can use the attr method.
$('Atag').attr({href:"http://www.coolsite.com"});
If you use plain js code, you can use:
co = document.getelementById('AtagID');
co.setAttribute('href',URL);
<a id="theLink">http://www.coolsite.com</a>
so to code in jquery:
<script src="jquery.js"></script>
<script>
$(document).ready(function() {
$("#theLink").attr("href", "http://www.coolsite.com");
});
</script>
What is the best way to access an ASP.NET HiddenField control that is embedded in an ASP.NET PlaceHolder control through JavaScript? The Visible attribute is set to false in the initial page load and can changed via an AJAX callback.
Here is my current source code:
<script language="javascript" type="text/javascript">
function AccessMyHiddenField()
{
var HiddenValue = document.getElementById("<%= MyHiddenField.ClientID %>").value;
//do my thing thing.....
}
</script>
<asp:PlaceHolder ID="MyPlaceHolder" runat="server" Visible="false">
<asp:HiddenField ID="MyHiddenField" runat="server" />
</asp:PlaceHolder>
EDIT: How do I set the style for a div tag in the ascx code behind in C#? This is the description from the code behind: CssStyleCollection HtmlControl.Style
UPDATE: I replaced the asp:hiddenfield with an asp:label and I am getting an "undefined" when I display the HiddenValue variable in a alert box. How would I resolve this.
UPDATE 2: I went ahead and refactored the code, I replaced the hidden field control with a text box control and set the style to "display: none;". I also removed the JavaScript function (it was used by a CustomValidator control) and replaced it with a RequiredFieldValidator control.
My understanding is if you set controls.Visible = false during initial page load, it doesn't get rendered in the client response.
My suggestion to solve your problem is
Don't use placeholder, judging from the scenario, you don't really need a placeholder, unless you need to dynamically add controls on the server side. Use div, without runat=server. You can always controls the visiblity of that div using css.
If you need to add controls dynamically later, use placeholder, but don't set visible = false. Placeholder won't have any display anyway, Set the visibility of that placeholder using css. Here's how to do it programmactically :
placeholderId.Attributes["style"] = "display:none";
Anyway, as other have stated, your problems occurs because once you set control.visible = false, it doesn't get rendered in the client response.
If the Visibility is set to false server-side, the placeholder won't be rendered and you won't be able to access anything inside it from JavaScript. Your code should work when the placeholder is visible="true"
Get rid of the placeholder, leave the hidden field empty at first, after the search populate it.
Try this:
function popup(lid)
{
var linkid=lid.id.toString();
var lengthid=linkid.length-25;
var idh=linkid.substring(0,parseInt(lengthid));
var hid=idh+"hiddenfield1";
var gv = document.getElementById("<%=GridViewComplaints.ClientID %>");
var gvRowCount = gv.rows.length;
var rwIndex = 1;
var username=gv.rows[rwIndex].cells[1].childNodes[1].innerHTML;
var prdid=gv.rows[rwIndex].cells[3].childNodes[1].innerHTML;
var msg=document.getElementById(hid.toString()).value;
alert(msg);
document.getElementById('<%= Labelcmpnme.ClientID %>').innerHTML=username;
document.getElementById('<%= Labelprdid.ClientID %>').innerHTML=prdid;
document.getElementById('<%= TextBoxviewmessage.ClientID %>').value=msg;
return false;
}
<ItemTemplate>
<asp:LinkButton ID="LabelComplaintdisplayitem" runat ="server" Text='<%#Eval("ComplaintDisp").ToString().Length>5?Eval("ComplaintDisp").ToString().Substring(0,5)+"....":Eval("ComplaintDisp") %>' CommandName ="viewmessage" CommandArgument ='<%#Eval("username")+";"+Eval("productId")+";"+Eval("ComplaintDisp") %>' class='basic' OnClientClick =" return popup(this)"></asp:LinkButton>
<asp:HiddenField ID="hiddenfield1" runat ="server" Value='<%#Eval("ComplaintDisp")%>'/>
</ItemTemplate>
If the place holder visibility is set to false, it will never be rendered , and the hidden field value will be only stored in the ViewState of the page.
just one question, why are you setting the visibility of the place holder to be false , if its containing a hidden field?
Anyway one possible way to get over this issue, is adding a TextBox or Label object , and set the display CSS style of it to "none" , then in your code copy whatever you are putting in the hidden field into the textbox/lable text property, this way you can easily read the value using javascript , since the textbox/label will be rendered but not visible to others, though this might not be that safe thing to do.
Instead of making ".visible=false", change the style to "display: none;". That will render your control but make it invisible.
Visible doesn't actually make it visible, you can leave it default. Just runat="server" and use its .Value.