~~~~~~~~救助关于服务器控件的问题!!!!
第一天上班,经理考我,要我做这样一个控件:
用asp.net做一个服务器控件A,控件里包含三个子控件:一个普通TextBox框,一个下拉框控件,一个时间控件。要求在A控件里添加一个属性,属性值可以通过下拉选择(下拉里面的值为:TextBox,DropDownTextBox,DateTimeTextBox),
当选择TextBox时,控件A在界面上显示为一个普通TextBox框,
当选择DropDownTextBox时,控件A在界面上显示为一个下拉框控件,
当选择DateTimeTextBox时,控件A在界面上显示为一个时间控件。
向各位高手救助!
[解决办法]
要求在A控件里添加一个属性,属性值可以通过下拉选择(下拉里面的值为:TextBox,DropDownTextBox,DateTimeTextBox),
这是什么意思?
[解决办法]
我是2003的 我做了一个可以选择出Button TextBox Label的你看看 应该差不多的
ASCX:
<%@ Control Language= "c# " AutoEventWireup= "false " Codebehind= "WebUserControl1.ascx.cs " Inherits= "www1.WebUserControl1 " TargetSchema= "http://schemas.microsoft.com/intellisense/ie5 "%>
<asp:DropDownList id= "DropDownList1 " runat= "server ">
<asp:ListItem Value= "TextBox "> TextBox </asp:ListItem>
<asp:ListItem Value= "Button "> Button </asp:ListItem>
<asp:ListItem Value= "Label "> Label </asp:ListItem>
</asp:DropDownList>
ASCX后台:
namespace www1
{
using System;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
/// <summary>
///WebUserControl1 的摘要说明。
/// </summary>
public class WebUserControl1 : System.Web.UI.UserControl
{
protected System.Web.UI.WebControls.DropDownList DropDownList1;
public string GetType
{
get
{
return this.DropDownList1.SelectedValue;
}
}
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
///设计器支持所需的方法 - 不要使用代码编辑器
///修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}
ASPX前台:
<%@ Page language= "c# " Codebehind= "WebForm1.aspx.cs " AutoEventWireup= "false " Inherits= "www1.WebForm1 " %>
<%@ Register TagPrefix= "uc1 " TagName= "WebUserControl1 " Src= "WebUserControl1.ascx " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> WebForm1 </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body MS_POSITIONING= "GridLayout ">
<form id= "Form1 " method= "post " runat= "server ">
<FONT face= "宋体 ">
<uc1:WebUserControl1 id= "WebUserControl11 " runat= "server "> </uc1:WebUserControl1>
<asp:Button id= "Button1 " style= "Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 48px " runat= "server "
Text= "Button "> </asp:Button> </FONT>
</form>
</body>
</HTML>
ASPX后台:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
namespace www1
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected HtmlForm Form1;
private void Page_Load(object sender, System.EventArgs e)
{
Button btn = new Button();
btn.Text = "Button ";
btn.ID = "wzl1 ";
btn.Visible = false;
TextBox tb = new TextBox();
tb.ID = "wzl2 ";
tb.Visible = false;
Label lbl = new Label();
lbl.ID = "wzl3 ";
lbl.Visible = false;
lbl.Text = "Label ";
this.Form1.Controls.Add(btn);
this.Form1.Controls.Add(tb);
this.Form1.Controls.Add(lbl);
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Button1_Click(object sender, System.EventArgs e)
{
WebUserControl1 ddl = (WebUserControl1)this.FindControl( "WebUserControl11 ");
if(ddl.GetType == "TextBox ")
{
((TextBox)this.Form1.FindControl( "wzl2 ")).Visible = true;
((Button)this.Form1.FindControl( "wzl1 ")).Visible = false;
((Label)this.Form1.FindControl( "wzl3 ")).Visible = false;
}
if(ddl.GetType == "Button ")
{
((Button)this.Form1.FindControl( "wzl1 ")).Visible = true;
((TextBox)this.Form1.FindControl( "wzl2 ")).Visible = false;
((Label)this.Form1.FindControl( "wzl3 ")).Visible = false;
}
if(ddl.GetType == "Label ")
{
((Label)this.Form1.FindControl( "wzl3 ")).Visible = true;
((TextBox)this.Form1.FindControl( "wzl2 ")).Visible = false;
((Button)this.Form1.FindControl( "wzl1 ")).Visible = false;
}
}
}
}
[解决办法]
if(DropDownList1.SelectedItem.Text== "TextBox ")
{
TextBox tb=new TextBox();
tb.Text= "adfa ";
//Page.Controls.Add(tb);
this.Panel1.Controls.Add(tb);
}
if(DropDownList1.SelectedItem.Text== "DropDownTextBox ")
{
DropDownList tb1=new DropDownList();
tb1.Items.Add( "ddd ");
tb1.Items.Add( "ffd ");
//Page.Controls.Add(tb1);
this.Panel1.Controls.Add(tb1);
}
if(DropDownList1.SelectedItem.Text== "DateTimeTextBox ")
{
......
}
[解决办法]
感觉要做一个自定义控件才能实现
[解决办法]
帮LZ顶
[解决办法]
记号,过会来.
[解决办法]
赫赫,刚做个类似的。lz参看这个链接,内有源码。
http://msdn.microsoft.com/library/CHS/cpguide/html/cpconEnhancingDesign-TimeSupport.asp?frame=true
[解决办法]
这是考你design time support。所以楼上的都不符合要求。