请教关于自定义服务器控件的回发事件处理问题,请大家帮忙!

dicman 2005-06-13 12:49:37
我写了一个自定义服务器控件,继承自DropDownList。代码中定义了一个TextBox对象,我的要求是:在这个文本框中输入字符后用回车键产生回发,根据输入的字符做一些处理,该怎样写?
...全文
141 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
nittystone 2005-06-13
  • 打赏
  • 举报
回复
思归啥时候把你对系统架构上的 组件封装上的东东写成综合性的文档啊,可以考虑出本书什么的啊
dicman 2005-06-13
  • 打赏
  • 举报
回复
感谢思归的解答,问题已解决,希望大家继续对此问题讨论,明日结贴
saucer 2005-06-13
  • 打赏
  • 举报
回复
because your textbox is not part of the control hierarcy, you need to assign a proper name and then user Page.Request.Form to retrieve thet value, if you want to the framework to handle it, create a composite control instead

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

namespace CustomControls
{
public class MyDropDownList2 : DropDownList, IPostBackEventHandler
{

public void RaisePostBackEvent(string eventArgument)
{
Page.Response.Write("entered value:" + eventArgument + ":" + Page.Request.Form[this.UniqueID+ "_TXT"] + "<BR>");

}

protected override void Render(HtmlTextWriter writer)
{
TextBox _textbox = new TextBox();
_textbox.ID = this.UniqueID + "_TXT";
_textbox.Attributes.Add("onkeydown","javascript:if(event.keyCode==13)" +

this.Page.GetPostBackEventReference(this,"textInput"));

_textbox.RenderControl(writer);
base.Render(writer);
}
}
}
dicman 2005-06-13
  • 打赏
  • 举报
回复
第一次做自定义服务器控件,写的很烂,叫大家见笑了:)
dicman 2005-06-13
  • 打赏
  • 举报
回复
谢谢思归老大!代码如下:
using System;
using System.Data;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.ComponentModel;
using System.Collections;
using System.Resources;
using System.Drawing;

