C# Can';t编辑列表视图上的文本框

C# Can';t编辑列表视图上的文本框,c#,asp.net,listview,textbox,C#,Asp.net,Listview,Textbox,我的文本框或我的大脑有些问题。我不明白不可编辑文本框的原因是什么。我的意思是,我的listview有3列:“名称”、“代码”、“绑定”。 单击“名称”,第一列和第二列将变得可编辑。编辑之后,我按下“保存”按钮……什么也没发生。我不能理解这么简单的事情。为什么它不起作用? 对不起,我犯了错误,我是第一次来这里 <asp:Content ID="ContentMain" ContentPlaceHolderID="centerContent" runat="server"> &l

我的文本框或我的大脑有些问题。我不明白不可编辑文本框的原因是什么。我的意思是,我的listview有3列:“名称”、“代码”、“绑定”。 单击“名称”,第一列和第二列将变得可编辑。编辑之后,我按下“保存”按钮……什么也没发生。我不能理解这么简单的事情。为什么它不起作用? 对不起,我犯了错误,我是第一次来这里

<asp:Content ID="ContentMain" ContentPlaceHolderID="centerContent" runat="server">
    <asp:Panel ID="i_ListContainer" runat="server" HorizontalAlign="Center">
        <asp:ListView ID="CpzListView" runat="server" DataKeyNames="id" InsertItemPosition="FirstItem"
            OnItemCreated="i_UserLV_ItemCreated" OnItemInserted="i_UserLV_ItemInserted" OnItemEditing="i_UserLV_ItemEditing"
            OnItemInserting="i_UserLV_ItemInserting" OnItemUpdated="i_UserLV_ItemUpdated"
            OnItemUpdating="i_UserLV_ItemUpdating">
            <LayoutTemplate>
                <table cellpadding="0" cellspacing="0" class="mGrid">
                    <th style="width: 150px">
                        Name
                    </th>
                    <th style="width: 150px">
                        Code
                    </th>
                    <th style="width: 50px">
                        Binding
                    </th>
                    </thead>
                    <tbody>
                        <asp:PlaceHolder runat="server" ID="itemPlaceholder" />
                    </tbody>
                </table>
            </LayoutTemplate>
            <ItemTemplate>
                <tr>
                    <td>
                        <asp:LinkButton ID="i_viewEditLB" runat="server" CommandName="Edit" Text='<%#Bind("cpzname") %>' />
                    </td>
                    <td>
                        <asp:Label ID="i_viewUserFioLabel" runat="server" Text='<%#Bind("code") %>' />
                    </td>
                    <td style="text-align: center; width: 80px">
                        <a href='<%# GetUrl(Eval("cpzname"), Eval("code"))%>'>Bindindg</a>
                    </td>
                </tr>
            </ItemTemplate>
            <InsertItemTemplate>
            </InsertItemTemplate>
            <EditItemTemplate>
                <tr>
                    <td>
                        <asp:TextBox ID="tbCpzName" runat="server" Text='<%# Bind("cpzname") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:TextBox ID="tbCpzCode" runat="server" Text='<%# Bind("code") %>'></asp:TextBox>
                    </td>
                    <td>
                        <asp:LinkButton ID="lbSave" runat="server" CommandName="Update" Text="Save" />
                    </td>
                </tr>
            </EditItemTemplate>
        </asp:ListView>
    </asp:Panel>
</asp:Content>

你的代码没有问题。你错过了一件小事

要始终获取实际数据,需要设置EnableViewState=“false”。主要的是,仅仅为特定的输入设置这一点是不够的。只有在我像这样设置页面级视图状态之后,这才对我有效

 <%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" 
    AutoEventWireup="true"
CodeBehind="Default.aspx.cs" Inherits="WebApplication8._Default" 
EnableViewState="false" %>

您应该编写如下页面加载方法代码:

 protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
            return;
        UpdateBinding();
    }

在您的代码中,编辑的数据在编辑代码运行之前丢失,因为当您单击“保存”按钮时,将执行第一页加载,并将列表视图与旧数据绑定(UpdateBind),然后将执行i_UserLV_ItemUpdate。break point将帮助您解除重新启动及其工作方式。

感谢EnableViewState=“false”实际上帮助了工作,但并不像我预期的那样。问题是我有类似的页面,页面设置相同,功能正常,但此页面不是<代码>这是更好的方法:如果(!IsPostBack)UpdateBind();
 protected void Page_Load(object sender, EventArgs e)
    {
        if(IsPostBack)
            return;
        UpdateBinding();
    }