申明一个新对象???if(userName=="")
{yourClassname ttttt=new yourClassname();}
elseif
{yourClassname bbbbb=new yourClassname();}
elseif
{yourClassname ddddd=new yourClassname();}

解决方案 »

  1.   

    是动态的做如下声明:
    Public MyLabel As System.Web.UI.WebControls.Label根据需要Public不同的服务器端控件,如何实现?
      

  2.   

    这是典型的Fatcory模式,请看:
    NameFactory.cs
    using System;namespace Factory
    {
    public class Namer{
    protected string last;
    protected string first;

    public string getFirst(){
    return first;
    }

    public string getLast(){
    return last;
    }
    }

    public class FirstFirst: Namer{

    public FirstFirst(string s){
    int i = s.LastIndexOf(" ");
    if(i > 0){
    first = s.Substring(0,i).Trim();
    last  = s.Substring(i+1).Trim();
    }
    else{
    first = "";
    last  = s;
    }
    }
    } public class LastFirst: Namer{
    public LastFirst(string s){
    int i = s.IndexOf(",");
    if(i > 0){
    last  = s.Substring(0,i).Trim();
    first = s.Substring(i+1).Trim();
    }
    else{
    last  = s;
    first = "";
    }
    }
    }

    public class NameFactory{
    public Namer getNamer(string entry){
    int i = entry.IndexOf(",");
    if(i>0){
    return new LastFirst(entry);
    }
    else{
    return new FirstFirst(entry);
    }
    }
    }
    }FactoryClient.cs
    using System;
    using Factory;public class FactoryClient{ public static void Main(string[] Args)
    {
    string name = "";
    while((name.Trim()).Length == 0){
      name = Console.ReadLine();
    } NameFactory nf = new NameFactory();
    Namer n = nf.getNamer(name);

    Console.WriteLine(n.getFirst() + " " + n.getLast());
    }
    }
      

  3.   

    文件名:datagrid.aspx<%@ Page Language="vb" AutoEventWireup="false" Codebehind="datagrid.aspx.vb" Inherits="eg_lesson.datagrid"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML>
    <HEAD>
    <title>datagrid</title>
    <meta content="Microsoft Visual Studio .NET 7.0" name="GENERATOR">
    <meta content="Visual Basic 7.0" name="CODE_LANGUAGE">
    <meta content="JavaScript" name="vs_defaultClientScript">
    <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
    <form id="Form1" method="post" runat="server">
    <FONT face="宋体"></FONT>
    <DIV id="disply" style="Z-INDEX: 102; LEFT: 232px; WIDTH: 208px; POSITION: absolute; TOP: 32px; HEIGHT: 144px" runat="server"></DIV>
    <FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体"></FONT><FONT face="宋体">
    </FONT>
    </form>
    </body>
    </HTML>=============================================
    'datagrid.aspx.vbImports System.Web.UI.HtmlControls
    Imports System.Data
    Imports System.Data.SqlClient
    Public Class datagrid
        Inherits System.Web.UI.Page
        ' Protected WithEvents disply As System.Web.UI.HtmlControls.HtmlGenericControl#Region " Web 窗体设计器生成的代码 "    '该调用是 Web 窗体设计器所必需的。
        <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()    End Sub    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
            'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
            '不要使用代码编辑器修改它。
            InitializeComponent()
        End Sub#End Region    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            '在此处放置初始化页的用户代码
            Dim disply As System.Web.UI.HtmlControls.HtmlGenericControl
            Dim DataGrid1 As New System.Web.UI.WebControls.DataGrid()
            disply.Controls.Add(DataGrid1)  '将DataGrid控件动态加入到Div元素中
            Dim conn As New SqlConnection()
            '连接数据库字符串
            conn.ConnectionString = "data source=(local);initial catalog=ga;password=lovesong;user id=sa"
            conn.Open()
            Dim strSel As String
            strSel = "select * from tbl_pcs"
            'SqlDataAdapter是在DataSet与关系数据库间起桥梁作用
            Dim objDataAdapter As New SqlDataAdapter(strSel, conn)
            Dim objDataSet As New DataSet()
            objDataAdapter.Fill(objDataSet, "pcs")
            DataGrid1.DataSource = objDataSet.Tables("pcs").DefaultView  '为DataGrid控件指定数据源
            DataGrid1.AllowPaging = True    '是否允许分页属性
            DataGrid1.PageSize = "10"       '每页的数量
            ' DataGrid1.PagerStyle.Mode = PagerMode.NumericPages   '确定分页用数字表示页码
            '指定用一个自定义格式作为前后页跳转
            DataGrid1.PagerStyle.NextPageText = "Next"  '这和PagerMode.NumericPages属性只能选择其一 
            DataGrid1.PagerStyle.PrevPageText = "Prev"
            Response.Write(DataGrid1.PageCount)        '写出DataGrid总共有多少页
            ' DataGrid1.Columns[0].HeaderText = "ID号"
            ' DataGrid1.Columns(0)
            AddHandler DataGrid1.PageIndexChanged, AddressOf LogGridPage  'AddHandler语句将事件进行绑定
            DataGrid1.DataBind()    End Sub
        Sub LogGridPage(ByVal sender As System.Object, ByVal e As DataGridPageChangedEventArgs)
            sender.CurrentPageIndex = e.NewPageIndex
            sender.DataBind()
        End SubEnd Class
      

  4.   

    在datagrid.aspx里有以下服务端控件
    <DIV id="disply" style="Z-INDEX: 102; LEFT: 232px; WIDTH: 208px; POSITION: absolute; TOP: 32px; HEIGHT: 144px" runat="server"></DIV>
    我将在Public里声明的disply 注释
    ' Protected WithEvents disply As System.Web.UI.HtmlControls.HtmlGenericControl
    放到PageLoad里
     Dim disply As System.Web.UI.HtmlControls.HtmlGenericControl
    然后运行会报以下错误:未将对象引用设置到对象的实例。 disply.Controls.Add(DataGrid1)  '将DataGrid控件动态加入到Div元素中
    难道不可以放到子程序里定义?
      

  5.   

    to evonne_feng(evonne) :
    兄弟我不理解,可否做下解释??
      

  6.   

    to  jackyhx(飞不动的侠) 
    我加入了以下两句报了值不能为空。参数名:child 的错.
            Dim disply As System.Web.UI.HtmlControls.HtmlGenericControl
            Me.Controls(1).Controls.Add(disply)不知是何原因????
      

  7.   

    使用 PlaceHolder 控件可能更好。
    将 "<div>内容</div>"整个添加到 PlaceHolder的 Controls 中去。
      

  8.   

    to spring_ok(spring.z):
            Dim disply As System.Web.UI.HtmlControls.HtmlGenericControl
            Dim objControls As New PlaceHolder()
            objControls.Controls.Add(disply)还是报原来一样的错误
    值不能为空。参数名:child 
    有谁能告诉我为什么吗????
      

  9.   

    跑题了吧?怎么到placeholder那了?有关系么?
      

  10.   

    to  jackyhx(飞不动的侠):
    可否解释一下Me.Controls(1).Controls.Add(disply)中的controls(1)是什么意思和起什么作用?
      

  11.   

    可否解释一下Me.Controls(1).Controls.Add(disply)中的controls(1)是什么意思和起什么作用?
    this.Controls[1]  is  System.Web.UI.HtmlControls.HtmlForm
    you use 
    Response.Write(this.Controls[1].ToString());see url
    http://www.csdn.net/expert/topic/914/914057.xml?temp=.1204492
      

  12.   

    能否说说this.controls(1)中的数据是按什么顺弃来排列的呢?
      

  13.   

    按照aspx文件里控件假如的顺序,需要说明的是,有些服务端控件还包含一个
    “子”控件
      

  14.   

    怎没见到微软专家呢???????能否解决一下关于.net里.aspx页和codebhind的后头代码之间关系问题。比如说:在后台代码定义一个控件,通过了编译。在.aspx页里进行修改后不进行重新生成过程。在.aspx里的控件还会接受编译吗?