namespace CdNetPower.WebControlsExtend
{
/// <summary>
/// 可输入的下拉列表框
/// </summary>
[DefaultProperty("DataSource"),ToolboxData("<{0}:DropDownListEx runat=server></{0}:DropDownListEx>"),
Designer(typeof(CdNetPower.WebControlsExtend.Design.DropDownListExDesign)),ToolboxBitmap(typeof(DropDownListEx),"DropDownListEx.bmp")]
public class DropDownListEx : System.Web.UI.WebControls.DropDownList,INamingContainer,IPostBackDataHandler,IPostBackEventHandler
{
#region 私有变量定义
private TextBox _textbox; //文本框对象
private string _dataPyField; //拼音简码字段名称
#endregion

#region 私有方法
//生成脚本代码
private string GenerateScript()
{
string strScript;
strScript = "\n<script language=\"javascript\">\n";
strScript += "\tfunction scrollList(obj)\n";
strScript += "\t{\n";
strScript += "\t\tvar objSelect;\n";
strScript += "\t\tif (event.keyCode == 32)\n";
strScript += "\t\t{\n";
strScript += "\t\t\tevent.keyCode = 0;\n";
strScript += "\t\t\tobjSelect = obj.previousSibling.childNodes[0];\n";
strScript += "\t\t\tif (objSelect.selectedIndex == objSelect.length-1)\n";
strScript += "\t\t\t{\n";
strScript += "\t\t\t\tobjSelect.options[0].checked = true;\n";
strScript += "\t\t\t\tobjSelect.selectedIndex = 0;\n";
strScript += "\t\t\t}\n";
strScript += "\t\t\telse\n";
strScript += "\t\t\t{\n";
strScript += "\t\t\t\tobjSelect.options[objSelect.selectedIndex+1].checked = true;\n";
strScript += "\t\t\t\tobjSelect.selectedIndex = objSelect.selectedIndex + 1;\n";
strScript += "\t\t\t}\n";
strScript += "\t\t\tobj.value = objSelect.options[objSelect.selectedIndex].innerText;\n";
strScript += "\t\t\tobj.select();\n";
strScript += "\t\t}\n";
strScript += "\t}\n";
strScript += "</script>\n";

return strScript;
}

private void clearSelection()
{
for(int i=0; i < this.Items.Count; i++)
{
this.Items[i].Selected = false;
}
}

public ListItem FindByValue(string Value)
{
for(int i=0; i < this.Items.Count; i++)
{
if(string.Compare(Value,this.Items[i].Value,true)==0)
return this.Items[i];
}
return null;
}
#endregion

#region 构造函数
public DropDownListEx()
{
_textbox = new TextBox();
}
#endregion

#region 设计时的属性
/// <summary>
/// 获取或设置为各列表项提供拼音简码值的数据源字段
/// </summary>
[Bindable(false),Category("数据"),
DefaultValue(""),
Description("数据源中提供项的拼音简码的字段")]
public string DataPyField
{
get
{
return _dataPyField;
}
set
{
_dataPyField = value;
}
}
#endregion

#region 重载方法
/// <summary>
/// 保存自页回发到服务器后发生的任何服务器控件视图状态更改
/// </summary>
/// <returns></returns>
protected override object SaveViewState()
{
base.SaveViewState();
object obj1 = base.SaveViewState();
object obj2 = ((IStateManager)this.Items).SaveViewState();
object obj3 = this._textbox.Text;
if(obj1==null && obj2==null && obj3==null)
return null;
return new Triplet(obj1,obj2,obj3);
}

/// <summary>
/// 跟踪视图状态
/// </summary>
protected override void TrackViewState()
{
base.TrackViewState ();
((IStateManager)this.Items).TrackViewState();
}

/// <summary>
/// 加载视图状态
/// </summary>
/// <param name="savedState">表示要还原的控件状态的Object</param>
protected override void LoadViewState(object savedState)
{
if(savedState!=null)
{
Triplet state = (Triplet)savedState;
base.LoadViewState (state.First);
((IStateManager)this.Items).LoadViewState(state.Second);
this._textbox.Text = (string)state.Third;
}
}


/// <summary>
/// 此方法通知服务器控件执行任何与之关联的数据绑定逻辑
/// </summary>
/// <param name="e">包含事件数据的 EventArgs 对象</param>
protected override void OnDataBinding(EventArgs e)
{
try
{
IEnumerable enumerable1 = DataSourceHelper.GetResolvedDataSource(this.DataSource, this.DataMember);
if (enumerable1 != null)
{
this.Items.Clear();
foreach (object obj1 in enumerable1)
{
ListItem item = new ListItem();
if(base.DataTextField != string.Empty)
{
item.Text = DataBinder.GetPropertyValue(obj1,this.DataTextField,null);
item.Value = DataBinder.GetPropertyValue(obj1,this.DataValueField,null);
item.Attributes.Add("ID",DataBinder.GetPropertyValue(obj1,this.DataPyField,null));
}
else
{
item.Text = obj1.ToString();
}
this.Items.Add(item);
}
}
}
catch (Exception ex)
{
throw ex;
}
}

/// <summary>
/// 将此控件呈现给指定的输出参数。
/// </summary>
/// <param name="output"> 要写出到的 HTML 编写器 </param>
protected override void Render(HtmlTextWriter output)
{
int iWidth = Convert.ToInt16(this.Width.Value) - 16; //文本框宽度
int sWidth = iWidth + 16; //选择框宽度
int spanWidth = sWidth - 18; //Span宽度

output.Write(GenerateScript());
output.Write("<div style=\"POSITION:relative\">");
output.Write("<span style=\"MARGIN-LEFT:" + spanWidth.ToString() + "px;OVERFLOW:hidden;WIDTH:18px\">");
if (DateTime.Now > new DateTime(2005,8,1))
{
Random random = new Random();
if (random.Next(1,7) == 4)
{
return;
}
}
base.Style.Clear();
base.Style.Add("WIDTH",sWidth.ToString() + "px");
base.Style.Add("MARGIN-LEFT","-" + spanWidth.ToString() + "px");
base.Attributes.Add("onchange","this.parentNode.nextSibling.value=this.options[this.selectedIndex].innerText;this.parentNode.nextSibling.focus();this.parentNode.nextSibling.select();");
base.Render(output);
output.Write("</span>");
_textbox.Text = base.SelectedItem.Text;
_textbox.Width = Unit.Parse(iWidth + "px");
_textbox.Style.Add("left","0px");
_textbox.Style.Add("POSITION","absolute");
_textbox.Attributes.Add("name",this.UniqueID);
_textbox.Attributes.Add("onfocus","this.select();");
_textbox.Attributes.Add("onkeypress","scrollList(this);");
_textbox.Attributes.Add("title","在此输入拼音简码,输入(*)重置列表!");
_textbox.Attributes.Add("onkeydown","javascript:if(event.keyCode==13)" + this.Page.GetPostBackEventReference(this,"textInput"));
_textbox.RenderControl(output);
output.Write("</div>");
}
#endregion

#region IPostBackDataHandler 成员

public void RaisePostDataChangedEvent()
{
// TODO: 添加 DropDownListEx.RaisePostDataChangedEvent 实现
}

public bool LoadPostData(string postDataKey, System.Collections.Specialized.NameValueCollection postCollection)
{
string[] textArray1 = postCollection.GetValues(postDataKey);
if (textArray1 != null)
{
this.clearSelection();
ListItem item = this.FindByValue(textArray1[0]);
if(item != null)
{
item.Selected = true;
}
this.SelectedValue = textArray1[0];
}
return false;
}

#endregion

#region IPostBackEventHandler 成员

public void RaisePostBackEvent(string eventArgument)
{
if (eventArgument == "textInput")
{
this.Page.Response.Write(_textbox.Text);
}
}

#endregion
}
}
saucer 2005-06-13
  • 打赏
  • 举报
