当前位置:编程学习 > C#/ASP.NET >>

循环repeater取ItemTemplate出错,请高手指点。

运行到此行: string str_Name = (item.FindControl("Text1") as TextBox).Text;
错误如下:Object reference not set to an instance of an object.
 

<table cellspacing="1" class="style1">
                <asp:Repeater ID="Rep1" runat="server" OnItemCommand="Rep1_ItemCommand">
                    <ItemTemplate>
                        <tr>
                            <td>
                                <%# Container.ItemIndex + 1%>
                            </td>
                            <td>
                                <asp:TextBox ID="Tex1" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox><img
                                    alt="" src="Images/logo.jpg" />
                            </td>
                            <td>
                                <asp:TextBox ID="Text2" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                            </td>
                            <td>
                                <asp:TextBox ID="Text3" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                            </td>
                        </tr>
                    </ItemTemplate>
                </asp:Repeater><br />
                <asp:Button ID="btn_Get" runat="Server" OnClick="btn_Get_Click" Text="遍历" />
                <tr>
                    <td>
                        <br />
                    </td>
                </tr>
            </table>     
   /// <summary>
        /// 遍历
        /// </summary> 
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btn_Get_Click(object sender, EventArgs e)
        {
            foreach (RepeaterItem item in this.Rep1.Items)
            {
                string str_Name = (item.FindControl("Text1") as TextBox).Text;
                string str_Email = (item.FindControl("Text2") as TextBox).Text;
                string str_address = (item.FindControl("Text3") as TextBox).Text;

                string str_Message = "ID:" + str_Name + "Address:" + str_address;
                Response.Write("<script>alert('" + str_Message + "')</script>");
            } 
        } --------------------编程问答--------------------

//批量移除至垃圾箱事件
        protected void btnMoveToRecy_Click(object sender, EventArgs e)
        {
            
            string idList = "";//所选id值,GUID也可以使用这样的方式

            for (int i = 0; i < this.rptCards.Items.Count; i++)
            {
//rptCards -- > asp:Repeater 
                CheckBox cbos = (CheckBox)this.rptCards.Items[i].FindControl("cboCard");
                if (cbos==null)
                {
                    lblAlert.Text = "cbos不存在!";
                    return;
                }
                if (cbos.Checked)//刷选呗选中的,累加
                {
                    idList += cbos.ToolTip + ",";//把ID值绑定到CheckBox的ToolTip中,累加id值
                }
            }

            UpdateState(idList);
        }

--------------------编程问答-------------------- response.write是不是出这个上面,你把这个语句放到foreach外面看下 --------------------编程问答-------------------- 楼主,谢谢你啊
  看到你的代码给我解决另外一个问题,Repeater 三个嵌套使用的问题。
但看了你的问题你先看<asp:TextBox ID="Tex1" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox><img
  alt="" src="Images/logo.jpg" />
此处Id='Tex1'

而你代码的Id为Text1,所以找不到这个ID,就会报未将对象引用到实例的错误
 希望能解决你的问题啊 --------------------编程问答-------------------- 你写错啦,看下面红色部分

<asp:TextBox ID="Tex1" runat="server" Text='<%#Eval("Name") %>'></asp:TextBox><img
  alt="" src="Images/logo.jpg" />


protected void btn_Get_Click(object sender, EventArgs e)
  {
  foreach (RepeaterItem item in this.Rep1.Items)
  {
  string str_Name = (item.FindControl("Text1") as TextBox).Text;
  string str_Email = (item.FindControl("Text2") as TextBox).Text;
  string str_address = (item.FindControl("Text3") as TextBox).Text;

  string str_Message = "ID:" + str_Name + "Address:" + str_address;
  Response.Write("<script>alert('" + str_Message + "')</script>");
  } 
  }

--------------------编程问答-------------------- 你ID错了。。。所以找不到对象了。。。。 --------------------编程问答-------------------- ID错误 --------------------编程问答-------------------- --------------------编程问答-------------------- <asp:TextBox ID="Tex1"======这个ID号错了
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,