关于repeater内控件的事件绑定
前台代码:
<asp:repeater id= "ContentViewRepeater " runat= "server ">
<HEADERTEMPLATE>
<TR class= "Table_blue_bg2 " height= "30 ">
<TD style= "HEIGHT: 27px " align= "center " colSpan= "2 "> 短消息 </TD>
</TR>
</HEADERTEMPLATE>
<ITEMTEMPLATE>
<TR class= "Table_blue_bg1 " height= "30 ">
<TD style= "HEIGHT: 27px " align= "left " colSpan= "2 "> <IMG height= "14 " src= "../images/false.gif " width= "21 " border= "0 "> 本消息由 <%#DataBinder.Eval(Container.DataItem, "FULL_NAME ")%> 在 <%#DataBinder.Eval(Container.DataItem, "SEND_TIME ")%> 发送给您 </TD>
</TR>
<TR class= "listout " onmouseover= "javacript:className= 'listover ' " onmouseout= "javacript:className= 'listout ' ">
<TD style= "HEIGHT: 28px " align= "right " width= "17% "> 标 题:
</TD>
<TD style= "HEIGHT: 28px " align= "left " width= "83% "> <%#DataBinder.Eval(Container.DataItem, "TITLE ")%> </TD>
</TR>
<TR class= "listout " onmouseover= "javacript:className= 'listover ' " onmouseout= "javacript:className= 'listout ' ">
<TD align= "right " width= "17% "> 附件: </TD>
<TD align= "left ">
<INPUT class=input3 id=btnDownFile type=button value= " <%#DataBinder.Eval(Container.DataItem, "FILE_NAME ")%> " name=btnDownFile runat= "server ">
</TD>
</TR>
<TR>
<TD align= "right " width= "17% "> 内容:
</TD>
<TD align= "left "> <TEXTAREA style= "WIDTH: 410px; HEIGHT: 88px " name= "modifyReason "> <%#DataBinder.Eval(Container.DataItem, "CONTENT ")%> </TEXTAREA> </TD>
</TR>
</ITEMTEMPLATE>
<FOOTERTEMPLATE>
</FOOTERTEMPLATE>
</asp:repeater>
后台代码:
private void Page_Load(object sender, System.EventArgs e)
{
messageID=Request.QueryString.Get( "ID ");
sql = "select distinct T_BBSMSG.TITLE,T_BBSMSG.CONTENT,T_BBSMSG.SEND_TIME,T_BBSMSG_FILE.[FILE_ID], ";
sql+= " T_BBSMSG_FILE.[FILE_NAME],T_BBSMSG_FILE.[FILE_LENGTH],T_USER.FULL_NAME from T_BBSMSG ";
sql+= " left join T_BBSMSG_FILE on T_BBSMSG.[file_ID]=T_BBSMSG_FILE.[file_ID] ";
sql+= " left join T_BBSMSG_USER ON T_BBSMSG.MESSAGE_ID = T_BBSMSG_USER.MESSAGE_ID ";
sql+= " left join T_USER on T_BBSMSG_USER.SEND_USER_ID = T_USER.[USER_ID] ";
sql += " where T_BBSMSG.Message_ID = ' " + messageID + " ' ";
this.ContentViewRepeater.DataSource=SqlHelper.ExecuteDataset(new SqlConnection(connStr),System.Data.CommandType.Text,sql).Tables[0];
this.ContentViewRepeater.DataBind();
foreach(RepeaterItem item in this.ContentViewRepeater.Controls)
{
System.Web.UI.HtmlControls.HtmlInputButton btn=item.FindControl( "btnDownFile ") as System.Web.UI.HtmlControls.HtmlInputButton;
if(btn!=null)
{
btn.ServerClick += new System.EventHandler(this.btnDownFile_Click);
}
}
}
我想给btnDownFile绑定一个事件,但是findControl的时候,一直是null
请高手帮我看看,这段代码是哪里出了问题,谢谢啊
[解决办法]
foreach(RepeaterItem item in this.ContentViewRepeater.Items)
{
System.Web.UI.HtmlControls.HtmlInputButton btn=item.FindControl( "btnDownFile ") as System.Web.UI.HtmlControls.HtmlInputButton;
if(btn!=null)
{
btn.ServerClick += new System.EventHandler(this.btnDownFile_Click);
}
}
}
[解决办法]
1.
不用这样麻烦 !!!
而且想你这样动态添加事件的, 是无法保持视图状态的, 自然也是无法成功触发 Click 事件 !!!
2.
比较常规的方法是 使用Repeat(DataGrid,DataList,GridView均一样) 的 ItemCommand 事件,如
// .aspx
<asp:Repeater ID= "Repeater1 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " CommandName= "SayHello " runat= "server " Text= "SayHello " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
switch (e.CommandName) {
case "SayHello ":
Response.Write(e.Item.ItemIndex);
Response.Write(e.CommandArgument); // 自定义的参数信息
break;
}
}
3.
直接设置 button 的 click 事件, 当然处理程序需要一点技巧才能获取其他信息, 如:
// .aspx
<asp:Repeater ID= "Repeater2 " runat= "server " OnItemCommand= "Repeater1_ItemCommand ">
<ItemTemplate>
<asp:Button ID= "btn " runat= "server " CommandArgument= ' <%# Eval( "SomeFieldName ") %> ' Text= "SayHello " OnClick= "btnInRepeat2_Click " />
<input id= "btn2 " runat= "server " runat= "server " value= "SayHello " onclick= "btn2InRepeat2_Click " />
</ItemTemplate>
</asp:Repeater>
// .aspx.cs
protected void btnInRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
Button btn = sender as Button;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
protected void btn2InRepeat2_Click(object sender, EventArgs e)
{
// 触发事件的 Button
HtmlButton btn = sender as HtmlButton;
// Button 所在行
RepeaterItem item = btn.NamingContainer as RepeaterItem;
}
4.
对于 button ,建议使用 WebControl 的 Button 而不是使用 runat=serer 的 input,
你看到了 button 具有 CommandName 和 CommandArguments 可以使用
Hope helpful!
[解决办法]
1. 不推荐使用Html Web Controls
2. 如果在Repeater中ItemTemplate中放的按钮,一般使用ItemCommand事件处理即可,无需自己再注册事件
[解决办法]
<INPUT class=input3 id=btnDownFile type=button value= " <%#DataBinder.Eval(Container.DataItem, "FILE_NAME ")%> " name=btnDownFile runat= "server ">
换成
<asp:button id=btnDownFile type=button Text== " <%#DataBinder.Eval(Container.DataItem, "FILE_NAME ")%> " Command= <%#DataBinder.Eval(Container.DataItem, "FILE_NAME ")%> name=btnDownFile runat= "server "> </asp:button>
后台代码
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
messageID=Request.QueryString.Get( "ID ");
sql = "select distinct T_BBSMSG.TITLE,T_BBSMSG.CONTENT,T_BBSMSG.SEND_TIME,T_BBSMSG_FILE.[FILE_ID], ";
sql+= " T_BBSMSG_FILE.[FILE_NAME],T_BBSMSG_FILE.[FILE_LENGTH],T_USER.FULL_NAME from T_BBSMSG ";
sql+= " left join T_BBSMSG_FILE on T_BBSMSG.[file_ID]=T_BBSMSG_FILE.[file_ID] ";
sql+= " left join T_BBSMSG_USER ON T_BBSMSG.MESSAGE_ID = T_BBSMSG_USER.MESSAGE_ID ";
sql+= " left join T_USER on T_BBSMSG_USER.SEND_USER_ID = T_USER.[USER_ID] ";
sql += " where T_BBSMSG.Message_ID = ' " + messageID + " ' ";
this.ContentViewRepeater.DataSource=SqlHelper.ExecuteDataset(new SqlConnection(connStr),System.Data.CommandType.Text,sql).Tables[0];
this.ContentViewRepeater.DataBind();
}
}
void Repeater1_ItemCommand(object sender ,System.EventArgs e)
{
string cmd=e.CommandName;
......
}