DataList控件嵌套,激发内部控件事件

 

DataList控件在嵌套的时候,内部控件都没有事件了,如果想激发事件,需要自己写事件。需要在外部DataList控件的ItemDataBound事件中给内部控件定义事件。但是要在内部控件绑定数据源之前定义。例如现在我有两个嵌套的DataList控件,外部控件ID为DL_DataList,内部控件ID为DL_Item。假如现在有一个已经定义好的DataTable类型的变量 dt。我们在DL_DataList_ItemDataBound事件中写代码如下:

private void DL_DataList_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
        
{
            Control container 
= e.Item;
            ListItemType itemType 
= e.Item.ItemType;

            
if (itemType == ListItemType.Item || itemType == ListItemType.Footer || itemType == ListItemType.AlternatingItem)
            
{
                
if (e.Item.DataItem == null)
                
{
                    
return;
                }

            
                DataList dlList 
= (DataList) container.FindControl("DL_Item2");

                
if (null != dlList)
                
{
dlList.ItemCommand 
+= new DataListCommandEventHandler(DL_Item2_ItemCommand);
                        dlList.ItemDataBound 
+= new DataListItemEventHandler(DL_Item2_ItemDataBound);

                        dlList.DataSource 
= dt;
                        dlList.DataBind();
}
}
光这些还不行,我们还要另外定义一个方法,在Page_Load中调用re_Main_PreRender方法,就可以了。
protected void rp_Main_PreRender(object sender, EventArgs e)
        
{
            
foreach (DataListItem item in DL_DataList.Items)
            
{
                DataList rp_Min 
= (DataList)item.FindControl("DL_Item2");
                rp_Min.ItemCommand 
+= new DataListCommandEventHandler(DL_Item2_ItemCommand);
            }

        }
posted on 2008-04-22 13:51  Edwin dong  阅读(1157)  评论(3编辑  收藏  举报

<% Function googleColor(value, random) Dim colorArray colorArray = Split(value, ",") googleColor = colorArray(random Mod (UBound(colorArray) + 1)) End Function Function googleScreenRes() Dim screenRes, delimiter, resArray screenRes = Request.ServerVariables("HTTP_UA_PIXELS") delimiter = "x" If IsEmpty(screenRes) Then screenRes = Request.ServerVariables("HTTP_X_UP_DEVCAP_SCREENPIXELS") delimiter = "," End If resArray = Split(screenRes, delimiter, 2) If (UBound(resArray) + 1) = 2 Then googleScreenRes = "&u_w=" & resArray(0) & "&u_h=" & resArray(1) End If End Function Function googleDcmguid() Dim dcmguid dcmguid = Request.ServerVariables("HTTP_X_DCMGUID") If Not IsEmpty(dcmguid) Then googleDcmguid = "&dcmguid=" & dcmguid End If End Function Dim googleTime, googleDt, googleScheme, googleHost googleTime = DateDiff("s", "01/01/1970 00:00:00", Now()) googleDt = (1000 * googleTime) + Round(1000 * (Timer - Int(Timer))) googleScheme = "http://" If StrComp(Request.ServerVariables("HTTPS"), "on") = 0 Then googleScheme = "https://" googleHost = Server.URLEncode(googleScheme & Request.ServerVariables("HTTP_HOST")) Dim googleAdUrl, googleAdOutput googleAdUrl = "http://pagead2.googlesyndication.com/pagead/ads?" &_ "ad_type=text_image" &_ "&channel=" &_ "&client=ca-mb-pub-6539345765131754" &_ "&dt=" & googleDt &_ "&format=mobile_single" &_ "&host=" & googleHost &_ "&ip=" & Server.URLEncode(Request.ServerVariables("REMOTE_ADDR")) &_ "&markup=xhtml" &_ "&oe=utf8" &_ "&output=xhtml" &_ "&ref=" & Server.URLEncode(Request.ServerVariables("HTTP_REFERER")) &_ "&url=" & googleHost & Server.URLEncode(Request.ServerVariables("URL")) &_ "&useragent=" & Server.URLEncode(Request.ServerVariables("HTTP_USER_AGENT")) &_ googleScreenRes() &_ googleDcmguid() Set googleAdOutput = Server.CreateObject("MSXML2.ServerXMLHTTP") googleAdOutput.Open "GET", googleAdUrl, false googleAdOutput.Send Response.Write(googleAdOutput.responseText) %>