<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
    </head>
<body>
    <form id="form1" runat="server">
    <div>
    
        
    
        <asp:DataList ID="DataList1" runat="server" Width="100%" BackColor="White" 
            BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4" 
            DataSourceID="SqlDataSource1" ForeColor="Black" GridLines="Vertical" 
            onselectedindexchanged="DataList1_SelectedIndexChanged" 
            OnCancelCommand="DataList1_CancelCommand" OnEditCommand="DataList1_EditCommand" 
            OnUpdateCommand="DataList1_UpdateCommand" style="margin-top: 0px" >
            <FooterStyle BackColor="#CCCC99" />
            <AlternatingItemStyle BackColor="White" />
            <ItemStyle BackColor="#F7F7DE" />
            <EditItemTemplate>
                <table style="width:100%;">
                    <tr>
                        <td width="30%">
                            <asp:TextBox ID="TextBox1" runat="server" Width="100%" 
                                Text='<%# Eval("pcode") %>'></asp:TextBox>
                        </td>
                        <td  width="30%">
                            <asp:TextBox ID="TextBox2" runat="server"  Width="100%" 
                                Text='<%# Eval("ItemNAME") %>'></asp:TextBox>
                        </td>
                        <td  width="40%">
                            <asp:LinkButton ID="LinkButton3" runat="server" CommandName="update">更新</asp:LinkButton>
                        </td>
                    </tr>
                </table>
            </EditItemTemplate>
            <SelectedItemStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
            <HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
            <ItemTemplate>
                <table style="width:100%;">
                    <tr>
                        <td width="30%">
                            <%# Eval("pcode")%> </td>
                        <td width="30%">
                           <%# Eval("ItemNAME")%> </td>
                        <td width="40%">
                            <asp:LinkButton ID="LinkButton1" runat="server" CommandName="edit">编辑</asp:LinkButton><asp:LinkButton
                                ID="LinkButton2" runat="server" CommandName="cancel" 
                               >取消</asp:LinkButton></td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:DataList>
    
        
    
    </div>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:Blwy BarCodeConnectionString %>" 
        SelectCommand="SELECT [pcode], [ItemNAME] FROM [CODE_Pcode_create ] WHERE ([OrderNo] = @OrderNo)">
        <SelectParameters>
            <asp:Parameter DefaultValue="1001A012-1/1" Name="OrderNo" Type="String" />
        </SelectParameters>
    </asp:SqlDataSource>
    </form>
</body>
</html>using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {    }
    protected void DataList1_CancelCommand(object source, DataListCommandEventArgs e)
    {    }
    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {    }
    protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
    {
        Response.Write(((TextBox)(e.Item.FindControl("TextBox1"))).Text);
    }
    protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
    {
               DataList1.EditItemIndex = e.Item.ItemIndex;
        DataList1.DataBind();
      Response.Write("<script language='javascript'>((TextBox)(e.Item.FindControl('TextBox1'))).Text</script>);
    }
    
}
 总是提示这行(TextBox)(e.Item.FindControl('TextBox1')错误, 未将对象的引用设置到对象的实例。

解决方案 »

  1.   

    总感觉这种写法是拖出来的,我不建议这样写
    LZ依赖性有点强哦至于你说的(TextBox)(e.Item.FindControl('TextBox1')为null
    说明找不到textbox这个控件,试着e.Item.FindControl别的控件试试
      

  2.   

    本人一向不推荐 使用这类控件说多了
    ===========================
    找不到是因为EditItemTemplate 里面有2个TextBox
      

  3.   

    建议楼主使用 repeter自己写代码  好控制不容易出错
      

  4.   

    建议楼主改掉用sqldatasource的习惯 
      

  5.   

    自己写数据源
        protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
        {              string text= ((TextBox)e.Item.FindControl("TextBox1")).Text;
        }得不到编辑后的数据,取到的还是原来