Turining An HTML <td> into a link ASP.NET MVC - asp.net

I'd like to be able to turn rows of an html table into links to controllers.
I figured something like
<td onclick="<%:Html.ActionLink("", "Index", new {id=item.user_id}) %>">
I'm using MVC 2
Thanks.

<td onclick="window.location='<%:Url.Action("Index", new {id=item.user_id}) %>'">
The onclick attribute accepts some javascript code to execute. If you simply give it a URL, javascript doesn't know what to do with that.
In the snippet above, you're setting the window.location property to the desired URL. This causes the browser to go there.
EDIT: I also just realized that you were using the Html.ActionLink() method which actually generates an tag in your code. You'd be better off using the Url.Action() method, which actually generates a URL.

Related

liferay-ui:search-container-column-button wont react to click

i am using liferay-ui:search-container-column-button inside liferay container the button should point to another jsp with take parameter from the current selected row.
<liferay-ui:search-container emptyResultsMessage="There are no organizations available">
<liferay-ui:search-container-results
results="<%=OrganizationLocalServiceUtil.getOrganizationAll() %>"
total="<%=OrganizationLocalServiceUtil.getOrganizationsCount() %>">
</liferay-ui:search-container-results>
<liferay-ui:search-container-row
className ="fff.sem.service.model.Organization"
keyProperty="organizationId"
modelVar="organization"
escapedModel="<%=true %>"
>
<liferay-ui:search-container-column-text
name="Organization short name"
value="<%= organization.getOrganizationShortName() %>">
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-text
name="Organization full name"
value="<%= organization.getOrganizationFullName() %>">
</liferay-ui:search-container-column-text>
<liferay-ui:search-container-column-button href="<%=addOrganization1URL %>"></liferay-ui:search-container-column-button>
<liferay-ui:search-container-column-jsp
align="right"
path="/html/sem/organization_actions.jsp"
/>
</liferay-ui:search-container-row>
As you noticed in the code I used search-container-column-jsp to add edit and delete feature for the row. I use a column with search-container-column-button so that each button of that column point to another form, but the search-container-column-button wont react on clicking. What is going on?
Recent Liferay forum posts indicate a potential issue with the taglib:
search-container-column-button does not work
Issue in calling an action from liferay search-container-column-button tag
grep of the Liferay source code nor a github search return a single instance where the taglib is used.
My attempts to use the <liferay-ui:search-container-column-button to properly call any type (action, render, resource) URL were not successful. In examining the source closer, I believe the generic dump of the href attribute value into the onclick attribute is the problem.
In comparing the html generated by a <aui:button> to that of the search-container-column-button the onclick attribute is missing location.href=
I would recommend using a liferay-ui:search-container-column-jsp as you have done for the other actions, but instead of an icon-menu use a <aui:button> if you want the field to be a button.
Including the missing info:
<%
final String hrefFix = "location.href='" + addOrganization1URL .toString()+"'";
%>
<liferay-ui:search-container-column-button href="<%=hrefFix%>"/>
does provide a working solution, however, if this is indeed a bug and is fixed in a future patch, this workaround would then become a bug.

hyperlink.NavigateUrl getting changed on master page (possibly by ResolveUrl() )

I have a hyperlink that in certain cases I want to change to show a jquery popup, but I'm having a strange problem when doing it on a master page. The following works in a regular page:
hyp1.NavigateUrl = "#notificationPopup";
Which renders as:
<a id="ctl00_hyp1" href="#notificationPopup">Example</a>
This is exactly what I want. The problem is with the exact same code on a hyperlink on the master page it renders as:
<a id="ctl00_hyp1" href="../MasterPages/#notificationPopup">Example</a>
It looks like it might be running the navigateUrl through ResolveClientUrl() or something when I'm setting it on the master page. I've tried swapping the <asp:hyperlink for a <a href runat=server, but the same thing happens.
Any ideas?
There is a note on MSDN Control.ResolveClientUrl method description.
The URL returned by this method is
relative to the folder containing the
source file in which the control is
instantiated. Controls that inherit
this property, such as UserControl and
MasterPage, will return a fully
qualified URL relative to the control.
So the behavior of master page in your exampe is fully predictable (although this is not a very comfortable to work with). So what are the alternatives?
The best one is to set the <a> as a client control (remove runat="server"); should work like a charm even in a master page:
Example
In the case if this control should be server side only: you could just build an URL from your code behind by using UriBuilder class:
UriBuilder newPath = new UriBuilder(Request.Url);
// this will add a #notificationPopup fragment to the current URL
newPath.Fragment = "notificationPopup";
hyp1.HRef = newPath.Uri.ToString();
Create a hidden field on your form and set the value to where you want to navigate / the url of the hyperlink instead of the hyperlinks navigate url. Then call the onclick method of the hyperlink in javascript and set the hyperlink there before the browser does the actual navigation.
<html><head><title></title></head>
<script type="text/javascript">
function navHyperlink(field)
{
field.href = document.getElementById('ctl00_hdnHypNav').value;
return true;
}
</script>
<input type="hidden" id="hdnHypNav" value="test2.html" runat="server"/>
<a href="" onclick="navHyperlink(this);" >click here</a>
</html>
Code behind would be:
hdnHypNav.value = "#notificationPopup";
You could also just try setting the url after the postback with below code, i.e. replace your code behind line with this one but I am not sure if it will work...
ScriptManager.RegisterStartupScript(this,this.GetType(),"SetHyp","$('ctl00_hyp1').href = '#notificationPopup';",True)
I found another way to solve the problem.
hyp1.Attributes.Add("href", "#notificationPopup");
Seeing as the whole reason I replaced my static hyperlink with a runat="server" one was to benefit from automatic resource-based localization, none of these answers served my needs.
My fix was to enclose the hyperlink in a literal:
<asp:Literal ID="lit1" runat="server" meta:resourcekey="lit1">
Example
</asp:Literal>
The downside is if you need to programmatically manipulate the link, it's a bit more annoying:
lit1.Text = String.Format("Example", HttpUtility.HtmlAttributeEncode(url));

