0

不确定这是否是一个奇怪的,但我有一个在行编辑时消失的网格视图,

我的步骤是>单击butUsers>单击编辑>网格消失>单击butDev>网格返回>单击ButUsers>网格加载我在编辑模式中选择的项目。

我的活动缺少什么?

谢谢

代码隐藏

using System;
using System.Collections.Generic;
using System.Linq;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class equipment : System.Web.UI.Page
{
    public string strMode;
    ParameterCollection pcEquip;

    dsData.tblEquipmentDataTable dtEquipment;
    dsDataTableAdapters.tblEquipmentTableAdapter taEquipment;
    SqlDataSource dsEquipment;

    protected void Page_Load(object sender, EventArgs e)
    {
        dtEquipment = new dsData.tblEquipmentDataTable();
        taEquipment = new dsDataTableAdapters.tblEquipmentTableAdapter();
        dsEquipment = new SqlDataSource();

        smEquipment.RegisterAsyncPostBackControl(butUsers);
    }
    protected void butUsers_Click(object sender, EventArgs e)
    {
        hidMode.Value = "Users";
        dtEquipment.Clear();
        taEquipment.FillByUsers(dtEquipment);
        gvItems.DataSource = dtEquipment;
        gvItems.DataBind();

        upTable.Update();
    }
    protected void butDevelopment_Click(object sender, EventArgs e)
    {
        //hidMode.Value = "Users";
        dtEquipment.Clear();
        taEquipment.FillByDev(dtEquipment);
        gvItems.DataSource = dtEquipment;
        gvItems.DataBind();

        upTable.Update();
    }
    protected void gvItems_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    }
    protected void gvItems_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvItems.EditIndex = e.NewEditIndex;
        gvItems.DataSource = dtEquipment;
        gvItems.DataBind();
        upTable.Update();

    }
}

ASPX 页面

<asp:Content ID="cntCenter" ContentPlaceHolderID="CenterContent" runat="Server">
    <asp:UpdatePanel runat="server" ID="upTable" UpdateMode="Conditional">
        <ContentTemplate>
            <asp:HiddenField runat="server" ID="hidMode" />
            <grid:TemplateGridView
                EnableTemplateEditing="True"
                runat="server" ID="gvItems" OnRowDataBound="gvItems_RowDataBound" OnRowEditing="gvItems_RowEditing"
                AutoGenerateColumns="False" DataKeyNames="ID" AllowPaging="True" 
                AllowSorting="True" CssClass="table" BorderStyle="None" BorderWidth="0px"
                GridLines="None">
                <Columns>
                    <asp:CommandField ShowEditButton="True" ShowDeleteButton="True" />
                    <asp:BoundField DataField="ID" HeaderText="ID" InsertVisible="False"
                        ReadOnly="True" SortExpression="ID" Visible="False" />
                    <asp:BoundField DataField="UserID" HeaderText="UserID"
                        SortExpression="UserID" Visible="False" />
                    <asp:BoundField DataField="AssetNo" HeaderText="Asset No" 
                        SortExpression="AssetNo" />
                    <asp:BoundField DataField="Username" HeaderText="User Name"
                        SortExpression="Username" />
                </Columns>
                <EditItemTemplate>
                    <div class="details">
                        <asp:HiddenField ID="hidID" runat="server" Value='<%# Eval("ID") %>' />
                        <asp:HiddenField ID="hidUserID" runat="server" ClientIDMode="Static" Value='<%# Bind("UserID") %>' />
                        <table class="detailsview" cellpadding="0" cellspacing="0">
                            <tr>
                                <td class="td_title_align">Asset No:</td>
                                <td>
                                    <asp:TextBox ID="txtAssetNo" runat="server" CssClass="edit_box" Text='<%# Bind("AssetNo") %>' />
                                </td>
                                <td class="td_title_align">CPU:</td>
                                <td>
                                    <asp:TextBox ID="txtCPU" runat="server" CssClass="edit_box" Text='<%# Bind("CPU") %>' />
                                </td>
                                <td class="td_title_align">Shipped Date:</td>
                                <td>
                                    <asp:TextBox ID="txtShippedDate" runat="server" CssClass="edit_box" Text='<%# Bind("ShippedDate", "{0:dd/MM/yyyy}") %>' />
                                </td>
                            </tr>
                            <tr>
                                <td colspan="8">
                                    <div class="footer_command">
                                        <asp:Button runat="server" ID="btnUpdate" CommandName="Update" Text="Update" CssClass="command_button" />
                                        <asp:Button runat="server" ID="btnCancel" CommandName="Cancel" Text="Cancel" CssClass="command_button" />
                                    </div>
                            </tr>
                        </table>
                    </div>
                </td>
            </tr>
                </EditItemTemplate>
            </grid:TemplateGridView>
        </ContentTemplate>
    </asp:UpdatePanel>
</asp:Content>
4

1 回答 1

1

您只需在 pageLoad 期间绑定一次。我认为此代码块在您当前代码中的每个 Postback 上执行,因此这可能会重新绑定您的数据表。您可以通过在调试模式下在页面加载上放置一个断点来检查(这将在每次回发时调用)。这可能会解决您的问题

 protected void Page_Load(object sender, EventArgs e)
    {
       if(!Page.IsPostback)
       {
         dtEquipment = new dsData.tblEquipmentDataTable();
        taEquipment = new dsDataTableAdapters.tblEquipmentTableAdapter();
        dsEquipment = new SqlDataSource();
        smEquipment.RegisterAsyncPostBackControl(butUsers);
        }
    }

编辑 我从您的代码中可以理解的是,您正在初始化数据表和表适配器pageLoad。现在,我们将初始化代码放入其中,if(!Page.IsPostBack)您将获得空引用,因为它们尚未初始化(它们已在 PageLoad 事件中初始化)。

记住 HTTP 是无状态的

. 要在每次回发时保存这些表的状态,您可以使用 Viewstate 或 Session。

于 2013-05-07T16:20:55.180 回答