• 全部
  • .NET Framework
  • ASP
  • Web Services
  • .NET互联网桌面应用
  • VB
  • 图表区
  • 分析与设计
  • 组件/控件开发
  • AppLauncher
  • 问答

自定义控件的问题!!!!!!!!!!!

cyp503 2004-04-30 08:46:52
各位,我按照.net framework帮助文档做了一个自定义控件,源代码如下:

using System;
using System.Web.UI;
using System.Collections;
using System.Collections.Specialized;

namespace WebControlLibrary
{

public class TestButton: System.Web.UI.WebControls.WebControl, IPostBackEventHandler
{

// Defines the Click event.
public event EventHandler Click;

//Invoke delegates registered with the Click event.
protected virtual void OnClick(EventArgs e)
{

if (Click != null)
{
Click(this, e);
}
}


// Define the method of IPostBackEventHandler that raises change events.
public void RaisePostBackEvent(string eventArgument)
{

OnClick(new EventArgs());
}


protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
" Value = 'Click Me' />");
}
}
}

编译后引用,这个自定义控件完全没问题.注意:直到这里没问题

后来,我又做了一个自定义控件,这个控件嵌入上述控件TestButton,

新作的这个控件也可以实现页面回发,因为实现了IPostBackEventHandler接口

但问题出现在"虽然已经可以实现页面回发,但服务器端不能判断是由于哪个控件引起的"

即:

MyCustomButton btn=new MyCustomButton();
btn.Click+=new EventHandler(this.btnClick);

private void btnClick(object sender,EventArgs e)
{
Response.Write("hello world!"); //这里不能输出!!
}
...全文
69 点赞 收藏 21
写回复
21 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
vzxq 2004-05-08
学习!
回复
cyp503 2004-05-08
thanks saucer :)

回复
cyp503 2004-05-07
WebControlCollection.cs

----------------------------------------------------------------------------------


using System;
using System.Collections;
using System.Web.UI.WebControls;

namespace WebControlLibrary
{
[Serializable()]
public class WebControlCollection:System.Collections.CollectionBase
{
public WebControlCollection(){}
public WebControlCollection(WebControlCollection value)
{
this.AddRange(value);
}
public WebControlCollection(WebControl[] value)
{
this.AddRange(value);
}

public WebControl this[int index]
{
get{return (WebControl)List[index];}
set{List[index]=value;}
}

public int Add(WebControl value)
{
return List.Add(value);
}

public void AddRange(WebControl[] value)
{
for(int i=0;i<value.Length;i=i+1)
{
this.Add(value[i]);
}
}

public void AddRange(WebControlCollection value)
{
for(int i=0;i<value.Count;i=i+1)
{
this.Add(value[i]);
}
}

public bool Contains(WebControl value)
{
return List.Contains(value);
}

public void CopyTo(WebControl[] array,int index)
{
List.CopyTo(array,index);
}

public int IndexOf(WebControl value)
{
return List.IndexOf(value);
}

public void Insert(int index,WebControl value)
{
List.Insert(index,value);
}

public void Remove(WebControl value)
{
List.Remove(value);
}

public new WebControlEnumerator GetEnumerator()
{
return new WebControlEnumerator(this);
}
}

public class WebControlEnumerator:object, System.Collections.IEnumerator
{
private IEnumerator baseEnumerator;
private IEnumerable temp;
public WebControlEnumerator(WebControlCollection mappings)
{
this.temp=((IEnumerable)mappings);
this.baseEnumerator=temp.GetEnumerator();

}

public WebControl Current
{
get{return (WebControl)baseEnumerator.Current;}
}

object IEnumerator.Current
{
get
{
return baseEnumerator.Current;

}
}

public bool MoveNext()
{
return baseEnumerator.MoveNext();
}

bool IEnumerator.MoveNext()
{
return baseEnumerator.MoveNext();
}

public void Reset()
{
baseEnumerator.Reset();
}
//
// bool IEnumerator.Reset()
// {
// baseEnumerator.Reset();
// }

}
}
回复
cyp503 2004-05-07
MenuButton.cs

------------------------------------------------------------------

using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

