浅谈ListView中的一个Bug

来源:互联网 发布:sdp 分销系统 源码 编辑:程序博客网 时间:2024/04/29 06:33
        protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)        {            if (e.Item.ItemType == ListViewItemType.InsertItem)            {                System.Web.UI.WebControls.DropDownList ddlLinkType = (System.Web.UI.WebControls.DropDownList)e.Item.FindControl("DropDownListLinkType");                if (ddlLinkType != null)                {                    TextBox LogoUrlTextBox = (TextBox)e.Item.FindControl("LogoUrlTextBox");                            
 if (LogoUrlTextBox != null)                    {                        ddlLinkType.Attributes["onchange"] = "ddlonchange(this,'" + LogoUrlTextBox.ClientID + "')";                    }                }            }        }

1.在后台为前台控件添加javascript注册事件,发现数据无法“插入”、“更改”。

 


 

ddlLinkType.Attributes["onchange"] = "ddlonchange(this,'" + LogoUrlTextBox.ClientID + "')";


2.注销该代码程序又可以进行“插入”、“更改”。

 

     <tr style="">                    <td>                        <input type="submit" name="ctrl1$InsertButton" value="插入" onclick="javascript:WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions("ctrl1$InsertButton", "", true, "Insert", "", false, false))" id="ctrl1_InsertButton" />                        <input type="submit" name="ctrl1$CancelButton" value="清除" id="ctrl1_CancelButton" />                    </td>                    <td>                         </td>                    <td>                        <input name="ctrl1$SiteNameTextBox" type="text" id="ctrl1_SiteNameTextBox" />                        <span id="ctrl1_RequiredFieldValidator1" style="color:Red;visibility:hidden;">*</span>                    </td>                    <td>                    <select name="ctrl1$insertddlLinkType" id="ctrl1_insertddlLinkType" onchange="ddlonchange(this,'ctrl1_LogoUrlTextBox')"><option value="LogoLink">图片连接</option><option value="TextLink">文本连接</option></select>                    </td>                    <td>                        <input name="ctrl1$LogoUrlTextBox" type="text" id="ctrl1_LogoUrlTextBox" />                    </td>                    <td>                        <input name="ctrl1$SiteUrlTextBox" type="text" id="ctrl1_SiteUrlTextBox" />                         <span id="ctrl1_RequiredFieldValidator2" style="color:Red;visibility:hidden;">*</span>                    </td>                </tr>


3.查看渲染到浏览器的代码,发现InsertItemTemplate里的所有ID号都缺少"ListView_"前缀。通过对事件分析,ListView创建前触发了ItemCreated事件,在事件里使用ClientID

导致了InsertItemTemplate模块中的控件渲染到浏览器端后的id值就没有“ListView1_”这个前缀,点击按钮,就无法把数据插入到ListView里。

4.在微软也发布了这个Bug

  文章地址:http://connect.microsoft.com/VisualStudio/feedback/details/328680/problem-accessing-controls-clientid-on-asp-net-listviews-itemcreated

 

 

5.经过分析,可以将注册到InsertItemTemplate的事件代码放到dataBound中运行,应为当运行到InsertItemTemplate时,就会调用此事件

 

 public static Control FindControl(string controlId, ControlCollection controls)        {            foreach (Control control in controls)            {                if (control.ID == controlId)                    return control;                if (control.HasControls())                {                    Control nestedControl = FindControl(controlId, control.Controls);                    if (nestedControl != null)                        return nestedControl;                }            }            return null;        } protected void ListView1_DataBound(object sender, EventArgs e)        {            System.Web.UI.WebControls.DropDownList ddlLinkType = FindControl("insertddlLinkType", ListView1.Controls) as System.Web.UI.WebControls.DropDownList;            TextBox LogoUrlTextBox = (TextBox)FindControl("LogoUrlTextBox", ListView1.Controls);            RequiredFieldValidator validator = FindControl("RequiredFieldValidator1", ListView1.Controls) as RequiredFieldValidator;            if (ddlLinkType != null && LogoUrlTextBox != null && validator != null)            {                ddlLinkType.Attributes["onchange"] = "ddlonchange(this,'" + LogoUrlTextBox.ClientID + "','" + validator.ClientID + "')";            }        }


6.采用微软的解决方案。。很好的处理这个难题,,到了.NET4.0也没有处理这个Bug,以后可以采用这样的办法处理。

 

原创粉丝点击