后台找到repeater里面的div并添加客户端点击事件

时间:2023-12-15 17:51:56

public partial class Inv_SelectWorkservice : System.Web.UI.Page,IPostBackEventHandler{

}

通过OnItemCreated

找到repeater里面的div并添加客户端点击事件
div要加上runat="server" id="itemTy" onclick="test"

后台:
protected void Repeater2_ItemCreated(object sender, RepeaterItemEventArgs e)
{
switch (e.Item.ItemType)
{
case ListItemType.EditItem:
break;
case ListItemType.Footer:
break;
case ListItemType.Header:
break;
case ListItemType.AlternatingItem:
case ListItemType.Item:
System.Web.UI.HtmlControls.HtmlGenericControl itemTyC = e.Item.FindControl("itemTy") as System.Web.UI.HtmlControls.HtmlGenericControl;//获取div
HiddenField hf2 = (HiddenField)e.Item.FindControl("tx_proTyId");
if (hf2 != null)
{
ViewState["proTyId"] = hf2.Value;
}
if (itemTyC != null)
{
itemTyC.Attributes["onclick"] = this.Page.ClientScript.GetPostBackEventReference(this, "ItemCe");为每一行数据添加点击事件
if (e.Item.ItemIndex==0)
{
itemTyC.Attributes["class"] ="active";
}
}
break;
case ListItemType.Pager:
break;
case ListItemType.SelectedItem:
break;
case ListItemType.Separator:
break;
default:
break;
}
}

//这个方法会更具onclick值,调用具体的业务逻辑
public void RaisePostBackEvent(string eventArgument)
{

switch (eventArgument)
{
case "test":
JSController.Alert(this, "procheck");
break;
case "ItemCe":
JSController.Alert(this, "ItemCe");
break;
default:
break;
}

}