Inline Code in markup page in ASP.NET Webforms? - c#

Is something similiar to the following pseduocode possible on server side controls?
<asp:Label runat="server" ID="lbl" Text=<%=DateTime.Now.ToString(); %> />
I.e. assigning attributes.

This will work for databound controls, e.g.
<asp:Label runat="server" ID="lbl" Text="<%# DateTime.Now.ToString() %>" />
Then in your code behind, you'll need to call lbl.DataBind().

Try this:
<asp:Label runat="server" ID="lbl" Text='<%=DateTime.Now.ToString()%>' />

Related

Place RequiredFieldValidator inside a asp:DataList

I have a problem with placing RequiredFieldValidator into a DataList, and some assistance would be greatly appreciated.
Problem description:
When I place the <asp:RequiredFieldValidator>inside the <ItemTemplate> and run the page I get the following error page:
When I place it outside of it, the page works with no error, but obviously it cannot identify witch <asp:TextBox> does not have content in it, before it is posted back. And I would like to make where it is able to tell if a particular text box in the data list does not have text entered.
Here is the markup: (the Image src attribute is not implemented yet)
<asp:DataList ID="imageUploadRoster" runat="server"
DataSourceID="ImageUploadRosterDataSource" RepeatDirection="Horizontal" RepeatColumns="5" HorizontalAlign="Left">
<HeaderTemplate>
<h3>Set Image Names</h3>
</HeaderTemplate>
<ItemStyle HorizontalAlign="Center" VerticalAlign="Middle" Height="180px" Width="180px" />
<ItemTemplate>
<div class="imageSetNameDiv">
<asp:HiddenField ID="ImageId" runat="server" Value='<%# Eval("ImageId") %>' />
<asp:HiddenField ID="ImageMimeTypeLabel" runat="server" Value='<%# Eval("ImageMimeType") %>' />
<asp:Image ID="ImageThumbnailLabel" runat="server" Src='<%# Eval("ImageThumbnail") %>' Width="120px" Height="120px" />
<br />
<asp:RequiredFieldValidator ID="imageNameRequired" runat="server"
ControlToValidate="ImageName" ErrorMessage="RequiredFieldValidator"
ValidationGroup="imageUploadValid">
</asp:RequiredFieldValidator>
<asp:TextBox ID="ImageName" runat="server" Text='<%# Eval("ImageName") %>' />
<br />
<hr />
<asp:Button ID="removeImage" runat="server" Text="Remove" CommandName="delete" CommandArgument='<%# Eval("ImageId") %>' />
</div>
</ItemTemplate>
</asp:DataList>
Thank you in advance for any assistance.
Peter
This error happens when ASP.NET encounters two controls with the same ID on the page. I guess for some reason in your case validators for each item get the same ID. Setting the following property:
ClientIDMode="Predictable"
for validator should fix it. This mode makes sure control in databound context gets correct ID.

ASP.net C# Repeater multiple objects

I have the following situation:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:ImageButton OnClick="imgSearchResult_Click" BackColor="#333333" ID="imgSearchResult" height="32" width="32" runat="server" ToolTip='<%# Eval("ToolTip") %>' ImageUrl='<%# Eval("ImageUrl") %>'/>
<asp:Label ID="labValue" Text='<%# Eval("Text") %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
The Repeater contains ImageButton and Label objects. If I only use one object, I can add them from codebehind through the binding:
Repeater.DataSource = imageList;
Repeater.DataBind();
OR
Repeater.DataSource = labelList;
Repeater.DataBind();
How can I add both to one Repeater?
If you need them in one Repeater, they -are- related in that "view" (loosely used term) of the data. So why not make that view into an explicit object that contains both the image and the label? Either named, or anonymous:
return Enumerable.Zip(imageList, labellist, (image, label) => new {image, label})
Alternatively, you can just pass the images and query for the labels by index:
<asp:Repeater ID="myRepeater" runat="server">
<ItemTemplate>
<asp:Label ID="labValue" Text='<%# GetLabelText(Container.ItemIndex) %>' runat="server" Width="32" />
</ItemTemplate>
</asp:Repeater>
This requires a public GetLabelText(index) method and probably extra state in your code behind to make the ImageList available to that method. Not very nice, but it gets the job done.

Can't reference Image ID in C# Code. Why?

