Set asp:LinkButton text in markup - asp.net

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";
}

Related

Repeater Control in ASP.NET

Please help me to display images from database to webpage. This is my source code, but it shows error creating repeater control
<asp:Repeater ID="Repeater1" runat="server" OnItemDataBound="Repeater1_ItemDataBound" OnItemCommand ="Repeater1_ItemCommand">
<ItemTemplate>
<asp:Image ID="myImage" ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>' runat="server" alt=" " ;style="height:200px;width:200px;border:1px solid gray"/><asp:Literal ID="litSeparator" runat="server"></asp:Literal>
</ItemTemplate>
</asp:Repeater>
I think the problem is in this "ImageUrl='<%# "~/ImageHandler.ashx?BImID="+Eval("img_id") %>'".
Try to reorganize yor code like this:
ImageUrl='<%# GetImageUrl(Eval("img_id")) %>'
and then in code behind define the method GetImageUrl like this:
protected string GetImageUrl(object id)
{
return "~/ImageHandler.ashx?BImID=" + id;
}
Regards,
Uroš

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}" />

find control inside datalist in button event

I had data list as label for Question and textarea for Answer ,as
datalist render Question on label and the user will add the Answer in
text area as the answers inserted in database. I did my code , but
when I add my code to find the controls (label,textarea) they returned
with null value although I add answers for the Questions .
protected void BT_submit_Click(object sender, ImageClickEventArgs e)
{
Label QID = (Label)Dl_Question.FindControl("lbl_QID");
HtmlTextArea QAnswer = (HtmlTextArea)Dl_Question.FindControl("Txt_Answer");
}
DataList code:
<asp:DataList ID="Dl_Question" runat="server" onitemdatabound="Dl_Question_ItemDataBound" onitemcommand="Dl_Question_ItemCommand">
<ItemTemplate>
<asp:Label ID="lbl_QID" runat="server" Text='<%# Eval("ID") %>' Visible="false">
</asp:Label><br />
<asp:Label ID="Lbl_Question" runat="server" Text='<%# Eval("Question") %>'></asp:Label> <br />
<textarea id="Txt_Answer" cols="80" rows="5" runat="server"></textarea>
</ItemTemplate>
</asp:DataList>
I would assume you are looking at higher level than you think. You might need something like this:
Label QID = Dl_Question.**Items[n]**.FindControl("lbl_QID") as Label;
If you don't want to bother specifying the control heir achy use a recursive algorithm.

Conditional output in GridView row

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

Formatting DataBinder.Eval data

How can I format data coming from a DataBinder.Eval statement in an ASPX page?
For example, I want to display the published date of the news items in a particular format in the homepage. I'm using the ASP.NET 2.0 Repeater control to show the list of news items.
The code for this goes like this:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<HeaderTemplate><table cellpadding="0" cellspacing="0" width="255"></HeaderTemplate>
<ItemTemplate>
<tr><td >
<a href='/content/latestNews.aspx?id=<%#DataBinder.Eval(Container.DataItem, "id") %>'>
<asp:Label ID="lblNewsTitle" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "title") %>'></asp:Label>
</a>
</td></tr>
<tr><td>
<asp:Label ID="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate"))%>'></asp:Label>
</td></tr>
</ItemTemplate>
<FooterTemplate></table></FooterTemplate></asp:Repeater>
Is there a way I could call a custom method with the DataBinder.Eval value as its parameter (something like below)?
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
If yes, then where do I write the GetDateInHomepageFormat method? I tried out in the code behind page but got a run time error?
If this is not possible, is there a way to do inline formatting?
There is an optional overload for DataBinder.Eval to supply formatting:
<%# DataBinder.Eval(Container.DataItem, "expression"[, "format"]) %>
The format parameter is a String value, using the value placeholder replacement syntax (called composite formatting) like this:
<asp:Label id="lblNewsDate" runat="server" Text='<%# DataBinder.Eval(Container.DataItem, "publishedDate", "{0:dddd d MMMM}") %>'</label>
After some searching on the Internet I found that it is in fact very much possible to call a custom method passing the DataBinder.Eval value.
The custom method can be written in the code behind file, but has to be declared public or protected. In my question above, I had mentioned that I tried to write the custom method in the code behind but was getting a run time error. The reason for this was that I had declared the method to be private.
So, in summary the following is a good way to use DataBinder.Eval value to get your desired output:
default.aspx
<asp:Label ID="lblNewsDate" runat="server" Text='<%# GetDateInHomepageFormat(DataBinder.Eval(Container.DataItem, "publishedDate")) )%>'></asp:Label>
default.aspx.cs code:
public partial class _Default : System.Web.UI.Page
{
protected string GetDateInHomepageFormat(DateTime d)
{
string retValue = "";
// Do all processing required and return value
return retValue;
}
}
Hope this helps others as well.
Why not use the simpler syntax?
<asp:Label id="lblNewsDate" runat="server" Text='<%# Eval("publishedDate", "{0:dddd d MMMM}") %>'</label>
This is the template control "Eval" that takes in the expression and the string format:
protected internal string Eval(
string expression,
string format
)
http://msdn.microsoft.com/en-us/library/3d2sz789.aspx
You can use a function into a repeater like you said, but notice that the DataBinder.Eval returns an object and you have to cast it to a DateTime.
You also can format your field inline:
<%# ((DateTime)DataBinder.Eval(Container.DataItem,"publishedDate")).ToString("yyyy-MMM-dd") %>
If you use ASP.NET 2.0 or newer you can write this as below:
<%# ((DateTime)Eval("publishedDate")).ToString("yyyy-MMM-dd") %>
Another option is to bind the value to label at OnItemDataBound event.
This line solved my problem:
<%#DateTime.Parse(Eval("DDDate").ToString()).ToString("dd-MM-yyyy")%>
To format the date using the local date format use:
<%#((DateTime)Eval("ExpDate")).ToString("d")%>
How to Format an Eval Statement to Display a Date using Date Locale
Thanks to all. I had been stuck on standard format strings for some time. I also used a custom function in VB.
Mark Up:-
<asp:Label ID="Label3" runat="server" text='<%# Formatlabel(DataBinder.Eval(Container.DataItem, "psWages1D")) %>'/>
Code behind:-
Public Function fLabel(ByVal tval) As String
fLabel = tval.ToString("#,##0.00%;(#,##0.00%);Zero")
End Function
Text='<%# DateTime.Parse(Eval("LastLoginDate").ToString()).ToString("MM/dd/yyyy hh:mm tt") %>'
This works for the format as you want
<asp:Label ID="ServiceBeginDate" runat="server" Text='<%# (DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:yyyy}") == "0001") ? "" : DataBinder.Eval(Container.DataItem, "ServiceBeginDate", "{0:MM/dd/yyyy}") %>'>
</asp:Label>
You can use it this way in aspx page
<%# DataBinder.Eval(Container.DataItem, "DateColoumnName", "{0:dd-MMM-yyyy}") %>

Resources