namespace WebControlLibrary
{
/// <summary>
/// MenuBarItem
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MenuBarItem runat=server></{0}:MenuBarItem>")]
public class MenuBarItem : System.Web.UI.WebControls.WebControl,IPostBackEventHandler , INamingContainer

{
public event EventHandler Click;
private string text;

[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public string Text
{
get{return text;}
set{text = value;}
}

private string strToolTip;
/// <summary>
/// 提示
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public override string ToolTip
{
get{return strToolTip;}
set{strToolTip=value;}
}


public void RaisePostBackEvent(string eventArgument)
{
OnBtnClick(new EventArgs());

}

protected virtual void OnBtnClick(EventArgs e)
{
if(Click!=null)
Click(this,e);
}


/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
output.Write(GetBeginHtmlSource());
output.Write("<input type=submit name="+this.UniqueID+" value="+text+" alt=\""+strToolTip+"\" id="+this.UniqueID+" />");
//base.RenderControl(output);
output.Write("</td></tr></tbody></table></td>");
}

/// <summary>
/// 获取开始输出的Html源
/// </summary>
/// <returns>Html源</returns>
private string GetBeginHtmlSource()
{
StringBuilder sb=new StringBuilder();
ensb.Append(" <td align=absmiddle noWrap width=\"50\">");
sb.Append("\n");
sb.Append("<table border=1 bordercolordark=#ECEAE6 bordercolorlight=#ECEAE6 cellpadding=0 cellspacing=0 height=22 ");
sb.Append(" onMouseDown=\"javascript:this.borderColorDark='#ffffff';this.borderColorLight='#808080'\" ");
sb.Append(" onMouseOut=\"javascript:this.borderColorDark='#ECEAE6';this.borderColorLight='#ECEAE6';\" ");
sb.Append("onMouseOver=\"javascript:this.borderColorDark='#808080';this.borderColorLight='#ffffff';\"");
sb.Append("onMouseUp=\"javascript:this.borderColorDark='#ECEAE6';this.borderColorLight='#ECEAE6'\"");
sb.Append("style=\"CURSOR: hand\" width=\"100%\">");
sb.Append("\n");
sb.Append(" <tbody>");
sb.Append("\n");
sb.Append(" <tr>");
sb.Append("\n");
sb.Append("<td align=center bgcolor=#ECEAE6 bordercolordark=#ECEAE6");
sb.Append(" bordercolorlight=#ECEAE6 noWrap valign=center>");
return sb.ToString();
}
}
}
回复
cyp503 2004-05-07
谢谢各位的热心回复:



我现在把源代码贴出来,麻烦各位帮个忙!真的很急!谢谢

MenuBar.cs
----------------------------------------------------------------------


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

namespace WebControlLibrary
{
/// <summary>
/// MenuBar
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MenuBar runat=server></{0}:MenuBar>")]
public class MenuBar : System.Web.UI.WebControls.WebControl,System.Web.UI.IPostBackEventHandler,System.Web.UI.INamingContainer
{

private WebControlCollection webControlCollection=new WebControlCollection();
/// <summary>
/// MenuBarItem集合
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue(typeof(WebControlCollection))]
public WebControlCollection MenuBarItems
{
get{return webControlCollection;}
set
{
this.EnsureChildControls();
webControlCollection=value;
}
}


private string strWidth="100%";
/// <summary>
/// 宽度
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("100%")]
public new string Width
{
get{return strWidth;}
set
{
this.EnsureChildControls();
strWidth=value;
}
}

private string strHeight="20";
/// <summary>
/// 高度
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public new string Height
{
get{return strHeight;}
set
{
this.EnsureChildControls();
strHeight=value;
}
}

/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
output.Write(GetBeginHtmlSource());
foreach(WebControl wtl in webControlCollection)
{
wtl.RenderControl(output);
}
output.Write(" </tr> </tbody></table>");

}


/// <summary>
/// 获取开始输出的Html源
/// </summary>
/// <returns>Html源</returns>
private string GetBeginHtmlSource()
{
StringBuilder sb=new StringBuilder();
sb.Append("<table bgcolor=#ECEAE6 border=1 bordercolordark=#808080 ");
sb.Append(" bordercolorlight=#ffffff cellpadding=0 cellspacing=0 width=\""+strWidth+"\" height=\""+strHeight+"\">");
sb.Append(" <tbody>");
sb.Append(" <tr bordercolordark=#ECEAE6 bordercolorlight=#ECEAE6> ");
return sb.ToString();
}

#region Implementation of IPostBackEventHandler
public void RaisePostBackEvent(string eventArgument)
{

}
#endregion


}
}
回复
saucer 2004-05-07
if you look at the source code in IE, you will see the name/id are empty for theose MenuItemButton's, the problem is that those control were never part of the Controls hierarchy, try to modify MenuBar.cs to something like


using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Text;

