高手帮帮忙,如何用户控件添加事件?

ly006404 2009-12-18 04:52:21
我用了一个类似于DropDownList的用户控件,不同的是他有筛选功能,现在想要实现类似DriopDownList控件中的SelectedIndexChanged事件。请问下,在这个控件中应该如何实现这个事件。
...全文
156 14 打赏 收藏 转发到动态 举报
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
xingdongfang 2009-12-20
  • 打赏
  • 举报
回复
这个完全可以。

你public SelectIndexchanged mCallback;

然后在你的ddl事SelectIndexchanged事件里面触发mCallback.
ly006404 2009-12-18
  • 打赏
  • 举报
回复
11楼的高手,就这样就行吗?这样写在引用该控件的页面可以找到这个事件吗?我想要的结果是和DropDownList控件中SelectIndexchanged事件一样的效果。不知道行不行?
ly006404 2009-12-18
  • 打赏
  • 举报
回复
对,就是在ascx.cs中定义一个event 。
xingdongfang 2009-12-18
  • 打赏
  • 举报
回复
楼主是不是说的服务端事件

需要在ascx.cs中定义一个event

public event EventHandler mCallback;

protected void FireEvent()
{
if (this.mCallback != null)
{
this.mCallback(this, EventArgs.Empty);
}
}
protected void Page_Load(object sender, EventArgs e)
{
//fire event
this.FireEvent();
}

在引用它的页变的oninit事件中定义:

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.WebUserControl11.mCallback += new EventHandler(WebUserControl11_mCallback);
}

void WebUserControl11_mCallback(object sender, EventArgs e)
{
throw new NotImplementedException();
}

ly006404 2009-12-18
  • 打赏
  • 举报
回复
*.ascx
<%@ Control Language="C#" AutoEventWireup="true" CodeFile="SelectClientSingle.ascx.cs" Inherits="UserControl_SelectClientSingle" %>

<script type="text/javascript" >
function <% =this.ClientID %>OnWorkerSingleselect()
{
document.getElementById("<% =txtDictionarySingleName.ClientID %>").readonly = true ;
selectIndex = document.getElementById("<% =selDictionarySingle.ClientID %>").selectedIndex ;
document.getElementById("<% =txtDictionarySingleName.ClientID %>").innerText = document.getElementById("<% =selDictionarySingle.ClientID %>").options[selectIndex].text ;
var strName = document.getElementById("<% =selDictionarySingle.ClientID %>").options[selectIndex].text ;
document.getElementById("<% =txtDictionarySingleId.ClientID %>").innerText = document.getElementById("<% =selDictionarySingle.ClientID %>").options[selectIndex].value ;
document.getElementById('<%=this.ClientID %>myFrame').style.display = 'none';
document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.display= 'none'


}
function <% =this.ClientID %>SetDictionaryName(strName)
{
document.getElementById("<% =txtDictionarySingleName.ClientID %>").innerText = strName ;
}
function <% =this.ClientID %>btnDictionarySingleCannelSel()
{
document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.display= 'none' ;
document.getElementById('<%=this.ClientID %>myFrame').style.display = 'none';
}
function <% =this.ClientID%>displayDivtxtDictionarySingle()
{
var tt = document.getElementById("<% =txtDictionarySingleName.ClientID %>") ;
var ttop = tt.offsetTop; //TT控件的定位点高

var thei = tt.clientHeight; //TT控件本身的高
var tleft = tt.offsetLeft; //TT控件的定位点宽

while (tt = tt.offsetParent){ttop+=tt.offsetTop; tleft+=tt.offsetLeft;}
if(tleft > document.body.clientWidth / 2)
{

tleft = tleft - document.getElementById('<% =this.ClientID %>divtxtDictionarySingle').clientWidth ;

}
if(ttop > (window.screen.height - 300) / 2)
{

ttop = ttop + thei ;
ttop = ttop - document.getElementById('<% =this.ClientID %>divtxtDictionarySingle').clientHeight ;

}
document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.top = ttop ;
document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.left = tleft ;
document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.display= 'block' ;
document.getElementById('<% =this.ClientID %>txtFilterDictionary').focus() ;
document.getElementById('<%=this.ClientID %>myFrame').style.top = ttop ;
document.getElementById('<%=this.ClientID %>myFrame').style.left = tleft ;
document.getElementById('<%=this.ClientID %>myFrame').style.width = document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').offsetWidth ;
document.getElementById('<%=this.ClientID %>myFrame').style.height = document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').offsetHeight ;
document.getElementById('<%=this.ClientID %>myFrame').style.zIndex = document.getElementById('<% =this.ClientID%>divtxtDictionarySingle').style.zIndex - 1;
document.getElementById('<%=this.ClientID %>myFrame').style.display = 'block';
}
function <% =this.ClientID%>filterDictionary()
{
onKeyCode = event.keyCode ;
if(onKeyCode > 36 && onKeyCode < 41)
{
<% =this.ClientID%>ChangeSelected(onKeyCode)
}
else
if(onKeyCode ==8 || onKeyCode==32 || (onKeyCode > 64 && onKeyCode < 91) || (onKeyCode > 47 && onKeyCode < 57) || (onKeyCode > 95 && onKeyCode < 106) )
{
document.getElementById("<% =txtDictionarySingleKey.ClientID %>").value =document.getElementById("<% =this.ClientID %>txtFilterDictionary").value ;
if(document.getElementById("<% =this.ClientID %>txtFilterDictionary").value != "" )
{
document.getElementById("<% =btnDictionarySinglePostBack.ClientID %>").click() ;
}
else
{
if(onKeyCode ==8)
{
document.getElementById("<% =btnDictionarySinglePostBack.ClientID %>").click() ;
}
}
}
if(onKeyCode == 13)
{

<% =this.ClientID %>OnWorkerSingleselect() ;
}

}

