I have develop small Asp.net MVC3 application using Telerik rad Controls with in that i have try to get the list of data for that i have create strong type view control,it is create all the controls and i run the application it showing error like Object Reference null Exception
in my database i have 10 records then why it shoeing the error please help me ...here i have post my code please refer this once.
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<TelerikMvcApplication1.Models.tb1_post>" %>
<asp:Content ID="Content1" ContentPlaceHolderID="TitleContent" runat="server">
Index
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Index</h2>
<fieldset>
<legend>tb1_post</legend>
<div class="display-label">post</div>
<div class="display-field"><%: Model.post %></div>
<div class="display-label">postdate</div>
<div class="display-field"><%: String.Format("{0:g}", Model.postdate) %></div>
<div class="display-label">username</div>
<div class="display-field"><%:Model.username %></div>
</fieldset>
<p>
<%: Html.ActionLink("Edit", "Edit", new { id=Model.postid }) %> |
<%: Html.ActionLink("Back to List", "Index") %>
</p>
</asp:Content>
The error is happening at the first reference to the model, therefore the Model is null. Make sure that the MVC controller is properly instantiating the model for the view.
Related
I've see all of the other pages with this questions, but no amount of research has solved my problem. I am getting the "The state information is invalid for this page and might be corrupted" error. My page loads fine the first time. The page contains a div that has tabs that change the info on the page and the tabs are loaded dynamically and are linked to a different page. The tabing uses jquery. Without changing anything on the page, when I try to reload this page by click on the link that gets to this page or even by clicking on ANY link in the menu bar that goes to other pages, I this error. Again, this only happens in IE and I've put break points all over my code and it never goes back to the server.
<%# Page Language="C#" MasterPageFile="~/Templates/Main.Master" AutoEventWireup="true"
CodeBehind="Reporting.aspx.cs" Inherits="Pegged.Reporting" Title="Workforce Planning Report" %>
<%# Register Src="Controls/ucWebPageNavigation.ascx" TagName="ucWebPageNavigation"
TagPrefix="uc1" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
<script type="text/javascript">
$(function () {
$("#tabs").tabs({
ajaxOptions: {
error: function (xhr, status, index, anchor) {
$(anchor.hash).html(
"Couldn't load this tab. We'll try to fix this as soon as possible.");
}
}
});
});
</script>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="IconLinks" class="TopLinks">
<uc1:ucWebPageNavigation ID="ucWebPageNavigation1" runat="server" />
</div>
<div id="divContainerReport" class="container_12">
<!--Facility Tabs-->
<div id="tabs">
<ul id="FacilityTabs" runat="server" ></ul></div>
</div>
</asp:Content>
This is the code that loads the tabs on page load:
ServicesDLL.Services ps = new ServicesDLL.Services();
string encryptString = "";
string encryptString2 = "";
encryptString = ps.EncryptQueryString("Test");
encryptString2 = ps.EncryptQueryString("Testing");
string innerhtml = "<li><a id=\"id1\" href=\"ReportFacility.aspx?facility=" + encryptString + "\">Test</a></li><li><a id=\"id2\" href=\"ReportFacility.aspx?facility=" + encryptString2 + "\">Testing</a></li>";
FacilityTabs.InnerHtml = innerhtml;
And this is the page the tabs load:
<%# Page Title="" Language="C#" MasterPageFile="~/Templates/Popup.Master" AutoEventWireup="true" CodeBehind="ReportFacility.aspx.cs" Inherits="Pegged.ReportFacility" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
<div id="divReportView" class="grid_8 BlueBorder">
<p id="testText" runat="server"></p>
</div>
</asp:Content>
I'm on VS 2008, Windows Xp.
My page:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<%= WriteReport()%>
<%= "Hellooo" %>
</asp:Content>
Code-behind:
public string WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
return swriter.ToString();
}
The funny thing is the "Heloo" goes in the contentPlace, but the method's return does not. It's place below the div of the ContentPlace (ends up in the footer div).
Thank you for your help.
EDIT----------------------
Temporary solution
<%= WriteReport()%>
<asp:Table runat="server" ID="testeme">
</asp:Table>
I added a bogus table (id=testeme), it does nothing and has nothing in it. But it works now, what the hell. Only works when below the method.
EDIT2--------------------
I tried to guess the relevant parts from the MasterPage and placed them here.
<%# Master Language="C#" AutoEventWireup="true" CodeBehind="MasterPage.master.cs" Inherits="MyAppV2.Views.MasterPage" %>
<div id="general">
<form id="form1" runat="server" enctype="multipart/form-data">
<div id="main">
<div>
<asp:contentplaceholder id="ContentPlaceHolder1" runat="server">
</asp:contentplaceholder>
</div>
</div>
</form>
</div>
<div id="footer">
<p>MYFOOTER</p>
</div>
First line of the view using the master page:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="MP5._Default" MasterPageFile="~/MasterPage.Master" %>
Instead of injecting code into your page using <%= ... %>, you can use an <asp:literal> tag:
<asp:Content runat="server" ContentPlaceHolderID="ContentPlaceHolder1">
<asp:Literal id="MyReport" runat="server" />
</asp:Content>
Then you can write to it from your code-behind using the text property. You can call the WriteReport() method from within Page_Load.
public void WriteReport()
{
StringWriter swriter = new StringWriter();
using (HtmlTextWriter hwriter = new HtmlTextWriter(swriter))
{
//Write a table...
}
MyReport.Text = swriter.ToString();
}
That should solve any oddities of where the code ends up on the page.
I am trying to execute the following code in a .aspx page:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
{ %>
<div class="resp">
<div class="top">
</div>
<div class="cont-resp">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% }
else
{%>
<div class="usuario">
<div class="top">
</div>
<div class="cont-usuario">
<h3>
<%# Eval("txt_nome") %></h3>
<p>
<%# Eval("txt_comentario") %></p>
</div>
</div>
<% } %>
</ItemTemplate>
</asp:Repeater>
It throws a runtime exception in the first line:
<% if (Convert.ToInt32(Eval("int_tipo")) == 1)
System.InvalidOperationException: Databinding methods such as Eval(), XPath() and Bind() can only be used in the context of a databound control.
What's wrong? Any ideas?
I had a similar problem and the following code worked for me:
<asp:Repeater ID="rptComentarios" runat="server">
<ItemTemplate>
<asp:PlaceHolder ID="placeholderBlaBlaBla" runat="server" Visible='<%# Convert.ToInt32(Eval("int_tipo")) == 1 %>'>
Your optional HTML
</asp:placeholder>
Other HTML
</ItemTemplate>
</asp:Repeater>
Some more comments:
Please note that single quotes are used to define the value Visible attribute of asp:placeholder. I tried double quotes too and they didn't work.
Anytime you want to get some optionally displayed HTML you should use a control to show/hide it. asp:placeholder works fine for that purpose. Don't ever do <% if(..) { %> - this is evil.
<%# ... %> is used to calculate or display expressions inside a repeater. These expressions can be displayed as HTML or passed as attributes of server side controls. You can't use if inside it.
I think there needs to be a # sign for the enclosure <%# ..Eval...%>
Or try the full Eval version
<%# if (Convert.ToInt32(DataBinder.Eval(Container.DataItem, "int_tipo"))
== 1) { %>
Take following example of code: (ASP.NET WebForms)
<asp:Content ContentPlaceHolderID="Contents" runat="server">
<div class="blogpost-list">
<asp:Repeater ID="blogList" runat="server">
<ItemTemplate>
<h2 class="blogpost-title">
<%# (Container.DataItem as BlogPost).Title %>
</h2>
<p class="blogpost-meta">
</p>
<p class="blogpost-content">
<%# (Container.DataItem as BlogPost).ParsedContent %>
</p>
</ItemTemplate>
</asp:Repeater>
</div>
</asp:Content>
Now what I want to do, is to avoid the content casting of the DataItem, ie. this line:
<%# (Container.DataItem as BlogPost).Title %>
I'm feeling inspired of the ASP.NET MVC, and was wondering if I could create a strong typed, view, and define it like:
<%# Page
Language="C#" MasterPageFile="~/Blog.Master"
AutoEventWireup="true" CodeBehind="Default.aspx.cs"
Inherits="MyBlog.Default<MyStrongViewType>"
%>
Or any other way to avoid typecasting, and in general, have a strong typed view for ASP.NET WebForms.
Any good ideas?
.NET 4.5 has a nifty solution for this. You just set the datatype you want to use on the repeater itself.
<asp:Repeater ID="blogList" runat="server" ItemType="BlogPost">
<ItemTemplate>
<h2 class="blogpost-title">
<%# Item.Title %>
</h2>
<p class="blogpost-meta">
</p>
<p class="blogpost-content">
<%# Item.ParsedContent %>
</p>
</ItemTemplate>
</asp:Repeater>
See:
http://weblogs.asp.net/scottgu/archive/2011/09/02/strongly-typed-data-controls-asp-net-vnext-series.aspx
http://www.asp.net/web-forms/videos/aspnet-web-forms-vnext/aspnet-vnext-videos-strongly-typed-data-controls
This might be a bit late, but this seems a nice solution:
http://dotnetminute.blogspot.com/2012/04/creating-strongly-typed-repeater.html
I think you can inherit from Repeater control and make it generic (e.g. Repeater). But you'll need to rewrite/inherit RepeaterItem class too.
You can use this or this article as example.
Over in this question, Scott writes that it is possible to get the current HTML for what's written in the FCKeditor by using FCKeditorAPI.__Instances['instanceNameHere'].GetHTML();
Could someone provide step-by-step instructions on how to accomplish this in an ASP.NET page? All I currently have so far in the .aspx file is this:
<%# Register Assembly="FredCK.FCKeditorV2" Namespace="FredCK.FCKeditorV2" TagPrefix="FCKeditorV2" %>
<%# Page Title="" Language="C#" ... %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create a new piece</h2>
<form id="form1" runat="server">
<FCKeditorV2:FCKeditor ID="FCKeditor1" runat="server">
</FCKeditorV2:FCKeditor>
<input id="Submit1" type="submit" value="Submit" runat="server" />
</form>
</asp:Content>
In javascript
Well you can do this here:
<script type="text/javascript">
var oEditor = FCKeditorAPI.GetInstance(’IDFromFCKEditor’);
oEditor.Events.AttachEvent( 'OnSelectionChange', function() {
document.getElementById("PreviewDiv").innerHTML = oEditor.GetHTML(true);
}) ;
</script>
Source http://docs.fckeditor.net/FCKeditor_2.x/Developers_Guide/JavaScript_API