how to avoid this error - asp.net

In my page, if i refer
using System.windows.forms;
using System.web.webcontrols.....
Textbox ambiguos refernece error is occured from webcontrols and forms,
how can i rectify this error... but i need those two namespaces in my asp.net page.

Let me first say that I find it a bit odd that you need to reference System.Windows.Forms in an ASP.NET project.
As already posted by ondesertverge, you can fully qualify your types. You can create aliases for the namespaces to save some typing:
using WebForms = System.Web.WebControls;
using WinForms = System.Windows.Forms;
// ...
var textbox = new WebForms.TextBox();

You can fully qualify one of them.
System.Windows.Forms.TextBox

You can alias the namespaces and uses the alias for each type:
using winforms = System.Windows.Forms;
using webForms = System.Web.Webcontrols;
...
winforms::TextBox ...
webforms::TextBox ...
...
Another option is to use the full name for each type (including the namespace), to avoid ambiguity.

Related

Higher order function in VB.NET (Select) gives me a "Select is not a member of [...]"

I am a C# lover, and not very familiar with VB.NET. I am trying to do a classic:
var data = data.Select(c=>c.Id).ToList()
However, when I do this in VB.NET
<% rModel.SearchProductIds = Model.Products.[Select](Function(c) c.Id).ToList()%>
I get the following error:
"Select is not a member of System.Collections.Generic.List"
I don't understand why. My framework is .NET 3.5, so it should work.
Is it a syntax mistake?
You probably need to
#Imports System.Linq
in your view.
Or perhaps more appropriately, add a property to your model that retrieves your product IDs instead of doing that right in the view.
If you use Linq a lot and don't want to add the #Imports statement repeatedly, follow the answer here to have it automatically imported to all of your views.

Casting UserControl ASP.control_name_ascx vs Control_Name - Advantages/Disadvantages?

Recently I dealt with a commercially available ASP.NET product that shall go unnamed. When poking around in the code, I noticed that there was usercontrol casting that looked like this:
Dim ctl As ASP.modules_controls_addressinput_ascx = DirectCast(Me.FindControl("AddressInput1"), ASP.modules_controls_addressinput_ascx)
More recently I needed to cast a usercontrol to its specific type in one of my own projects so I could access its public properties and naturally I copied the casting method from above, since I had not seen another way to do it.
However, when deploying the project with this type of casting it would "Build", but failed when I tried to "Publish" with the error "Unknown Type". After some tinkering, I realized that the type of the declared class would work as follows:
Dim ctl As Modules_Controls_AddressInput = DirectCast(Me.FindControl("AddressInput1"), Modules_Controls_AddressInput)
Where the usercontrol is declared like this in its ascx.vb file:
Partial Class Modules_Controls_AddressInput
Inherits System.Web.UI.UserControl
And indeed, this also fixed the issue with publishing.
My question is what would compel someone to cast the first way vs the second way, especially when it means that publishing the project will fail?
I am not sure but the first approach will cast your control to the compiled code in asp.net temp folder C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Temporary ASP.NET Files\ProjectName but the second approach will cast it to a the class itself. In my work usually I use LoadControl("UserControlPath") to create an instance of any user control
Dim ctrl As MyControl = CType(Page.LoadControl("MyControl.ascx"), MyControl)
ctrl.Property1 = value1
ctrl.Property2 = value2
for more further information about user controls in ASP.Net you can refer to this post http://msdn.microsoft.com/en-us/library/ms972975.aspx

Downgrading from ASP.net 4 MVC 2 to ASP.net 3.5 MVC

I am a bit of beginner with ASP.net MVC, and built most of my original app while following a book.
I am trying to downgrade a project because of server limitations.
I have resolved a number of errors, but now I am stuck on this on:
If I build it builds ok, but when I press play, I get this error:
Compiler Error Message: CS0246: The type or namespace name 'dynamic' could not be found (are you missing a using directive or an assembly reference?)
It occurs on this line:
[System.Runtime.CompilerServices.CompilerGlobalScopeAttribute()]
Line 142: public class views_rooms_index_aspx : System.Web.Mvc.ViewPage, System.Web.SessionState.IRequiresSessionState, System.Web.IHttpHandler {
Line 143:
Line 144: private static bool #__initialized;
Which I believe probably originates from somewhere like this in my View:
<%# Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<dynamic>" %>
Is System.Web.Mvc.ViewPage something that you can only do in asp.net 4? What do I need to replace this with for ASP.net 3.5?
The dynamic keyword was introduced in .NET 4.0, and will not be available in any version earlier than that.
I believe the default base class for an mvc view in MVC with ASP.NET 3.5 was System.Web.Mvc.ViewPage, without any generic type. If you want to add a strongly typed model (which is recommended whenever you want to pass data from the controller to the view) you should create a view model class, and replace dynamic with the namespace qualified name of your view model.
Example: you want to pass a string name that you got from somewhere in your controller, to the Home/Index view. Do the following:
Create a class HomeIndexViewModel (name doesn't matter, but this is a good one ;) ) in the Models folder of your project, and give it a public string Name {get; set;} property.
In your controller, insantiate this class and set the name. Pass it to the view using the return View(model); overload.
Make your view inherit System.Web.Mvc.ViewPage<YourProject.Models.HomeIndexViewModel>. You can now access the name using Model.Name in the view.
Your Inherits attribute:
Inherits="System.Web.Mvc.ViewPage<dynamic>"
Has the dynamic type as the generic type - change it to the correct ViewModel type.
For example:
Inherits="System.Web.Mvc.ViewPage<ProductViewModel>"
Answers above cover it pretty well.
you might consider using:
http://www.jetbrains.com/resharper/
i have used it to convert my website to web application and it really helps.

internal ToMVCHmlString in TagBuilder

I am looking to write a few helpers in my own assembly modeled after the helpers in System.web.mvc. My problem is that I cannot use the call to Tagbuilder.ToMvcHtlString since it is internal. So if I return a string it wont be ready for asp.net 4 when it comes.
I dont want to add anything to system.web.mvc as that is a given dll.
return MvcHtmlString.Create(tagBuilder.ToString());

Solution explorer of visual studio

In solution explorer of asp.net application we are adding something in References section
for eg:in our project there are sample.Dal,sample.exeption ,system.core etc
What is actually References means,,,can we add by 'using' statement
Using is used for namespace resolution. For example:
using System.Data;
lets you access the DataSet class without typing in the fully qualified name; System.Data.DataSet.
This doesn't however tell the compiler what assembly (DLL) the DataSet class lies in. So you need to tell it. So you refer to System.Data.dll by adding it to the references section in solution explorer.
A reference references assemblies required for the current project.
Where using statements reference namespaces for the current file.
And yes, a referenced namespace must exist in one of the referenced assemblies.
Yes, once you reference another project or assembly, it's namespaces and types are available for use in the project that references them (you must reference the project or assembly before you can use the types within it).
You can either use using declarations or fully-qualified type declarations to access the types, as in:
// Example1: The using keyword.
using System;
void Example1()
{
Int32 myExample;
// etc.
}
// Example2: Fully-qualified type.
void Example2()
{
System.Int32 myExample;
// etc.
}
Note: I have used C# here.

Resources