Routing to an anchor on another page

I am using Web Forms Routing in ASP.NET 4 and I am trying to route to a specific location on a page. On that page I have an element like <div id="3"> and I'd like to jump to this anchor from another page. For this purpose I have defined a Route in global.asax:
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/{PageAnchor}",
"~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } });
The HyperLink to link to that page and the anchor "3" is defined this way in markup:
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=#3 %>">
Link</asp:HyperLink>
The problem with the generated link is that the # character in the URL gets encoded by %23 this way: http://localhost:1234/Path/SubPath/%233 so that I reach the target page but not at the specified anchor.
Is there a way to avoid this unwished URL-encoding? Or any other way to route to an anchor?
Thank you in advance!
Anchors are not supported with ASP.NET's routing feature. Routing is designed to support only the part of the URL after the application's path and before the anchor.
I suggest adding an event handler (e.g. Page_Load) and in that event handler generate the URL, append the anchor, and set the value on the HyperLink control.
Of course, in most cases with Web Forms routing it's easiest to just set the URL manually to whatever you want. This is a nice option when the URL is not complex and is unlikely to change.
Does this work?
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath/#{PageAnchor}",
"~/MyPage.aspx", true, new RouteValueDictionary { { "PageAnchor", null } })
<asp:HyperLink ID="HyperLink1" runat="server"
NavigateUrl="<%$ RouteUrl:RouteName=MyRoute,PageAnchor=3 %>">
Link</asp:HyperLink>
If you place the # outside of the PageAnchor placeholder, you could avoid that value being decoded, and it seems like a cleaner way to do it, besides.
How about the route goes to a controller and that reroutes to the page with the anchor parameter?
I know this is an old question, but maybe someone else could benefit from this approach.
Create a route without the anchor.
RouteTable.Routes.MapPageRoute("MyRoute", "Path/SubPath", "~/MyPage.aspx");
And then construct the url like this, appending the anchor.
Link

Why does ASP.Net rewrite relative paths for runat=server anchor controls?

I have my UserControls in a ~/Controls folder in my solution:
/Controls/TheControl.ascx
If specify the following:
<a id="theId" runat="server" href="./?pg=1">link text</a>
ASP.Net seems to want to rewrite the path to point to the absolute location. For example, If the control is on site.com/products/fish/cans.aspx the link href will be rewritten to read
<a id="munged_theId" href="../../Controls/?pg=1>link text</a>
Why does Asp.Net rewrite these control paths, and is there an elegant way to fix it?
I just want the anchor control to spit out exactly what I tell it to!!! Is that so hard?
EDIT:
I've basically done what Kelsey suggested. I knew I could do it this way, but I don't like adding markup in my code when I want something relatively simple. At least it solves the problem:
Aspx page:
<asp:PlaceHolder ID="ph" runat="server"></asp:PlaceHolder>
Code-behind:
var anchor = new HtmlGenericControl("a") { InnerText = "Previous" + " " + PageSize) };
anchor.Attributes["href"] = "?pg=" + (CurrentPage - 1);
anchor.Attributes["class"] = "prev button";
ph.Controls.Clear();
ph.Controls.Add(anchor);
As you can see by the amount of code needed for what is essentially supposed to be be a simple and light-weight anchor, it's not the most optimal solution. I know I could use a Literal but I figured this was cleaner as I'm adding more than one anchor.
I would be interesting in knowing WHY ASP.Net takes over and tries to fix my URL, though.
Why do you have runat="server" and no ID defined? Do you need to access it server side? If you remove the runat="server" everything will work as expected.
For more information regardinging how ASP.NET handles paths check out this MSDN article.
Edit: You can get around the problem then by using a Literal control and then outputing the raw <a href... to it.
Eg:
<asp:Literal ID="myLiteral" runat="server" />
myLiteral.Text = "link text";
Then you can set the visible property on the Literal however you want.
I know this is a bit of an old topic, but I was running into this problem as well and in the end went with a similar solution, but was able to save a few lines of code by doing this in the ascx:
<anchor id="myAnchor" runat="server" href="xxx">link text</anchor>
Then in the code behind, I referenced it using an HtmlGenericControl and can then do this:
myAnchor.TagName = "a";
// other properties set as needed
Anyway, I thought I'd post in case anyone else stumbles in here with the same issue.
Best bet is to make everything app root relative using the magic ~/ lead-in to the url. That tends to keep stuff straight.
There isn't a great answer to your question. ASP.NET is going to treat a relative path in a UserControl as relative to the path of the user control.
What you can do is in the code behind for your user control, set the HRef property of your anchor tag based on the Request.Path property. Then you can create URLs relative to the page.
Alternative is to use a literal like Kelsey was suggestion, or I would just try and map everything app relative with ~/ like Wyatt suggested.
Even a literal doesn't work using ICallBackEventHandler and RenderControl at least... I ended up hacking the tag back client-side :/ e.g in JQuery:
$('#munged_theId').attr('href', './?pg=1');

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