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}" />
Related
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!
In aspx, using either a ListView, Repeater, or any of the other standard controls that are listed under the visual studio 2010 data toolbox: My data source is simply a set of records made up of ID, title, first_name, last_name, degree. I am trying to print them on a single line (going to a newline when the end of a line is reached, not necessarily by a new record), however, the ItemTemplate and AlternatingItemTemplate seems to print each record as a newline. Is there a way to get rid of that automatic newline? (I tried ItemSeparatorTemplate, but that didn't work)
I don't think that it is necessary, but I figured that I'll just give you the code:
<asp:ListView ID="ListView2" runat="server" DataSourceID="AccessDataSource2">
<ItemTemplate>
<a href='#<%# Eval("ID") %>' /><%# Eval("title")%> <%# Eval("first_name")%> <%# Eval("last_name")%> <%# Eval("degree")%></a>
</ItemTemplate>
<AlternatingItemTemplate>
<a href='#<%# Eval("ID") %>' /><%# Eval("title")%> <%# Eval("first_name")%> <%# Eval("last_name")%> <%# Eval("degree")%></a>
</AlternatingItemTemplate>
</asp:ListView>
Use Datalaist and make RepeatDirection="Horizontal".
<asp:DataList ID="datalist1" runat="server" RepeatDirection="Horizontal"></asp:DataList>
How would it be possible to set text of ASP.NET LinkButton like below:
<asp:LinkButton id="LinkButton_Select" runat="server" Text='
<p><%# DataBinder.Eval(Container.DataItem, "Start")%></p>
<p><%# DataBinder.Eval(Container.DataItem, "End")%></p>
'/>
Try this
<asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# "<p>"+ DataBinder.Eval(Container.DataItem, "Start")+"</p> <p>"+DataBinder.Eval(Container.DataItem, "End")+"</p>"%>'/>
Why not just do the below:
<p><asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "Start")%>'/><p>
<p><asp:LinkButton id="LinkButton_Select2" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "End")%>'/><p>
try something like
<asp:LinkButton id="LinkButton_Select" runat="server" Text='<%# string.Format("<p>{0}</p> <p>{1}</p>",DataBinder.Eval(Container.DataItem, "Start"),DataBinder.Eval(Container.DataItem, "End")) %>'/>
Your code will fail because, on a runat="server tag, each attribute has to either be completely a '<%# %>' section, or not at all. You can't use <%# %> for part of it and plain text for the rest. #StrouMfios showed the way around that using string.Format, but there's another issue - when converted to HTML, you'd end up with an <a> tag containing <p> tags, which is illegal. If splitting it up into two separate linkbuttons doesn't work for you, the only other way you could do it legally is by using <span> tags styled to be display:block with extra spacing.
I found this answer which is the most simple:
Text='<%# ""+ Eval("Start") + "" + Eval("End")+""
Thanks all!
This worked for me, set the value of attribute text in the page load.
Example:
yourpage.aspx
<asp:Button ID="yourButtonId" runat="server" OnClick="StartEvent" />
yourpage.aspx.cs
protected void Page_Load(Object sender, EventArgs e)
{
// Set Text asp:Button
yourButtonId.Text = "Your text";
}
I databing array of User objects to GridView control. Last column contains "action" anchors (edit, remove):
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
Edit
</ItemTemplate>
</asp:TemplateField>
However I would like not to output first anchor to Remove action if currently binded User object has the same id as use logged in (available with this.SessionUser.Id).Something like this:
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
if (this.SessionUser.Id <> Eval("user_id") { Edit }
</ItemTemplate>
</asp:TemplateField>
How can I do it?
Thanks!
You can use a runat="server" control for this
<asp:TemplateField HeaderText="Actions">
<ItemTemplate>
Remove
<a href="Edit.aspx?id=<%# Eval("user_id") %>" runat="server"
visible='<%# this.SessionUser.Id <> Eval("user_id") %>'>Edit</a>
</ItemTemplate>
</asp:TemplateField>
All server controls, even HTML tags with runat="server" have this Visible property, which omits the control from the final HTML when it is false.
not supported :( you need to write another function passing it user_id and get the appripriate string from it like this:
//in cs file
protected string GetLink(object o)
{
if(!SessionUser.Id.Equals(o)) //or anyother way to compare equality
return string.Format("<a href=\"Edit.aspx?id={0}\">",0);
return "";
}
//in aspx file
<ItemTemplate>
Remove
<%# GetLink(Eval("user_id"))%>
</ItemTemplate>
you can use CSS:
<a style='visible:<%# this.SessionUser.Id <> Eval("user_id") %>' > ... </a>
make sure that this.SessionUser.Id is a public variable in your .cs file
I'm working through examples in a book trying to learn ASP.NET, and I've stumbled on something strange in there. First of all, if I type it as it's written in the book, VS gives me errors. This is the code as it's written in the book:
<asp:DataList ID="employeesList" runat="server">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text=<%#"View more details about " + Eval("Name")%>
CommandName="MoreDetailsPlease"
CommandArgument=<%#Eval("EmployeeID")%> />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
So, I've plucked at it for a while, and came up with this solution which actually compiles:
<asp:DataList ID="employeesList" runat="server" onitemcommand="employeesList_ItemCommand">
<ItemTemplate>
<asp:Literal ID="extraDetailsLiteral" runat="server" EnableViewState="false" />
Name: <strong><%#Eval("Name") %></strong><br />
Username: <strong><%#Eval("Username") %></strong><br />
<asp:LinkButton ID="detailsButton" runat="server" Text='View more details about <%# Eval("Name") %>'
CommandName="MoreDetailsPlease" CommandArgument='<%Eval("EmployeeID") %>' />
</ItemTemplate>
<SeparatorTemplate>
<hr />
</SeparatorTemplate>
</asp:DataList>
Notice that I've also added the OnItemCommand in the asp:DataList tag, so now I'm able to fire the event as expected.
However, results in the browser isn't what I expect; the Name and Username listed in strong text show just fine, but the Literal control that should show extra details (the EmployeeID field) and the Name field inside the LinkButton won't show their values in the page:
not showing as expected http://lh6.ggpht.com/_x84bQLYH57A/SgxzygartcI/AAAAAAAAAIY/nhT-6RUJa6o/s144/EmployeeDirectory_notshowing.jpg
It should say "EmployeeID: 1" and "View more details about Zak Ruvalcaba"
I guess it's the Eval function that's not working when inside another control, can anyone help me out?
Change the LinkButton as :
<asp:LinkButton ID="detailsButton" runat="server"
Text='<%# Eval("Name", "View more details about {0}") %>'
CommandName="MoreDetailsPlease"
CommandArgument='<%# Eval("EmployeeID") %>' />
Sorry I confused the order of parameters. I updated my answer. Format must be the second parameter.
You can view another question I posted yesterday concerning something eerily similar here:
Need help with Eval inside DataList
I do believe Canavar actually gave the correct answer, however.