function <% =this.ClientID %>ChangeSelected(KeyCode) //移动Select的选中项

{
selIndex = document.getElementById("<% =selDictionarySingle.ClientID %>").selectedIndex ;
if(KeyCode == 38)
{
if(document.getElementById("<% =selDictionarySingle.ClientID %>").options.length > 0 && selIndex > 0)
{
document.getElementById("<% =selDictionarySingle.ClientID %>").selectedIndex = selIndex - 1 ;
}
}
if(KeyCode == 40 )
{
if(document.getElementById("<% =selDictionarySingle.ClientID %>").options.length > selIndex +1)
{
document.getElementById("<% =selDictionarySingle.ClientID %>").selectedIndex = selIndex + 1 ;
}
}

}

</script>
 <input id="txtDictionarySingleName" runat="server" class="tbTextbox" name="txtDictionarySingleName"

readonly="readonly" type="text" value="" />
<asp:RequiredFieldValidator ID="requDictionarySingleId" runat="server" ControlToValidate="txtDictionarySingleId"
ErrorMessage="*" Width="3px"></asp:RequiredFieldValidator>
<input id="txtDictionarySingleId" type="text" runat="server" style="display:none; width: 1px;" name="txtDictionarySingleId" />
<div id="<% =this.ClientID%>divtxtDictionarySingle" onmouseout="ContralHid(this,document.getElementById('<%=this.ClientID %>myFrame'));" class="DIV_CONTRAL" style="width: 310px; height: 220px ; z-index:20000;  position :absolute ; display: none; left: 800px; top: 40px;">
<table style="width: 200px">
<tr>
<td style="width: 120px ; ">
<input id="<% =this.ClientID %>txtFilterDictionary" style="width: 120px" type="text" onkeyup="<% =this.ClientID %>filterDictionary();" /></td>
<td style="">
<input id="<% =this.ClientID %>btnDictionarySingleSubmit" type="button" class="CoolButton_Blue" value="确定" onclick="return <% =this.ClientID %>OnWorkerSingleselect();" />
<input id="<% =this.ClientID %>btnDictionarySingleCannel" type="button" class="CoolButton_Blue" value="取消" onclick="return <% =this.ClientID %>btnDictionarySingleCannelSel();" />
</td>
</tr>
<tr>
<td colspan="2" style="height: 161px">
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<select id="selDictionarySingle" name="selDictionarySingle" multiple runat="server" style="width: 298px; height: 184px"

></select><br />
<div id="Div1" style="display:none">
<asp:TextBox ID="txtDictionarySingleKey" runat="server"></asp:TextBox>
<asp:Button ID="btnDictionarySinglePostBack" runat="server" Text="" OnClick="btnDictionarySinglePostBack_Click" CausesValidation="False" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
  <br />
 
</td>
</tr>
</table>

</div>
<iframe id="<%=this.ClientID %>myFrame" scrolling="no" frameborder="0" style="position:absolute; top:0px; left:-1px; display :none; width: 6px; height: 1px;" ></iframe>
Rock870210 2009-12-18
  • 打赏
  • 举报
回复
把用户控件code贴上来看看,不明白你怎么操作的
tzs2304 2009-12-18
  • 打赏
  • 举报
回复
up
ly006404 2009-12-18
  • 打赏
  • 举报
回复
没有人碰到过类似的问题吗?
ly006404 2009-12-18
  • 打赏
  • 举报
回复
你可是一颗星哦,帮帮忙啊。具体说下怎么实现。
ly006404 2009-12-18
  • 打赏
  • 举报
回复
哪里人不重要,能解决问题就是手。
Lovely_baby 2009-12-18
  • 打赏
  • 举报
回复
楼主是哪里人?
都是咯
呵呵~~
chengcheng1253 2009-12-18
  • 打赏
  • 举报
回复
不是很清楚 具体怎么做
ly006404 2009-12-18
  • 打赏
  • 举报
回复
自己写。带就好咯,我就直接用咯,就不用这么烦咯。
chengcheng1253 2009-12-18
  • 打赏
  • 举报
回复
自己写事件 还是说他自己带的? 自己写的话用委托代理 吧

62,046

社区成员

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

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

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

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