Setting 'codebehind' properties from within markup on ASP.NET Web Forms - c#

Placing the following code inside the 'markup' section on a web form does not work
Have I messed the syntax up or is something like this not possible on 'server side' controls?
<asp:TextBox runat="server" ID="txt" Text='<%#System.Configuration.ConfigurationManager.AppSettings["foo"] %>' />

Use $ expression.
<asp:TextBox
runat="server"
ID="txt"
Text='<%$ AppSettings: foo %>'

Related

ASP.NET is not calling C# code

For some reason this following ASP code is not calling the c# method "checkLastNames" that is suppose to be evaluated in the 'Visible=' field below.
<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%# checkLastNames("Z") %>' />
When I enter debug mode the method isn't even being called. Visible just defaults to true.
I've tried changing the method to return only false just to see if it would work but "Visible" is still defaulting to true.
protected bool checkLastNames(string s){
return false;
}
<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%# checkLastNames("Z") %>' />
That # means it's only evaulated during a databind operation. So if you are not databinding the page explicitly (through calling DataBind()) then this won't show.
Visible='<%= checkLastNames("Z") %>' />
You might want to try the code above. Also I would probably put this in a static function (assuming it's functionality is encapsulated in there)
<%# is for databinding expressions, so this works only if the namingcontainer control of this Button is databound.
For example in Page_Load:
this.DataBind();
But why not using codebehind in the first place?
btnZ.Visible = checkLastNames("Z");
Try like this
<asp:Button ID="btnZ"
runat="server"
Text="Z"
Height="20px"
Width="25px"
Font-Size="12px"
CommandArgument="Z"
OnClick="btnA_Click"
Visible='<%= checkLastNames("Z") %>' />

Images folder pictures in eval repeater

When I use photos without any folder, <asp:Image ID="Image1" ImageUrl='<%# Eval("PresidentPhotoPath") %>' runat="server" /> , it works.
However, when I use the photos under Images folder, the pictures are not shown and I get a blank screen only. Here is the code I'm using:
<div class="wrapper">
<asp:Repeater runat="server" ID="Repeater1">
<ItemTemplate>
<asp:Image ID="Image1"
ImageUrl='"/Images" + <%# Eval("PresidentPhotoPath") %>'
runat="server" />
</ItemTemplate>
</asp:Repeater>
</div>
Change asp:Image to code bellow:
<asp:Image ID="Image1"
ImageUrl='<%# string.Format("~/Images/{0}", Eval("PresidentPhotoPath")) %>'
runat="server" />
I would recommend using a code-behind method to build the string for you, like this:
protected string BuildPath(string photoPath)
{
return "Images/ + photoPath;
}
Note: Consider naming this something more useful than BuildPath as that is fairly generic, just picked that name because nothing better came to mind immediately.
Now in your markup you can just call the method, like this:
ImageUrl='<%# BuildPath(Eval("PresidentPhotoPath")) %>'
I recommend this approach for the following reasons:
The markup does not contain any complex logic whatsoever, just a method call with the Eval() value
It is easier to debug the logic versus embedded code blocks
You can leverage the power of Visual Studio compiler to catch syntax errors at compile-time versus run-time errors when the logic is embedded into the binding syntax of the markup

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.

Inline Code in markup page in ASP.NET Webforms?

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()%>' />

Accessing code behind property in server tags

Is it possible to access a code behind property for a server tag?
I have a property on my code behind page that i want to pass into a javascript function i.e.
<asp:RadioButton onclick="moveToNextPage()" class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
So i want to be able to get the variable out of my code behind page and pass it to the "moveToNextPage()" function. Is this possible?
If your code behind value is public or protected then you can try this (assuming your variable is called example):
<asp:RadioButton onclick='<%= string.Format("moveToNextPage({0})", example) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
<asp:RadioButton onclick='<%= string.Format("moveToNextPage({0})", yourproperty) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
Make sure your yourproperty is public
<asp:RadioButton onclick='<%# string.Format("moveToNextPage({0})", yourproperty) %>' class="inputcell" Runat="server" ID="myRadioButton" Text="No"></asp:RadioButton>
Important change would be <%# instead of <%=. In order for this to work, you need to call myRadioButton.DataBind() somewhere in code behind

Categories