href of anchor tag inside listview not fired - asp.net

I have a listview which contain an tag inside it, also it contain an href, but the href is not working. Here is the code
<asp:ListView ID="listsearch" runat="server" ItemPlaceholderID="itemsearch">
<LayoutTemplate>
<ul class="ada"><asp:PlaceHolder ID="itemsearch" runat="server"/></ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:LinkButton ID="LinkButton1" runat="server" PostBackUrl='<%# String.Format("https://www.google.co.in") %> ' Text="Ssdsd"/>
</li>
</ItemTemplate>
</asp:ListView>
HTML output:
<a id="ctl00_ContentPlaceHolder1_listsearch_ctrl0_LinkButton1" href='javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctl00$ContentPlaceHolder1$listsearch$ctrl0$LinkButton1"‌​, "", false, "", "google.co.in";, false, true))' style="border-width: 1px;">Ssdsd</a>

<asp:LinkButton> control is intended for simulate the behaviour of an <asp:button> but in hyperlink taste. If you do not need server processing on click event better use <asp:hyperlink> or simply non-server html <a> tag:
I guess you've a property like Url in the DataSource passed to the ListView, so you replace your LinkButton for something like that:
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl='<%# Eval("myUrl") %>'
Target="_blank"><%# Eval("myUrl") %></asp:HyperLink>
Or better:
<%# Eval("MyUrl") %>
And also opens the url in a new tab!

Related

Inline Widgets in Kentico Repeater

I have a repeater that evaluates the content field from a page. This content may have inline widgets in it:
Now, normally I would wrap Eval("Content") in a placeholder with EnableViewState="false" and then resolve it dynamically in code behind but the problem is that the repeater repeats the ID for the placeholder rendering it invalid.
Is there anyway around this?
<cms:CMSUniView ID="MainNavMenu" runat="server" >
<HeaderTemplate>
<ul>
<li>
</HeaderTemplate>
<ItemTemplate>
<a href="<%# Eval("Link") %>"
title="<%# HTMLHelper.HTMLEncode( Convert.ToString(Eval("DocumentName"))) %>" >
<%# HTMLHelper.HTMLEncode( Convert.ToString(Eval("DocumentName"))) %>
</a>
<%# Eval("Content") %>
</ItemTemplate>
<SeparatorTemplate>
</li>
<li>
</SeparatorTemplate>
<FooterTemplate>
</li>
</ul>
</FooterTemplate>
</cms:CMSUniView>
You can try to resolve the content by using CMS.MacroEngine.MacroResolver.Resolve() method. So your code will look like:
<%# MacroResolver.Resolve(Eval("Content").ToString()) %>
And if you want to find any control inside a repeater template, I guess you should do it on ItemDataBound event. Example

How to make this data control reusable for different content and data?