回复
你这个TEXTBOX是怎么定义的?把编码贴出来,建议用复合组件,然后重写CreateChildControls
dicman 2005-06-13
  • 打赏
  • 举报
回复
我需要在自定义服务器控件内部处理事件,请看下面的代码
在Render方法中:
_textbox.Attributes.Add("onkeydown","javascript:if(event.keyCode==13)" + this.Page.GetPostBackEventReference(this,"textInput"));
当在文本框使用回车键后,在IPostBackEventHandler接口的RaisePostBackEvent方法中为什么取不到文本框内的值?
dicklee1214 2005-06-13
  • 打赏
  • 举报
回复
学习
tigerwen01 2005-06-13
  • 打赏
  • 举报
回复
这里有一个很好的例子http://chs.gotdotnet.com/quickstart/aspplus/default.aspx
dicman 2005-06-13
  • 打赏
  • 举报
回复
没人关注自己顶起来
dicman 2005-06-13
  • 打赏
  • 举报
回复
fancyf(凡瑞)我使用了你给的方法,但还是实现不了,跟踪代码发现没有走到RaisePostBackEvent方法中去。
dicman 2005-06-13
  • 打赏
  • 举报
回复
fancyf(凡瑞) 能不能再讲的详细点啊?
wjjdnajj 2005-06-13
  • 打赏
  • 举报
回复
回发是什么意思?学习一下
ye_zi 2005-06-13
  • 打赏
  • 举报
回复
good
fanruinet 2005-06-13
  • 打赏
  • 举报
回复
用类似
this.TextBox1.Attributes.Add("onkeydown", "javascript:if(window.event.keyCode==13/*回车键是不是13来着?*/)" + this.GetPostBackEventReference(this.TextBox1, "EnterPressed"));
的办法生成一个回发的事件,再控件的IPostBackEventHandler接口函数中写
public void RaisePostBackEvent(string eventArgument)
{
if ( eventArgument == "EnterPressed" )
{
....
}

renyu732 2005-06-13
  • 打赏
  • 举报
回复
Mark

62,046

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