web ui controls and ASP.NET MVC - asp.net

Why will not fill View page of this controller method
public ActionResult Person()
{
testEntities6 testPersons = new testEntities6();
IQueryable<person> persons;
DropDownLst.Items.Clear();
DropDownLst.Items.Add("proba");
persons = from i in testPersons.person
select i;
return View(persons);
}
and include namespaces:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using MvcKVteam.Models;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
In view page I put this code:
<asp:DropDownList ID="DropDownLst" runat="server">
</asp:DropDownList>

I don't think you can use server controls in MVC. You should be using HTML Helpers instead.
Based on your existing code:
<%= Html.DropDownList("DropDownLst", Model.AsEnumerable()) %>
But I would recommend using a View Model instead of just passing an IQueryable to the View. I also recommend you pick up an MVC book. Professional ASP.NET MVC 1.0 is really good, but doesn't cover the 2.0 release.
Rich

Related

'Unknown server tag' as I'm trying to use a custom control

I'm a complete autodidact and never had added new classes to a Visual Studio project before. I stepped now on this post which seems pretty cool and therefore wanted to implement the described class and control.
I added a new class to my asp.net project DesignProject and pasted the code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
namespace DesignProject
{
public class DataPlaceHolder : PlaceHolder
{
protected override void DataBindChildren()
{
if (Visible)
{
base.DataBindChildren();
}
}
}
}
How can I use the control DataPlaceHolder in the markup? I've been trying with DesignProject:DataPlaceHolder.... but Unknown server tag 'DesignProject:DataPlaceHolder'.
Martin
Try:
<%# Register TagPrefix="myControl" Namespace="DesignProject" Assembly="MyApp" %>

datagrid has no property datasource

I couldnt use dataSource property of dataGrid ?
my usings are
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using System.Data;
private void button4_Click(object sender, RoutedEventArgs e)
{
tabControl1.SelectedIndex = 3;
dataGrid2.DataSource///////////////does not come up :S
ExcelData exceldata = new ExcelData();
this.dataGrid2.DataContext = exceldata;
}
what I missed?
it's DataSource not Datasource
Kindly Take a look at DataGrid Datasource property
also windows Datagrid does not have any Datacontext property
Take a look at Windows Datagrid

ASP.NET C# partial class in separate files not working CS1061

I have an asp.net page called prequal.aspx with a codebehind of prequal.aspx.cs. It works. I want to separate out each client code from this page into their own partial files (to reduce chance of modifying the wrong one by mistake later.) Both .cs files begin as such:
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class prequal : System.Web.UI.Page
{
When I put one of the clients code into its own file, it compiles fine but I get the following error when I try to view the page:
CS1061: 'ASP.prequal_aspx' does not contain a definition for 'lnkLanguage_Click' and no extension method 'lnkLanguage_Click' accepting a first argument of type 'ASP.prequal_aspx' could be found (are you missing a using directive or an assembly reference?)
prequal.aspx has this:
<asp:LinkButton id="lnkLanguage" onclick="lnkLanguage_Click" runat="server" CausesValidation="False">English / En EspaƱol</asp:LinkButton>
prequal.aspx.cs has this:
protected void lnkLanguage_Click()
{
// alternate preferred language
if (Session["lang"].ToString() == "spa")
{
Session["lang"] = "eng";
}
else
{
Session["lang"] = "spa";
}
populateQuestions();
}
populateQuestions() will call other code in prequal.aspx.cs which calls code in prequal-client1.aspx.cs. The code works before I split it up so am I going about creating separate partial class files incorrectly? Or is the issue something else that I am unaware of yet?
I believe the signature for lnkLanguage has to be:
protected void lnkLanguage_Click(object sender, EventArgs e)
{
//...
}

Accessing MasterPage object from an .aspx code-behind

I know we've done this before in another .aspx page that's using this master page. So I tried this in a new .aspx but for some reason, it is not recognizing the Master object. And the .aspx definitely is set to the master page in the page directive correctly and there's no errors to that effect:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Text;
public partial class LandingPage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Master.HideNavbar();
}
}
Try to reference your masterpage at your aspx file :
<%# MasterType virtualpath="~/YourMasterPage.master" %>
to get typed Master page class in your code you need to define what type is it, either in Page directive or with MasterType directive on page:
<%# Page masterPageFile="~/MasterPage.master"%>
<%# MasterType virtualPath="~/MasterPage.master"%>
If you don't have these directives, you can always cast Master property in code:
(MasterPage)this.Master
see also here
http://msdn.microsoft.com/en-us/library/c8y19k6h.aspx
Edit:
is HideNavbar() method public?

ASP.NET and dynamic Pages

Is it possible to write a System.Web.UI.Page and stored in an assembly?
And how can I make iis call that page?
So I will go deeply...
I'm writing a class like that:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Reflection;
using WRCSDK;
using System.IO;
public partial class _Test : System.Web.UI.Page
{
public _Test()
{
this.AppRelativeVirtualPath = "~/WRC/test.aspx";
}
protected void Page_Load(object sender, EventArgs e)
{
Response.Write("You are very lucky!!!");
}
}
That are stored into an assembly.
So Now How can I register that assemply and obtain that http://localhost/test.aspx invoke that class?
Thanks.
Bye.
You'll want to use an HttpHandler or HttpModule to do this.
Registering the assembly is just like registering any assembly -- just define that class in a code file and have the compiled DLL in your bin directory.
Then, as an example, you can create a IHttpHandlerFactory:
public class MyHandlerFactory : IHttpHandlerFactory
{
public IHttpHandler GetHandler(HttpContext context, ........)
{
// This is saying, "if they requested this URL, use this Page class to render it"
if (context.Request.AppRelativeCurrentExecutionFilePath.ToUpper() == "~/WRC/TEST.ASPX")
{
return new MyProject.Code._Test();
}
else
{
//other urls can do other things
}
}
.....
}
Your web.config will include something like this in the httpHandlers section
<add verb="POST,GET,HEAD" path="WRC/*" type="MyProject.Code.MyHandlerFactory, MyProject"/>
Not sure what you're after here. If you set up a deployment project, there's a setting to have it merge all the dll files into a single assembly. Is that what you want? Either way, if you want to reuse the same code behind class for several aspx pages, it is the page declarative (1st line of code in the aspx) that you must change.
Few options
1. You can refer to this assembly as part of visual studio references
2. Use relfection to load the assembly and class from your test ASAPX page.

Resources