I'm using ASP.NET Web Forms, and I've got a Repeater control to display some data. Currently it looks like this:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<strong>Details:</strong> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<strong>Status:</strong> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
This list has hover behaviour, etc. and I'd like to reuse the same style of list across my project, just with different data (really just replacing the three lines with <strong> tags in). However, I obviously don't want to copypaste and change it every time it's used, so I thought I'd try and make it into a User Control:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<%= DataString %>
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And in my corresponding class in CodeBehind:
public string DataString { get; set; }
So my main aim was to do this:
<uc:MyUserControl runat="server" ID="blah" DataString="<strong>Date/Time:</strong><%# DataBinder.Eval(Container.DataItem, "date") %> ETC..." />
But it seemed that ASP.NET wanted to evaluate the <% %> in the string there and then, and I tried different ways to get around it such as this:
<% string theDataString = "<strong>Date/Time:</strong> <%# DataBinder.Eval(Container.DataItem, "date") %>"; %>
<uc:MyUserControl runat="server" ID="blah" DataString='<%= theDataString %>' />
But I ran into trouble again with the closing %> in theDataString, and even after replacing it with something like {0: %} (to escape the % sign: can't remember the exact syntax), which did escape it correctly, I discovered that you can't actually use the <%= %> inside an attribute, which was annoying.
My last-ditch attempt was to set DataString in my page's CodeBehind, and the HTML <strong> rendered correctly, but ASP.NET didn't evaluate the <%# %> and just spat it out literally. So I'm kind of stuck as I can't think of anything else... (by the way I'm still relatively new to ASP.NET)
I'm not even sure this is a good way to go about it - previously I thought about making it into a templated user control but this method seemed simpler and it was something I already knew how to do, or so I thought. So any help with this would be greatly appreciated, thanks!
EDIT: Just to be clear, my emphasis here is on the re-usability of this particular control - so I will be able to display different types of data records in this style of list (clickable, highlight behaviour etc).
If your goal is to have three lines appear as bold instead of normal text, then your best bet is to use something more geared towards website-display than something programmatic and heavy-handed like a dynamic server-side user control. For example:
<asp:Repeater runat="server" ID="blah" OnItemDataBound="blah" OnItemCommand="blah">
<HeaderTemplate>
<ul class="itemList">
</HeaderTemplate>
<ItemTemplate>
<li runat="server" id="listItem" class="itemNormal" onmouseover="this.className = 'itemHover';" onmouseout="this.className = 'itemNormal';">
<asp:LinkButton runat="server" ID="btn_select" CommandName="Select" Text="Select" style="display: none;"></asp:LinkButton>
<span class="displayMode">Date/Time:</span> <%# DataBinder.Eval(Container.DataItem, "date") %><br />
<span class="displayMode">Details:</span> <%# DataBinder.Eval(Container.DataItem, "details") %><br />
<span class="displayMode">Status:</span> <%# DataBinder.Eval(Container.DataItem, "status") %><br />
</li>
</ItemTemplate>
<FooterTemplate>
</ul>
</FooterTemplate>
</asp:Repeater>
And then, programmtically show:
<style type="text/css">
.displayMode { font-weight: bold; }
/* and/or */
.displayMode { text-decoration: underline; }
</style>
Use the right tool for the job, in this case, I don't feel as though server-side code is the right way to accomplish this task. If, on the other hand, you need to change the content of the insides of your <li> tags, then yes you would have to do it in C#. However, if that were the case you could easily bind it to a different object or use a different control or inherit the control or any number of other methods. For your specific case, just use standard HTML manipulation techniques.

asp.net gridview edit template http link

I have a label within an Edit Template for a gridview which goes like this:
<asp:Label ID="lblProjectID" runat="server" Text='<%# Bind("Project_ID") %>'></asp:Label>
I would like to turn that label text into a http link like so:
<asp:Label ID="lblProjectID" runat="server" Text='<a href=http://intranet/?<%# Bind("Project_ID") %>> <%# Bind("Project_ID") %></a>'></asp:Label>
So the link would look something like http://intranet/?Project_ID
But that syntax is incorrect. What is the correct way to write that?
This is what you can do on your Label tag.
<asp:TemplateField>
<ItemTemplate>
<a href='<%# String.Format("http://intranet/?Project_ID={0}", Eval("Project_ID")) %>'><%# Eval("Project_ID")%></a>
</ItemTemplate>
</asp:TemplateField>
If you want it to be a link... then just use a link, not a label:
<a href='http://intranet/?<%# Eval("Project_ID") %>'><%# Eval("Project_ID")%></a>
or same thing with HyperLinkField (if you want to use it as a column and not inside EditItemTemplate:
<asp:HyperLinkField DataTextField="Project_ID" DataNavigateUrlFields="Project_ID" DataNavigateUrlFormatString="http://intranet/?{0}" />

Passing Value Through Link Button in ASP.NET

Conditions :
I have two tables,
category(id_kat,name_kat)
book(id_book,id_kat,name_book)
id_kat has a child and parent relations.
I'm a little bit confused using ASP.NET, especially when passing value between pages.
Now, i'm showing the category's datas with
<asp:listview id="listview1" runat="server" datakeynames"id_kat" datasourceID="sdskat">
//FYI sdskat is datasource for category table
<itemtemplate>
<li>
<asp:linkbutton id="linking" runat="server" ><%# Eval("name_kat") %></asp:linkbutton>
</li>
</itemtemplate>
</asp:listview>
The problem is, i wanted to pass "id_kat" value when i click the hyperlink. (but redirected to self page).
I want it as a query string (or another way if able).
Thanks a lot.
If you're linking to the same page with different query string parameters, you only need to include ? and after.
<%# Eval("name_kat") %>
You could use <asp:hyperlink id="linking" runat="server" ><%# Eval("name_kat") %></asp:hyperlink> instead. You can then add your url in the navigateURL attribute and append your id_kat on there.
Something like this should work:
<asp:hyperlink id="linking" runat="server" navigateURL="~/page.aspx?id_kat=<%# Eval('id_kat') %>" ><%# Eval("name_kat") %></asp:hyperlink>
the <asp:hyperlink> tag is essentially the tag but using a server side control.
If you have to use a linkbutton then in your code behind you can append the id_kat to the querystring and then response.redirect(url) to the new page.
Why not use a regular anchor?
<a href='foo.aspx?id=<%# Eval("id_kat") %>'><%# Eval("name_kat") %></a>

Using fixed text together with <%# when databinding in Asp.NET doesn't work

This works:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/MyCustomUrl.aspx
This doesn't:
<asp:HyperLink ID="EditGridItemLink" runat="server" NavigateUrl="subfolder/<%# GetCustomUrl() %>">
link
</asp:HyperLink>
link value = http://localhost/subfolder/<%# GetCustomUrl() %>
I'm doing this in a Column/TemplateField of an Asp.NET GridView.
Am I doing something wrong or is this simply impossible.
Of course I'm using a workaround now where I set the rest of the NavigateUrl value as well, but I was wondering why this isn't working...
You can do this instead: NavigateUrl='<%# string.Format("subfolder/{0}", GetCustomUrl()) %>'.
Databinding expressions for properties of server controls must be the only thing in the property (no mixing with static text in the way you tried).
You should put the whole expression inside the databinding markup:
<asp:HyperLink ID="EditGridItemLink" runat="server"
NavigateUrl="<%# "subfolder/" + GetCustomUrl() %>">
link
</asp:HyperLink>

Resources