I have written a project in C# in which I load a HTML webpage if an event occurs during teh course of the project usage.
My issue is that inside my html page, I have a href node inside a tag as such:
<a href="http://pseudo01.hddn.com/vod/demo.flowplayervod/flowplayer-700.flv"
style="display:block;width:inherit;height:inherit;background-color: black;overflow:hidden"
id="player">
</a>
I would like to change the href value programatically by sending a C# variable (called myHrefFile) depending on what the user did.
How can this be done?
you can do something like this
<a href="<%= someMethodThatGetHrefValue() %>"
style="display:block; width:inherit;height:inherit; background-color:black;
overflow:hidden"
id="player">click</a>
call the method that decide what should be href for link .
and put all the logic of deciding href in that method like this
public string someMethodThatGetHrefValue()
{
if(someval == true)
return "http://www.google.com";
else
return "http://www.yahoo.com";
}
Something like this perhaps? This is an Asp.net solution.
<asp:LinkButton ID="LinkBut" runat="server" onclick="LinkBut_Click">Click here</asp:LinkButton></p>
protected void LinkBut_Click(object sender, eventArgs e)
{
string myHrefFile = "";
if(Value.equals(true))
{
myHrefFile = "page1.aspx";
}
else
{
myHrefFile = "page2.aspx";
}
Response.Redirect(myHrefFile );
}
Related
I have a website in which I need to have conditional navigation. As such:
Clicked item in Menu
1: if frontpage, go to #id
(example <a href="#events">)
2: if subpage, go to front- or other subpage
(example <a href="../events/>)
The website is a .net website and the navigation is located on the masterpage.
I have thought of different ways to solve this, either with .Net & C#, JS or JQuery.
In .Net I thought I could assign asp controls and simply serve different links in different subpages and then have some sort of loop or replacement with the codebehind.
If anyone has any idea how to figure out this, please write. Thanks in advance!
Code examples:
<li>
Coaching & Consulting
</li>
Why dont dont you create a user control with your menu?
You can check it out here:
http://msdn.microsoft.com/en-us/library/vstudio/fb3w5b53(v=vs.100).aspx
and pass some parameter to the user control and handle the link changing in there.
You can have that menu stored somewhere else, like in a xml file somewhere and render it to the desired html.
I followed Vfleitao's advice and tried User Controls. Just so others can see how I solved it here are some code examples:
The header user control:
<%# Control Language="C#" AutoEventWireup="true" CodeFile="header.ascx.cs" Inherits="menu" %>
<ul class="show-for-large-up">
<li>About us</li>
<li>Coaching</li>
<li>Consulting</li>
<li>Events</li>
<li>Contact</li>
</ul>
Codebehind:
private string aboutUsLink;
private string coachingLink;
private string consultingLink;
private string eventsLink;
private string contactLink;
public string AboutUsLink
{
get { return aboutUsLink; }
set { aboutUsLink = value; }
}
public string CoachingLink
{
get { return coachingLink; }
set { coachingLink = value; }
}
public string ConsultingLink
{
get { return consultingLink; }
set { consultingLink = value; }
}
public string EventsLink
{
get { return eventsLink; }
set { eventsLink = value; }
}
public string ContactLink
{
get { return contactLink; }
set { contactLink = value; }
}
Use in page:
<%# Register TagPrefix="My" TagName="header" Src="~/header.ascx" %> <%-- Maybe this could be moved to the master page? --%>
<My:header runat="server" ID="MyHeader" AboutUsLink="#OmOs" CoachingLink="#Coaching" ConsultingLink="#Consulting" EventsLink="#Events" ContactLink="#Contact" />
Or
<My:header runat="server" ID="MyHeader" AboutUsLink="../about/" CoachingLink="../coaching/" ConsultingLink="../consulting/" EventsLink="../events/" ContactLink="../contact/" />
Sources for help:
http://asp.net-tutorials.com/user-controls/creating/
I need to execute Java script function from c# code behind . Using Asp.net server control Repeater and checking condition when condition is true need to run java script function which will turn hyper link display block this is my javascript function
function MyFunction(username) {
document.getElementById("hyp").style.display = "block";
}
This is my Asp.net code after that condition if true I call c# code behind method writeText() :
<%# Eval("part2").ToString() == "part2" ? writeText(Eval("Albumtitle").ToString(), Eval("Albumtitle").ToString(), inc.ToString()) : writeLink(Eval("Albumtitle").ToString(), Eval("Albumtitle").ToString())%>
protected string writeText(string PageDataId, string AlbumId, string a)
{
ClientScript.RegisterStartupScript(this.GetType(), "hwa", "MyFunction('Checkjj');", true);
string html = "";
html += "";
return html;
}
This c# call javascript function which display html anchor to block which was display none at the start
Here is that anchor tag
<a id="hyp" name="hyp" href="#" class="lightbox<%=inc+"b" %>">Part II</a>
With your html: <a id="hyp" name="hyp" ... add the attribute runat="server". Then on the server side you can access the object like if it were a server control.
There is not need to execute Javascript on the server side to perform the action you need.
Something like on "ItemDataBound" Event for the repeater:
void MyRepeater_ItemDataBound(Object Sender, RepeaterItemEventArgs e) {
string someVar = "someValue" ;
((HtmlAnchor)e.Item.FindControl("hyp")).Attributes.Add ("css", "lightbox" + someVar);
}
In order to use this event don't forget to add the event and the runat server attribute :
<asp:Repeater ID="MyRepeater" runat="server" OnItemDataBound="MyRepeater_ItemDataBound">
<a id="hyp" name="hyp" href="#" runat="server">Part II</a>
</asp:Repeater>
You can find this blog helpful:
http://www.aspnettutorials.com/tutorials/controls/repeater-onitemdatabound-cs/
http://www.nullskull.com/q/10259645/anchor-tag-not-well-formed-when-id-is-provided.aspx
so i have this button that appears on multiple pages all leading to the parent page. But now - when i reach the parent page i still have the button showing. I want to remove/hide the button when on the parent page.
Is it possible that i could check my current page url and check to see if it the parent page?
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
thinking using an if statement surrounding the tag
If you wanted to do your check in the code behind page, you can always execute the code below during the page load event.
if (Path.GetFileName(Request.Url.LocalPath) == "ParentPage.aspx")
{ element.visible = false; }
else
{ element.visible = true; }
yeah that's what the window.location variable is for see this
When you are in your parent page, what is the ElementId value ?
I assume it can be null, so you may be able to check this value before displaying your button
#if(ViewBag.ElementId != null){
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}
else { //TODO...
}
It looks like ASP.NET MVC to me.
#if (!Request.Url.AbsolutePath.EndsWith("/i/dont/want/this"))
{
<a id="element" href="/Pages/#ViewBag.ElementId" class="btn">
<i>Home</i>
</a>
}
I have many A html tags in my master web page. I would like to replace their HREF values at runtime using code. How to do that? All a tags are tagged with runat="server".
You have to iterate over all the controls in the ControlsCollection and update the Href property of all controls that are of type HtmlAnchor, like this:
private void UpdateTags(Control page)
{
foreach (Control ctrl in page.Controls)
{
if (ctrl is HtmlAnchor)
{
((HtmlAnchor)ctrl).HRef = "myNewlink";
}
else
{
if (ctrl.Controls.Count > 0)
{
UpdateTags(ctrl);
}
}
}
}
You can use HRef property of AncorTag HTML Control to change it.
like this:
<a id="anchor1" runat="server"></a>
In code
void Page_Load(object sender, EventArgs e)
{
anchor1.HRef = "http://www.microsoft.com";
}
HtmlAnchor MyAnchor = (HtmlAnchor)e.Item.FindControl("YourAnchorID");
MyAnchor.HRef = "mypage.aspx";
You should give it an Id and then change the Href property.
<a runat="server" id="link1">link 1</a>
And then:
link1.HRef = "http://stackoverflow.com";
You could also make a CustomControl, extending the Hyperlink-Class and put some Logic into that.
We use it to a custom hyperlink to add Trackingdata to some links.
In my web app (asp.net C#) I have menus as "All", "Education", "Fun", "Comedy". I want when I select All then it should be displayed as current menu, for which I have a CSS class current.
In case of WebUserControls I can do it easily by passing parameter of current page to select as below:
mywebpage.aspx
<uc:header ID="header1" runat="server" selectedMenu="comedy" />
header.ascx (code (c#))
public string selectedMenu
{
get { return strSelected; }
set { strSelected = value; }
}
header.ascx (html)
<ul>
<li><a href="/all/" title="All Videos" <%if (strSelected == "all"){%>class="current"<%} %>><span>All</span></a></li>
<li><a href="/fun/" title="Fun Videos" <%if (strSelected == "fun"){%>class="current"<%} %>><span>Fun</span></a></li>
<li><a href="/comedy/" title="Comedy Videos" <%if (strSelected == "comedy"){%>class="current"<%} %>><span>Comedy</span></a></li>
</ul>
When I'll pass comedy from my webpage to usercontrol then it will select comedy menu. I want to implement the same kind of functionality in case of master pages, could anyone tell me how to achieve this type of facility for master pages.
One way what I feel is to pass a query string parameter http://example.com/all/?select=all, I'll check on master page_load function if select parameter is "all" or fun or comedy then select corresponding menu. But this is not good, because I don't want to add an extra query string parameter to my URLs.
Please tell me how to solve this issue.
Thanks
You can access master page properties from your content page after casting the master to the correct type:
public class MyMasterPage : MasterPage
{
public string MyMenuProperty { get; set; }
protected void Page_PreRender(object sender, EventArgs e)
{
if (MyMenuProperty == "comedy")
{
/* do your menu stuff */
}
}
}
public class MyContentPage : Page
{
protected void Page_Load(object sender, EventArgs e)
{
var myMaster = Page.Master as MyMasterPage;
if (myMaster != null)
{
myMaster.MyMenuProperty = "comedy";
}
}
One way i have done simular in the past is add this to the page derivatives of any content pages:
<%# MasterType VirtualPath="~/YourMaster.master" %>
Then in the master i exposed this:
private PageTypes currentPageType;
public PageTypes CurrentPageType
{
get { return currentPageType; }
set { currentPageType = value; }
}
On this job this was used so the master knew what type of page it was on and therefore changed a few things, colours, controls etc. So from a contents pageload i did
Master.CurrentPageType = PageTypes.System;
One thing to note however, VS tends to moan about the MasterType derivative until you do a rebuild all.
I'm thinking that maybe a completely different approach might be easier to implement and maintain.
How about you just parse the URL in your header.ascx?
For example:
<li><a href="/all/" title="All Videos" runat="server" class='<%= Request.Url.ToString().Contains("/all/") ? "current" : "" %>' ><span>All</span></a></li>
That way, you won't have to worry about setting properties, accessing/casting master pages etc...
Hai prashant,
I had the same issue a month back and i posted in stack overflow check this one it may help you Find a UnorderedList <UL> control inside a master page from a content page in asp.net