i have a line of code that is throwing a Specified cast is not valid exception:
<%# DecimalToStringUsingUserLocale( (Decimal)Eval("HoursBooked") ) %>
i want to have a breakpoint on this line, so i can inspect the value of:
Eval("HoursBooked")
to see what type the underlying Object is. How can i do this?
How can i put a breakpoint on a databinding expression line?
Workaround
Move presentation from the view to the controller:
<%# HoursBookedToStr(Eval("HoursBooked")) %>
and then in the code-behind file:
protected string HoursBookedToStr(Object objectFromEval)
{
Decimal value = (Decimal)objectFromEval;
return DecimalToStringUsingUserLocale(value);
}
and now i can put a breakpoint on the line:
Decimal value = (Decimal)objectFromEval;
Note: Now i can solve my problem because i see the object is not of type Decimal, as i thought, but instead is of type System.DBNull
How can i put a breakpoint on a databinding expression line?
Note: Don't confuse the example with the question: placing breakpoints on databinding.
Related
I saw a piece of ASP code that looks as follows:
<% rs("Server.HTMLEncode(x)") %>
Since I doubt that the recordset has a field named "Server.HTMLEncode(x)" I was wondering if they meant to write:
<% Server.HTMLEncode(rs(x)) %>
Or if classic ASP does reflection and somehow interprets that string literal as a method call.
I used classic ASP for ten years, and I never encountered any kind of reflection. Based on my past experience, I would agree with you that they probably meant to write:
<% Server.HTMLEncode(rs(x)) %>
But, it still does not look correct. Did they mean to write out the value using Response.Write? I can't tell without seeing more of the code.
Looks like bad coding to me.
Classic ASP can be written with VBScript which does allow kind of reflection by using Eval() and Execute() methods. For example:
Class Foo
Public Bar
End Class
Dim oFoo, propName, myValue
Set oFoo = New Foo
oFoo.Bar = "hello world"
propName = "Bar"
myValue = Eval("oFoo." & propName)
Response.Write("Value of " & propName & ":" & myValue)
However, that's not the case with your sample code. This is just a string literal passed to the Recordset object which looks for exact match. If the given parameter is a number it will return value of field having such index otherwise it expects a string which is exact field name.
Assuming x is user input that should be a field name or index, they probably meant:
<%=rs(Server.HTMLEncode(x)) %>
Im trying to pass a value of a date control from form1 to form2.
on form 1.aspx.vb:
Public ReadOnly Property Property1() As Date
Get
Return StartDate.SelectedDate
End Get
End Property
On Form2.aspx:
<%# PreviousPageType VirtualPath="~/form1.aspx" %>
On form2.aspx.vb:
Label14.Text = PreviousPage.Property1
when I run it, the compiler gives me an error:
"Object reference not set to an instance of an object."
with marking in red:
Label14.Text = PreviousPage.Property1
Tried to assign the property to a string, it did not work either.
Any suggestions ???
Regards.
When the Form2.aspx page is accessed directly without cross-page posting, then PreviousPage property is null. You should add this check before retrieving value of Property1:
if (PreviousPage != null && PreviousPage.IsCrossPagePostBack) {
Label14.Text = PreviousPage.Property1
}
Using PreviousPageType VirtualPath="~/form1.aspx" directive is a little bit dangerous when other page than Form1.aspx do cross-page post to Form2. PreviousPage property throws InvalidCastException (it expects Form1 page but it gets something else).
For more information see: http://msdn.microsoft.com/en-us/library/ms178139.aspx.
I think Startdate isn't declared / initialized, how do you set the data for this readonly property?
Is there a better way to write the code below? I have quite a few blocks that are similar, and this is making the code in the Viewpage very messy to work with.
The data value with the associated label only needs to be output when certain conditions are met, which is almost always if the value is not null.
The options I can think is to use a response.write to atleast minimize the usage of the ASP script tags, or to format the webpage is such a way that the label displays with an appropriate n/a type value.
<% if (myData.Balance != null)
{ %>
Balance: <%= String.Format("{0:C}", (myData.Balance))%>
<% } %>
If you make use of the DisplayFormatAttribute class in System.ComponentModel.DataAnnotations you can explicitly control the output of null values in your view without dealing with inline script tags. By itself that won't help you remove the labels tied to the value, but you can at least have it automatically substitute an output if the value is null.
[DisplayFormat(NullDisplayText = "N/A", DataFormatString = "{0:c}")]
public double? Price { get; set; }
<%=Html.DisplayFor(m => m.Price)%>
With the above code it will automatically display "N/A" if the value is null, otherwise it will display the value using the default currency format.
As an alternative, if you want to remove the label too and don't want to deal with script tags in your view you could make your own HtmlHelper which takes an expression in the same format of Html.DisplayFor(expression) and then returns the combined output of an Html.LabelFor(expression) and Html.DisplayFor(expression) if and only if the value mapped to that expression is not null.
If you stick the "Balance" inside the format string, and use Response.Write, it ends up looking a lot cleaner, I think:
<% if (myData.Balance != null)
Response.Write(String.Format("Balance: {0:C}", myData.Balance)) %>
I use ASP.NET MVC for serving web application and I want to create something like the following code.
<% using(HTML.Form(Model)) { %>
<% HTML.CreateTextBox('txt1', x => x.Property1);
<% }
From the above code, Form extension method will receive object that represent type of current Model object in current View page. Next, CreateTextBox method will receive type from Form method and I can bind textbox to some property of this model.
Update 1
The following code is code of CreateTextBox method that will create instance of TextBox class.
public static CreateTextBox(string value, Expression<Func<object>> bindedProperty)
{
// T should be receive from HTML.Form method or class
return new TextBox<T>(value);
}
Is it possible to creating some code that doing something like the above code?
Thanks,
It's not necessary. The model is always going to be available, so you will always be able to do this:
<%
using(Html.BeginForm())
{
Html.TextBox('txt1', Model.Property1);
}
%>
The Model property on your page is always typed (assuming you are using System.Web.Mvc.ViewPage<T>) so you will always have access to the properties directly through the Model property.
If you aren't using the generic version (System.Web.Mvc.ViewPage<T>) then you should probably use that instead of the non-generic version.
The below code works but I am not sure how?
OnClientClick='<%# CreateConfirmation(Eval("EventName"),DataBinder.Eval(Container.DataItem, "EventDate", "{0:ddd, d MMM}")) %>'
Public Function CreateConfirmation(ByVal EventName As String, ByVal EventDate As String) As String
Return String.Format("return confirm('Are you sure you wish to register for {0} on {1} ?');", EventName, EventDate)
End Function
I have read that <%# %> is a databinding expression, but overhere we are not directly data-bidning (infact returining value from the function CreateConfirmation) and I also thought that it should work with <%= %> but it gives JavaScript error message i.e. Illigal XML character
pointing to =
Please could you clarify as to why is this?
Many Thanks.
This question is very precisely answered in an exisiting post:
Why will <%= %> expressions as property values on a server-controls lead to a compile errors?
You can call any code inside the <%#. The Eval bit is the piece that makes it relate to the row/object in the datasource.
You are still binding the returned string to the property. You can only use <%=%> with inline HTML. You have to use <%#%> when binding to a contols property.