I have a designer working at the ASPX level. He doesn't do C#, doesn't do code-behinds, doesn't compile, etc. He's a pure designer, working with HTML and server controls.
I need a conditional control -- an If...Then-ish type thing. Normally, I would do this:
<asp:Placeholder Visible='<%# DateTime.Now.Day == 1 %>' runat="server">
It's the first day of the month!
</asp:PlaceHolder>
Is there any way to do something like this without the databinding syntax? Something like:
<asp:If test="DateTime.Now.Day == 1" runat="server">
It's the first day of the month!
</asp:If>
Is there some kind of way to extend a placeholder to allow this? I've fiddled around a bit, but in the end, I have a conditional that I essentially have to compile.
Now, there's nothing wrong with the databinding syntax, but's just one more bit of...weirdness, the a designer is going to have to understand. Additionally, it doesn't give me "else" statements. Something like this would be great...
<asp:If test="DateTime.Now.Day == 1" runat="server">
It's the first day of the month!
<asp:Else>
It's not the first day of the month!
</asp:Else>
</asp:If>
Instead of writing control
asp:If
why not use:
<% if expression
{ %>
Yellow
<% } %>
<% else
{%>
Red
<% } %>
Taking into account that codebehind files are out as the designer probably not got VS, I think a simpler solution with less code may be more preferential:
<%# Page Language="C#"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
if (DateTime.Now.Day == 1)
{
DateTestPanelFirst.visible = true;
DateTestPanelOther.visible = false;
}
}
</script>
<html>
<body>
<asp:panel runat="server" id="DateTestPanelFirst" visible="false">
It's the first day of the month!
<asp:panel>
<asp:panel runat="server" id="DateTestPanelOther">
It's not the first day of the month!
<asp:panel>
</body>
</html>
the <asp:panel> could be changed to another type of web control, like <asp:label> etc. I think almost all dot net controls have the visible property, so you can hide/show them at any time.
The data-binding syntax has two problems: first it's a little weirder for your designer vs. using plain text, and second it requires the designer to remember to invert the "if" test in your "else" block.
The first problem may annoy your designer a little, but the second problem is much more severe, because it forces your designer to think like a programmer (inverting boolean logic!) and makes every if/else block into a possible bug you need to test for after your designer hands over a template.
My suggestion: use data-binding syntax, but fix the more severe problem by creating custom controls that only require data-binding test code on the If control, but not on the Else control. Sure your designers will have to type a few more characters, but the other more severe problems won't apply and your performance won't suffer as it would if you had to dynamically compile code each time your page ran.
Here's an example I coded up to illustrate:
<%# Page Language="C#"%>
<%# Register Assembly="ElseTest" TagPrefix="ElseTest" Namespace="ElseTest"%>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
DataBind();
}
</script>
<html>
<body>
<ElseTest:IfControl runat="server" visible="<%#1 == 1 %>">
This should be visible (in "if" area)
</ElseTest:IfControl>
<ElseTest:ElseControl runat="server">
This should not be visible (in "else" area)
</ElseTest:ElseControl>
<br /><br />
<ElseTest:IfControl runat="server" visible="<%#0 == 1 %>">
This should not be visible (in "if" area)
</ElseTest:IfControl>
<ElseTest:ElseControl runat="server">
This should be visible (in "else" area)
</ElseTest:ElseControl>
</body>
</html>
Here's the underlying controls, which are simply wrappers around asp:Literal:
using System;
using System.Web.UI;
using System.Web.UI.WebControls;
[assembly: TagPrefix("ElseTest", "ElseTest")]
namespace ElseTest
{
// simply renames a literal to allow blocks of data-bound-visibility
[ToolboxData("<{0}:IfControl runat=\"server\"></{0}:IfControl>")]
public class IfControl : Literal
{
}
[ToolboxData("<{0}:ElseControl runat=\"server\"></{0}:ElseControl>")]
public class ElseControl : Literal
{
public override bool Visible
{
get
{
// find previous control (which must be an IfControl).
// If it's visible, we're not (and vice versa)
for (int i = Parent.Controls.IndexOf(this)-1; i >= 0; i--)
{
Control c = Parent.Controls[i];
if (c is IfControl)
return !c.Visible; // found it! render children if the if control is not visible
else if (c is Literal)
{
// literals with only whitespace are OK. everything else is an error between if and then
Literal l = c as Literal;
string s = l.Text.Trim();
if (s.Length > 0)
throw new ArgumentException("ElseControl must be immediately after an IfControl");
}
}
throw new ArgumentException("ElseControl must be immediately after an IfControl");
}
set
{
throw new ArgumentException("Visible property of an ElseControl is read-only");
}
}
}
}
If you want it more concise, you can easily shorten the tag name (by changing the class names and/or tag prefix). You can also create a new property (e.g. "test") to use instead of "Visible".
If you really want to get rid of the <%# %>, there are likley many different tricks you can use to leverage CodeDOM or other ways to dynamically compile code, although performance will be a challenge since you'll probably end up dynamically compiling code each time the page runs, it may introduce pesky security issues, and more. I'd stay away from that.
Related
I am new to ASP.NET and user controls. I am trying to generate a javascript array from my C# code.
On the main .aspx page I have this:
<%# Page Language="C#" AutoEventWireup="true" CodeFile="main.aspx.cs" Inherits="main" %>
<%# Register Src="~/table.ascx" TagPrefix="uc1" TagName="myTable" %>
Then on my table.asc.cs I have this:
protected void Page_Load(object sender, EventArgs e)
{
(...)
this.LoadDataFromDB();
(...)
}
private void LoadDataFromDB()
{
(...)
Response.Write(array);
(...)
}
My problem is that the array is being written before the <html> tags. It still works fine, but, how could I put it inside the <head> tags for instance?
Thank you
UPDATE:
I added this to my main.aspx
<asp:Literal ID="Literalarray" runat="server" Mode="PassThrough" Text="" />
and this to my ascx.cs:
Literal Literalarray= new Literal();
Literalarray.Text = output;
What am I missing?
Use a Literal control instead of Response.Write. Place it on your control somewhere and set its Text property.
You have to place it on your control, not on your page and you don't need to reinitalize it.
This code in the ascx.cs:
Literal Literalarray= new Literal();
Literalarray.Text = output;
should be:
Literalarray.Text = output;
Which should be in the Page_Load as a designer file will declare the literal type and allocate the space for it. By declaring a new one, the old one may be hidden. Also, be aware that if you are generating a JavaScript array that you also generate the script tags as part of the output as a literal doesn't do much decorating around the result.
I'd probably suggest putting a literal in the head on the main.aspx and load the data in there that way for one idea.
You could also do dynamic controls so that in the table.ascx.cs you create a Literal like you did previously and then add that to the head of the page assuming the head tag has a "runat=server" attribute so the code behind can use it. I'm pretty sure that in the code behind for the table you could do something like this:
Literal Literalarray= new Literal();
Literalarray.Text = output;
this.Page.head.AddControl(Literalarray);
I have a problem and I can't figure it out how to solve it. I search for solutions but they did not work. So, I have a Datalist with ItemTemplate. I need to add google analytics on onclick event to <a> tags. I tried to add the onclick event like
onclick="_gaq.push(['_trackEvent', 'Homepage', 'Click on',<%#DataBinder.Eval(Container.DataItem,"URL")%>']);" <br />
but I get a yellow error screen with the message "..tag is not formatted correctly".
I also tryed replacing double qoutes with &qout; but no success. I also tried
onclick='<%# string.Format("_gaq.push(['_trackEvent','Homepage','Click on last awarded company','{0}']);", DataBinder.Eval(Container.DataItem, "URL") %>' <br />
but this also not worked.
Have you got any idea how could I solve this problem?
You really should do this kind of complex databinding in the "OnItemDataBound" event in the code behind. Have a look at the relevant MSDN page.
<asp:DataList id="ItemsList" OnItemDataBound="Item_Bound" runat="server">
Code Behind:
public void Item_Bound(object sender, DataListItemEventArgs e)
{
if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
// find your link
HyperLink link = (HyperLink)e.Item.FindControl("MyFirstHyperLink");
// so something nice with your link here, for example add attributes.
string a = DataBinder.Eval(e.Item, "URL", "_gaq.push(['_trackEvent','Homepage','Click on last awarded company','{0}']);");
link.Attributes.Add("onclick", a);
}
}
disclaimer: I haven't actually tested this code so you might need to make adjustments here and there. It merely serves to give you an idea of the direction to go.
Could you please try below?
Test
I've met the following scenario: I had to reuse some function that returns a collection of dynamic objects, from other assembly. Generally, there is a possibility to publish the dynamic object to other assembly, by using [assembly: InternalsVisibleTo("Some.Assembly")], because the dynamic objects are internal to their assembly. Not having the choice, I've tried the item data binding method solution to add the script, but dynamic objects are not accessible there, even if I used Eval. But Eval works in the markup, and there the quote/apostrophe problem occurs. My solution was HTML escaping:
onclick='<%# "doSomething('" + Eval("DataProperty") + "', this);"%>'
I've been poking at this all morning and I can't seem to find the way to do this.
I have a web app that has to show a tooltip on one of its div-s. The tooltip's contents come from some DB querying
< div style="height: 10px; clear: both;" title="<%=dbCount %> device(s) with version <%=devVersion %> in this group">
This works fine.
If I try replacing the <%=dbCount %> with an expression:
< div style="height: 10px; clear: both;" title="<%=dbCount > 0 ? ""+dbCount : "No " %> device(s) with version <%=devVersion %> in this group">
then on PageLoad I get an exception CS1518: Expected class, delegate, enum.
It doesn't matter how I structure the expression, if I put parenthesis or not, if I use String.Format or ternary expression - any sort of expression, besides using the variable name only, causes the error.
I tried replacing <%= with <%# and tried <% Response.Write(dbCount>0 ? "some" : "none") %> and I get the same error. This is the only line in the aspx I'm editing so the error is due to it, not elsewhere on the page.
I could use an <% if(...){ construct but then the designer is having trouble with finding the closing div and I don't want to pollute the source with too much junk, I'd rather keep the original version.
Do you know why is the compiler error showing up and how can I prevent it and use the output expression <%= devCount>0?"some":"none" %>?
Do not write code like that.
Aspx files only support that for backward compatibility.
If you really really must do it, Write only public properties that way.
Refer: Embeded code blocks
Embedded code blocks are supported in ASP.NET Web pages primarily to
preserve backward compatibility with older ASP technology. In general,
using embedded code blocks for complex programming logic is not a best
practice, because when the code is mixed on the page with markup, it
can be difficult to debug and maintain. In addition, because the code
is executed only during the page's render phase, you have
substantially less flexibility than with code-behind or script-block
code in scoping your code to the appropriate stage of page processing.
That said, the link does show you how to properly use embeded code.
If you really must do it this way, use Response.Write.
< div style="height: 10px; clear: both;" title="<%
{
string countMessage = dbCount > 0 ? ""+dbCount : "No ";
Response.Write(countMessage );
}
%> device(s) with version <%=devVersion %> in this group">
I've created a test web app and your code (<%=dbCount > 0 ? ""+dbCount : "No " %>) works fine (I'm assuming that "< div" is a mistype). I'm assuming that dbCount is an int and devVersion is a string. Is there something else going on? Is dbCount a public property that calls a method? Is it a public variable?
What is dbCount in your app?
All these variants are running ok here, among the C# samples I include the case you mentioned which is also running fine:
In VB.NET
MarkUp:
<body>
<form id="form1" runat="server">
<div id="<%If dbCount = 1 Then%><%="22"%><%Else%><%="55"%><% End If%>">
<%=dbCount.ToString()%>
</div>
</form>
</body>
Codebehind:
Partial Class varIn
Inherits System.Web.UI.Page
Public dbCount As Short = 0
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
dbCount = 1
End If
End Sub
End Class
In C#
MarkUp:
<div id="<%if (dbCount == 1) { %><%="22"%><%;}else{%><%="55"%><%;}%>">
<%=dbCount.ToString()%>
</div>
Or:
<div id="<%=dbCount > 0 ? ""+dbCount : "No " %>">
<%=dbCount.ToString()%>
</div>
Codebehind:
public partial class _Default : System.Web.UI.Page
{
public short dbCount;
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
dbCount = 1;
}
}
}
i'm seeing a really strange issue with asp.net rendering. i have EXACTLY this in the relevant part of the .aspx (just replaced names of paths and controls):
<div id="header">
<% if (SiteSettings.SiteName.Equals("sx") || SiteSettings.SiteName.Equals("sw"))
{ %>
<sc:sublayout runat="server" renderingid="{B04CFA1A-6B5B-49D3-8000-339DBE9899C1}"
path="/layouts/AX/HeaderSublayout.ascx" id="AXHeader" placeholder="content"></sc:sublayout>
<% }
else
{ %><!-- bla1 --><ax:strangeBehavingControl id="HeaderInclude" runat="server" IncludeType="Header" /><!-- bla2 -->
<% } %>
</div>
the rendered html looks like:
<!-- bla1 -->
""
expected content from strangeBehavingControl
<!-- bla2 -->
the .ascx for strangeBehavingControl is really simple:
<%# Control Language="C#" AutoEventWireup="true" CodeBehind="strangeBehavingControl.ascx.cs" Inherits="layouts.strangeBehavingControl" %>
no extra spaces anywhere, checked already many times. the code behind is also really simple:
public partial class strangeBehavingControl: System.Web.UI.UserControl
{
protected override void Render(HtmlTextWriter writer)
{
var filePath = GetFilePath();
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(Server.MapPath(filePath)))
Response.WriteFile(filePath);
}
}
so i was thinking that the strange "" where inside the rendered included files, but i checked them manually and they start with the expected characters. any idea how can those characters being inserted there?
You are generating markup for your control incorrectly. Your render method should be using the HtmlTextWriter instance given to it, and not using any direct output writing method on Response.
It also looks like a rather strange setup, as you are writing out the contents as a UserControl, meaning it will be rendered within a page. Is it correct to assume you are always outputting either valid HTML or plain text?
I would suggest you change your Render method as follows:
protected override void Render(HtmlTextWriter writer)
{
var filePath = GetFilePath();
if (!string.IsNullOrWhiteSpace(filePath) && File.Exists(Server.MapPath(filePath)))
using (var sr = new StreamReader(filePath))
writer.Write(sr.ReadToEnd());
}
Sometimes the control file has another charset.
Verify that your file does not have extra characters at the start, and if so remove them.
You may have to use a smart text editor that allows you to do that.
I had the same extra space problem, but my control was not implementing a Render() method.
I'm sure I'm going to have to write supporting javascript code to do this. I have an autocomplete extender set up that selects values from a database table, when a selection is made, i would like it to set the ID of the value selected to a hidden control. I can do that by handling a value change on the text box and making a select call to the database, Select idCompany from Companies Where CompanyName = "the text box value";
The most important thing is to constrain the values of the text box that is the targetcontrol for the autocomplete extender to ONLY use values from the autocomplete drop down. Is this possible with that control, is there examples somewhere? is there a better control to use (within the ajax control toolkit or standard .net framework - not a third party control)?
I'm going to be trying to work out some javascript, but I'll be checking back to this question to see if anyone has some useful links. I've been googling this last night for quite a while.
Update: I did not get an answer or any useful links, I've posted an almost acceptable user control that does what I want, with a few workable issues.
No one was able to give me an answer. This has been an ongoing saga. It started when I was trying to find a solution not using drop down lists for large amounts of data. I have run into issues with this so many times in previous projects. This seems to be workable code. Now I need to know how to supply a AutoPostBack Property, and allow for some events, such as SelectedValueChanged. And due to the javascript, it will conflict with another control, if I have more than one of them on the same page. Well, that's some of the known issues I'm looking at with the code, but it's a start and definately better than looking at a hung browser for 3 or 4 minutes while the drop down is loading 30k list items.
This code is assuming there is an asmx file with the script methods GetCompanyListBySearchString and GetCompanyIDByCompanyName.
ASPX FILE
<%# Control Language="C#" AutoEventWireup="true" CodeFile="SelectCompany.ascx.cs" Inherits="Controls_SelectCompany" %>
<%# Register TagPrefix="ajaxToolkit" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>
<script language="javascript" type="text/javascript">
var txtCompanyIDHiddenField = '<%= fldCompanyID.ClientID %>';
var txtCompanyIDTextBox = '<%= txtCompany.ClientID %>';
function getCompanyID() {
if (document.getElementById(txtCompanyIDTextBox).value != "")
CompanyService.GetCompanyIDByCompanyName(document.getElementById(txtCompanyIDTextBox).value, onCompanyIDSuccess, onCompanyIDFail);
}
function onCompanyIDSuccess(sender, e) {
if (sender == -1)
document.getElementById(txtCompanyIDTextBox).value = "";
document.getElementById(txtCompanyIDHiddenField).value = sender;
}
function onCompanyIDFail(sender, e) {
document.getElementById(txtCompanyIDTextBox).value = "";
document.getElementById(txtCompanyIDHiddenField).value = "-1";
}
function onCompanySelected() {
document.getElementById(txtCompanyIDTextBox).blur();
}
</script>
<asp:TextBox ID="txtCompany" runat="server" onblur='getCompanyID()'
/><ajaxToolkit:AutoCompleteExtender runat="server" ID="aceCompany" CompletionInterval="1000" CompletionSetCount="10"
MinimumPrefixLength="2" ServicePath="~/Company/CompanyService.asmx" ServiceMethod="GetCompanyListBySearchString"
OnClientItemSelected="onCompanySelected" TargetControlID="txtCompany" />
<asp:HiddenField ID="fldCompanyID" runat="server" Value="0" />
CODE BEHIND
[System.ComponentModel.DefaultProperty("Text")]
[ValidationProperty("Text")]
public partial class ApplicationControls_SelectCompany : System.Web.UI.UserControl
{
public string Text
{
get { return txtCompany.Text; }
set
{
txtCompany.Text = value;
//this should probably be read only and set the value based off of ID to
// make certain this is a valid Company
}
}
public int CompanyID
{
get
{
int ret = -1; Int32.TryParse(fldCompanyID.Value, out ret); return ret;
}
set
{
fldCompanyID.Value = value.ToString();
//Todo: should set code to set the Text based on the ID to keep things straight
}
}
}
Thanks for your post here. It is useful, however, it is assuming that everyone knows the setup to get the webservice called by a javascript function.
Sorry to be soo newbie, but I couldn't get the webservice called from client-side.
I read this documentation: http://msdn.microsoft.com/en-us/magazine/cc163499.aspx
Furthermore, I found an interesting post that explains how to create/get a name value pair which is pretty much what you are expecting as far as I understood:
http://blogs.msdn.com/phaniraj/archive/2007/06/19/how-to-use-a-key-value-pair-in-your-autocompleteextender.aspx
Sorry if I misunderstood you, but I am just trying to guide other people that pass through the same situation.
Thanks a lot.
You can check the value of the selection by trapping on ClientItemSelected event and ensure that it is not blank.