namespace WebControlLibrary
{
/// <summary>
/// MenuBar
/// </summary>
[DefaultProperty("Text"),
ToolboxData("<{0}:MenuBar runat=server></{0}:MenuBar>")]
public class MenuBar : System.Web.UI.WebControls.WebControl,System.Web.UI.IPostBackEventHandler,System.Web.UI.INamingContainer
{

private WebControlCollection webControlCollection=new WebControlCollection();
/// <summary>
/// MenuBarItem集合
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue(typeof(WebControlCollection))]
public WebControlCollection MenuBarItems
{
get{return webControlCollection;}
set
{

webControlCollection=value;
this.EnsureChildControls();
}
}


private string strWidth="100%";
/// <summary>
/// 宽度
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("100%")]
public new string Width
{
get{return strWidth;}
set
{
strWidth=value;
this.EnsureChildControls();
}
}

private string strHeight="20";
/// <summary>
/// 高度
/// </summary>
[Bindable(true),
Category("Appearance"),
DefaultValue("")]
public new string Height
{
get{return strHeight;}
set
{

strHeight=value;
this.EnsureChildControls();
}
}

protected override void CreateChildControls()
{
foreach(WebControl wtl in webControlCollection)
{
this.Controls.Add(wtl);
}
}
/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
output.Write(GetBeginHtmlSource());
foreach(WebControl wtl in webControlCollection)
{
wtl.RenderControl(output);
}
output.Write(" </tr> </tbody></table>");

}


/// <summary>
/// 获取开始输出的Html源
/// </summary>
/// <returns>Html源</returns>
private string GetBeginHtmlSource()
{
StringBuilder sb=new StringBuilder();
sb.Append("<table bgcolor=#ECEAE6 border=1 bordercolordark=#808080 ");
sb.Append(" bordercolorlight=#ffffff cellpadding=0 cellspacing=0 width=\""+strWidth+"\" height=\""+strHeight+"\">");
sb.Append(" <tbody>");
sb.Append(" <tr bordercolordark=#ECEAE6 bordercolorlight=#ECEAE6> ");
return sb.ToString();
}

#region Implementation of IPostBackEventHandler
public void RaisePostBackEvent(string eventArgument)
{

}
#endregion


}
}

回复
SVG 2004-05-03
现在楼主是用靓态控件来实现 submit 的, 就好比使用 script __doPostBack 一样来响服务事件.
一旦页面有不同的 type=submit 的提交, 使用静态控件就当然不知道是哪个 handler 了.
所以不要使用静态 submit 控件, 而使用 asp:button
回复
lllyg 2004-04-30
protected override void Render(HtmlTextWriter output)
{
output.Write("<INPUT TYPE = submit name = " + this.UniqueID +
" Value = 'Click Me' />");
}
---------------------------
up,好像要修改上面
回复
supermarrio 2004-04-30
UP
回复
saucer 2004-04-30
if you add your control to TestButton, what does your Render() look like?

also try to implement INamingContainer

public class TestButton: System.Web.UI.WebControls.WebControl, IPostBackEventHandler,INamingContainer

回复
saucer 2004-04-30
I assume you were saying the event is never raised, where are you creating those controls? make sure they are created in Page_Load event or before, if you are using CreateChildControls(), try to call EnsureChildControls()
回复
cyp503 2004-04-30
就是点击MenuBar后,不能确定是哪个MenuButton引起页面回发的
回复
cyp503 2004-04-30
我再说说我为什么要新增另一个自定义控件

我要实现的是一个MenuBar

而MenuBar是可以添加自定义的button的
回复
vzxq 2004-04-30
学习,UP
回复
cyp503 2004-04-30
??

To:SVG(ben)

能详细一点吗?小弟愚质
回复
zhengyi2002grade 2004-04-30
up
回复
SVG 2004-04-30
不能判断是由于哪个控件引起

因为你有
"<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />"
和你另一个 button 控件不知道是谁触发 postback 的.

所以, 建议 "<INPUT TYPE = submit name = " + this.UniqueID + " Value = 'Click Me' />" 不要是 response.write 了崃, 是要 new button() 出来. 再与 new button .click 的 event 挂上 eventhandler 就行了.

这时, 也无需实现 IPostBackEventHandler
回复
cyp503 2004-04-30
都实现了
回复
张新林 2004-04-30
试着给子控件加ID。
回复
张新林 2004-04-30
MenuBar实现INamingContainer接口了吗?
回复
发帖
.NET技术社区
创建于2007-09-28

5.8w+

社区成员

.NET技术交流专区
申请成为版主
帖子事件
创建了帖子
2004-04-30 08:46
社区公告
暂无公告