asp.net的listView控件:<InsertItemTemplate>
            <tr style="">
                <td>
                    <asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="插入" />
                    <asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="清除" />
                </td>
                <td>
                    <asp:TextBox ID="NameTextBox" runat="server" Text='<%# Bind("Name") %>' />
                </td>
                <td>
                    <asp:TextBox ID="locationTextBox" runat="server" 
                        Text='<%# Bind("location") %>' />
                </td>
                <td>
                       //下面的ID用于获取到该TextBox
                        <asp:TextBox ID="LogoTextBox_Logo" runat="server" Text='<%# Bind("Logo") %>' />
                </td>
                <td>
                    <asp:TextBox ID="sequenceTextBox" runat="server" 
                        Text='<%# Bind("sequence") %>' />
                </td>
                <td>
                    <asp:TextBox ID="createrTextBox" runat="server" Text='<%# Bind("creater") %>' />
                </td>
                <td>
                    <asp:DropDownList ID="ddlInsertTemplate" runat="server">
                          <asp:ListItem value="Text">文本链接</asp:ListItem>
                          <asp:ListItem value="pic">图片</asp:ListItem>
                    </asp:DropDownList>
                </td>
            </tr>
        </InsertItemTemplate>
此处是后台代码:protected void ListView1_ItemCreated(object sender, ListViewItemEventArgs e)
        {
            if (e.Item.ItemType==ListViewItemType.InsertItem)
            {
               //这里根据下拉控件的ID获取到了下拉控件的对象
                DropDownList ddl = (DropDownList)e.Item.FindControl("ddlInsertTemplate");
 //这里也是找到插入数据时要获取的textBox的对象,红色字体的ID和上面<InsertItemTemplate>的TextBox的Id是一样的   
                TextBox tb = (TextBox)e.Item.FindControl("LogoTextBox_Logo");
                //下面红色部分生成的ID和在HTML页面渲染成的ID不一样。求解
                ddl.Attributes["onchange"] = "changeVisible(this,"+tb.ClientID+")";
            }
        }
后台代码中tb对象是不是空,并且生成的ClientId和在页面生成的ID不一样
以下是渲染成的HTML的部分代码
<tr style="">
                <td>
                    <input type="submit" name="ListView1$ctrl4$InsertButton" value="插入" id="ListView1_InsertButton" />
                    <input type="submit" name="ListView1$ctrl4$CancelButton" value="清除" id="ListView1_CancelButton" />
                </td>
                <td>
                    <input name="ListView1$ctrl4$NameTextBox" type="text" id="ListView1_NameTextBox" />
                </td>
                <td>
                    <input name="ListView1$ctrl4$locationTextBox" type="text" id="ListView1_locationTextBox" />
                </td>
                <td>
                        <input name="ListView1$ctrl4$LogoTextBox_Logo" type="text" id="ListView1_LogoTextBox_Logo" />
                </td>
                <td>
                    <input name="ListView1$ctrl4$sequenceTextBox" type="text" id="ListView1_sequenceTextBox" />
                </td>
                <td>
                    <input name="ListView1$ctrl4$createrTextBox" type="text" id="ListView1_createrTextBox" />
                </td>
                <td>
                    <select name="ListView1$ctrl4$ddlInsertTemplate" id="ListView1_ddlInsertTemplate" onchange="changeVisible(this,ctrl4_LogoTextBox_Logo)">//这里生成的clientId和绿色部分的不一样
<option value="Text">文本链接</option>
<option value="pic">图片</option>
 
</select>

解决方案 »

  1.   

    不要使用ItemCreated事件,这个是不行的,需要使用ItemDataBound事件才能使用ClientID
      

  2.   

    另外m
     ddl.Attributes["onchange"] = "changeVisible(this,"+tb.ClientID+")";
    应该改成
     ddl.Attributes["onchange"] = "changeVisible(this,'"+tb.ClientID+"')";
      

  3.   


    这个我试了下,在ItemDataBound事件中,是不是走insertItemTemplate这个模板的,所以js函数不会被绑定到插入事件中的下拉控件里。
      

  4.   


    这个也不行,加了单引号,渲染到HTML里单引号会被渲染成&#39的符号。
      

  5.   

    http://www.byywee.com/page/M0/S461/461437.html在ItemCreated事件里面 获取到的ClientID 是不正确的 解决办法看上面链接
      

  6.   

    http://topic.csdn.net/u/20110810/21/3071ac73-1ccd-4195-93fa-b227e40f05a8.html如果之前方法不能解决 看这个