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?
Related
The code I have (below) works except for when I add the ref="abc" to the TemplateItem tag. When it is included I get this error:
Property 'TemplateItem' does not have a property named 'ref'
How do I resolve this issue?
Default.aspx
<%# Register Src="~/message.ascx" TagName="Message" TagPrefix="uc" %>
<uc:Message ID="msg" runat="server" abc="123" >
<TemplateItem ref="abc">Hi</TemplateItem>
</uc:Message>
message.ascx
<asp:placeholder runat="server" id="PlaceHolder1" />
message.ascx.vb
Partial Class message
Inherits System.Web.UI.UserControl
Public Property abc() As String
Sub Page_Init()
If TemplateItem IsNot Nothing Then
TemplateItem.InstantiateIn(PlaceHolder1)
End If
End Sub
Private m_TemplateItem As ITemplate = Nothing
<TemplateContainer(GetType(TemplateItem2))> _
Public Property TemplateItem() As ITemplate
Get
Return m_TemplateItem
End Get
Set(ByVal value As ITemplate)
m_TemplateItem = value
End Set
End Property
Public Class TemplateItem2
Inherits Control
Implements INamingContainer
Public Property ref() As String
End Class
End Class
Are you sure it should be:
Public Class TemplateItem2
at the bottom there, and not just the following?:
Public Class TemplateItem
(If you just want a different name from the property, maybe something like TemplateItemImplementation or just TemplateItemImpl would be more clear?)
Also, I'm not sure how this works:
TemplateItem.InstantiateIn(PlaceHolder1)
The MSDN page on CompiledTemplate.InstantiateIn() says: This API supports the .NET Framework infrastructure and is not intended to be used directly from your code. I guess this is a sidenote though, and I'm assuming you've gotten it to work the way you want...
I think the problem is related to the instantiation of TemplateItem in that line though; I can not see how TemplateItem2 (which contains the property ref()) is related to ITemplateItem. Maybe you just need to make TemplateItem2 also implement ITemplateItem?
Another thing to try is to make ref() a property of ITemplateItem, if possible. Maybe that Interface becomes the Type of <TemplateItem />, which would explain why it does not contain the property Ref?.
(That is assuming the type is specified by the return type of the property TemplateItem()).
So i am currently adding a collection of usercontrols to a Panel Collection.
Here is that code
foreach (IssuePoll poll in issuePollList)
{
IssuePollsUC issuePoll = (IssuePollsUC)Page.LoadControl("~/UserControls/IssuePollsUC.ascx");
issuePoll.LoadPoll(poll, false, politician.PoliticianID);
pnlUnFinishedTest.Controls.Add(issuePoll);
}
I am trying to get those usercontrols so i can call a validate method and save method inside each of those controls. Here is the code i am using for that, but it is not working.
foreach (Control control in pnlUnFinishedTest.Controls)
{
IssuePollsUC issuePolls = (IssuePollsUC)control;
issuePolls.SavePollAnswer(appUser.User.PersonID);
}
I get an error message on the convert, it says
"Unable to cast object of type 'System.Web.UI.LiteralControl' to type 'UserControls.IssuePollsUC'"
EDIT: Looks like the problem lies in the fact that a Control cannot be convert into (User Control)
There are other controls in your panel other than your user control i.e. the literal that is causing the cast to fail. Try
foreach (Control control in pnlUnFinishedTest.Controls)
{
IssuePollsUC issuePolls = control as IssuePollsUC;
if(issuePolls != null)
{
issuePolls.SavePollAnswer(appUser.User.PersonID);
}
}
This will make it more type safe.
EDIT
Please note that you must add dynamic controls in the Page_Init event not Page_Load or anywhere else. I suspect your controls aren't even there - adding them not in Page_Init means that that they are not in ViewState and won't be present in any control collection.
I have a lable I want to update on a web form page from another class. I created an instance of that class then referenced a public method to do the update since I was unable to access the lable control directly. But when it reaches the public method it seems as if the controls were initialized and are not recognizable.
*...' calling class*
dim kws as new form2class
kws.setErrorLable("ERROR FOUND" & ex.message.tostring)
.....
called class
public sub serrorLable(Byval msg as string)
label10.text=msg
end sub
Am I missing something here? Thanks in advance
You can access the current page like this:
Page currentPage = (Page)HttpContext.Current.Handler;
Depending on where the label is in the page, you may need to use recursion to find the Label, but the above code will get you to the current page.
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.
When a controller renders a view based on a model you can get the properties from the ViewData collection using the indexer (ie. ViewData["Property"]). However, I have a shared user control that I tried to call using the following:
return View("Message", new { DisplayMessage = "This is a test" });
and on my Message control I had this:
<%= ViewData["DisplayMessage"] %>
I would think this would render the DisplayMessage correctly, however, null is being returned. After a heavy dose of tinkering around, I finally created a "MessageData" class in order to strongly type my user control:
public class MessageControl : ViewUserControl<MessageData>
and now this call works:
return View("Message", new MessageData() { DisplayMessage = "This is a test" });
and can be displayed like this:
<%= ViewData.Model.DisplayMessage %>
Why wouldn't the DisplayMessage property be added to the ViewData (ie. ViewData["DisplayMessage"]) collection without strong typing the user control? Is this by design? Wouldn't it make sense that ViewData would contain a key for "DisplayMessage"?
The method
ViewData.Eval("DisplayMessage")
should work for you.
Of course after I create this question I immediately find the answer after a few more searches on Google
http://forums.asp.net/t/1197059.aspx
Apparently this happens because of the wrapper class. Even so, it seems like any property passed should get added to the ViewData collection by default.
I really need to stop answering my own questions :(