I would like to know, why I can't reference "asp:Image" (Image1) control in my code, but I can reference datalist control (DataList1) which contains asp:Image. Here is my code:
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="imageHolder">
<asp:DataList ID="DataList1" runat="server" RepeatColumns="4" Width="100%">
<ItemTemplate>
<asp:Image ID="Image1" OnDataBinding="DataList1_DataBinding" Width="80%" Height="100px"
CssClass="datalistImages" runat="server" ImageUrl='<%# "http://mywebsite.com/" + Eval("url") %>' />
</ItemTemplate>
</asp:DataList>
</asp:Content>
And I'm 100% sure I'm working in the correct class. Any suggestions?
Because it's in the item collection.
If you want to reach that image control at real time you have to use ItemCreated or ItemDataBound events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist.items%28v=vs.110%29.aspx
Datalist events.
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.datalist_events%28v=vs.110%29.aspx

ASP.NET C# Nested controlls access

I've got something like that
<asp:ListView ID="lv" runat="server">
<LayoutTemplate>
<asp:Literal ID="litControlTitle" runat="server" />
<label id="test" runat="server">dw</label>
<asp:PlaceHolder ID="itemPlaceHolder" runat="server" />
</LayoutTemplate>
<ItemTemplate>
</ItemTemplate>
</asp:ListView>
Can someone tell me, how should I change label text, using C# code?
Main problem for me is- how to get access to the nested control (label, literal) from c# code?
EDITED:
<SelectedItemTemplate>
<asp:HiddenField ID="NumberEdit" runat="server"
Value='<%# Bind("numbers") %>' />
<label for="NameEdit">Name:</label>
<asp:TextBox ID="NameEdit" Width="160px" runat="server" AutoPostBack="true" OnTextChanged="NameEdit_TextChanged"
Text='<%# Bind("Name") %>' />
<br />
<label for="ShortcutEdit">Shortcut:</label>
<asp:TextBox ID="ShortcutEdit" Width="80px" runat="server"
Text='<%# Bind("Shortcut") %>' />
<br />
and I would like to generate automatically Shortcut text when user will change Name (Shortcut = 2 first letters from NameEdit)? Can you explain me, how should I do it? –
You would want to have an ItemDataBound event handler to get access to the controls for that particular item in your listview. The example on the page I linked should help you out.
First thing is that you need a data source binded with this ListView control, for example SqlDataSource, or any other allowed type you need:
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:YourConnectionString %>"
SelectCommand="SELECT [Title], [id] FROM [Articles]"></asp:SqlDataSource>
<asp:ListView ID="lv" runat="server" DataSourceID="SqlDataSource1" >
// rest of the code
</asp:ListView>
Second thing is that controls from LayoutTemplate template will be rendered only if there is any data to show. So if you have datasource, but it is empty, this tamplate will not be applied. But you can use EmptyDataTemplate to display info when there is nothing from the datasource to display.
And then, when you already have datasource defined and binded to your ListView and there is data that will be displayed, the LayoutTemplate will be rendered. And then you can use FindControl method of the ListView. As for example of getting this literal:
Literal l = (Literal)lv.FindControl("litControlTitle");
It's returning null for you because you have no data to display, so controls are not rendered at all.
((Label)ListView1.FindControl("test")).Text = "Hello!";

Making an email label a hyperlink

I was wondering if it is possible to make a label a hyperlink? Below is the code for a column I have set up and I want to make "lblEmail" clickable so that the email opens up and with that email address in it. The idea is that as various users log in to the site, their unique info will appear in the column. Is it as simple as wrapping the label control in an anchor tag? If so, I must be missing something because I tried that. Since I am new, it is likely I missed something!
Thanks in advance!
<p>
<asp:Label ID="lblName" runat="server" Text=""></asp:Label> <br />
<asp:Label ID="lblPhoneNo" runat="server" Text=""></asp:Label> <br />
<asp:Label ID="lblAddress" runat="server" Text=""></asp:Label> <br />
<asp:Label ID="lblCity" runat="server" Text=""></asp:Label>, <asp:Label ID="lblState" runat="server" Text="Label"></asp:Label> <asp:Label ID="lblZipCode" runat="server" Text="Label"></asp:Label> <br />
<asp:Label ID="lblEmail" runat="server" Text=""></asp:Label> <br />
</p>
Instead of using a label, you could make use of an hyperlink control.
The usage is as follows:
<asp:HyperLink id="hyperlink1"
ImageUrl="images/pict.jpg"
NavigateUrl="http://www.microsoft.com"
Text="Microsoft Official Site"
Target="_new"
runat="server"/>
Why not just use a hyperlink server control?
<asp:Hyperlink runat="server" id="lnk1">Your Text Here</asp:Hyperlink>
If you need additional formatting, you can wrap it in other another tag (or insert any legal HTML tag within the hyperlink).
Instead of using a label like hyperlink way, you should better use directly a hyperlink. Try the following :
<asp:Hyperlink runat="server" id="email" NavigateUrl="your_desired_address">
EmailLabelContent here
</asp:Hyperlink>
Thanks.

Categories