asp.net可编辑网格视图“找不到数据”行导致回发后出现问题

时间:2015-03-27 10:00:03

标签: asp.net gridview postback viewstate

我的gridview有一个简单的问题。它是一个可编辑的gridview,允许插入,更新和删除行。它绑定到数据表。我将数据表存储在viewstate中,并在page_load上为每个回发绑定数据表。没有列顺序或分页,我在此gridview上只有几条记录。当没有数据要显示时,我会手动向网格添加一个空信息文本,例如“找不到数据”。它是第一个page_load的okey。但是在回发之后,这个文本消失了,但是行仍然存在。主要问题是在回发后行有“编辑”和“删除”命令列。回发后,此行不应具有编辑或删除列。在回发之前它确实很好。

如果gridview中至少有一行,则不会发生此问题。

如果我至少有一行,那么如果我添加更多行:没问题

如果我有空格,那么我添加一行(在任何回发之前,例如组合框值更改):没问题

如果我有空网格:那么我通过更改组合框中的值进行回发,然后网格中的“没有找到行”消息消失,并且网格中有一个没有文本且有编辑的额外行并删除列

如果我有不需要的行的网格,那么我向该gridview添加一行:不需要的行将消失并且新行出现在网格上。没问题

所以唯一的问题是在回发后出现的额外行。

代码详情如下。请帮我解决这个愚蠢的问题。我好几天都无法解决。

ASPX:

<asp:GridView ID="grdTerminals" runat="server" 
     AutoGenerateColumns="False" DataKeyNames="TRM_ID" 
     OnRowCancelingEdit="grdTerminals_RowCancelingEdit" 
     OnRowDataBound="grdTerminals_RowDataBound" 
     OnRowEditing="grdTerminals_RowEditing" 
     OnRowUpdating="grdTerminals_RowUpdating" ShowFooter="True" 
     OnRowCommand="grdTerminals_RowCommand" 
     OnRowDeleting="grdTerminals_RowDeleting"
    HeaderStyle-BackColor="#73be1e" HeaderStyle-ForeColor="Window" HeaderStyle-Font-Bold="true" Width="500px" HeaderStyle-Height="30">
    <Columns>
        <asp:TemplateField HeaderText="Terminal" HeaderStyle-HorizontalAlign="Left"> 
            <EditItemTemplate> 
                <asp:DropDownList ID="ddlTerminal" runat="server" DataTextField="TRM_MNMC" DataValueField="TRM_ID"> 
                </asp:DropDownList> 
            </EditItemTemplate> 
            <ItemTemplate> 
                <asp:Label ID="lblTerminal" runat="server" Text='<%# Eval("TRM_MNMC") %>'></asp:Label> 
            </ItemTemplate> 
            <FooterTemplate> 
                <asp:DropDownList ID="ddlNewTerminal" runat="server" DataTextField="TRM_MNMC" DataValueField="TRM_ID">
                </asp:DropDownList> 
            </FooterTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="TAS No" HeaderStyle-HorizontalAlign="Left"> 
            <EditItemTemplate> 
                <asp:TextBox ID="txtTASNo" runat="server" Text='<%# Bind("TAS_NO") %>' Width="120"></asp:TextBox> 
            </EditItemTemplate> 
            <FooterTemplate> 
                <asp:TextBox ID="txtNewTASNo" runat="server" Width="120"></asp:TextBox> 
            </FooterTemplate> 
            <ItemTemplate> 
                <asp:Label ID="lblTASNo" runat="server" Text='<%# Bind("TAS_NO") %>' Width="120"></asp:Label> 
            </ItemTemplate> 
        </asp:TemplateField> 
        <asp:TemplateField HeaderText="" ShowHeader="False"> 
            <EditItemTemplate> 
                <asp:LinkButton ID="lbkUpdate" runat="server" CausesValidation="True" CommandName="Update" Text="Kaydet"></asp:LinkButton> 
                <asp:LinkButton ID="lnkCancel" runat="server" CausesValidation="False" CommandName="Cancel" Text="Cancel"></asp:LinkButton> 
            </EditItemTemplate> 
            <FooterTemplate> 
                <asp:LinkButton ID="lnkAdd" runat="server" CausesValidation="False" CommandName="Insert" Text="Insert"></asp:LinkButton> 
            </FooterTemplate> 
            <ItemTemplate> 
                <asp:LinkButton ID="lnkEdit" runat="server" CausesValidation="False" CommandName="Edit" Text="Edit"></asp:LinkButton> 
            </ItemTemplate> 
        </asp:TemplateField> 

        <asp:CommandField HeaderText="" ShowDeleteButton="True" ShowHeader="True" DeleteText="Delete"/>
    </Columns> 
</asp:GridView>

C#

// I am storing the data in ViewState
        DataTable dtTerminals
        {
            get
            {
                return ViewState["_dtVehicleTerminals"] as DataTable;
            }
            set
            {
                ViewState["_dtVehicleTerminals"] = value;
            }
        }
//Page Load
        protected void Page_Load(object sender, EventArgs e)
        {
            if (dtTerminals == null)
            {
                dtTerminals = new DataTable();
                dtTerminals.Columns.Add("TRM_ID");
                dtTerminals.Columns.Add("TRM_MNMC");
                dtTerminals.Columns.Add("TAS_NO");
            }
            if (!IsPostBack)
            {
                string VhcId = Request["vhc_id"];//Edit mode
                if (!string.IsNullOrEmpty(VhcId))
                {
                    BindTerminalInfo(VhcId);
                }
                else
                {
                    //To show empty grid
                    BindTerminalGrid();
                }
            }
// Bind Terminal Info
        void BindTerminalInfo(string VhcId)
        {
            dtTerminals = VehicleManager.GetVehicleTerminals(VhcId);
            BindTerminalGrid();
        }
//Bind Terminal Grid
        public void BindTerminalGrid()
        {
            if (dtTerminals.Rows.Count > 0)
            {
                grdTerminals.DataSource = dtTerminals;
                grdTerminals.DataBind();
            }
            else
            {
                //Show No Records Found
                dtTerminals.Rows.Add(dtTerminals.NewRow());

                grdTerminals.DataSource = dtTerminals;
                grdTerminals.DataBind();

                grdTerminals.Rows[0].Cells.Clear();
                grdTerminals.Rows[0].Cells.Add(new TableCell());
                grdTerminals.Rows[0].Cells[0].Text = "No Data found";
                dtTerminals.Rows.Clear();
            }
        }
//Other methods related to grid functionality
        protected void grdTerminals_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                DropDownList ddlTerminal = (DropDownList)e.Row.FindControl("ddlTerminal");
                if (ddlTerminal != null)
                {
                    ddlTerminal.DataSource = VehicleManager.GetDefaultUserTerminalNames();
                    ddlTerminal.DataBind();
                    ddlTerminal.DataTextField = "TRM_MNMC";
                    ddlTerminal.DataValueField = "TRM_ID";
                    ddlTerminal.SelectedValue = grdTerminals.DataKeys[e.Row.RowIndex].Values[0].ToString();
                }
            }
            if (e.Row.RowType == DataControlRowType.Footer)
            {
                DropDownList ddlNewTerminal = (DropDownList)e.Row.FindControl("ddlNewTerminal");
                ddlNewTerminal.DataSource = VehicleManager.GetDefaultUserTerminalNames();
                ddlNewTerminal.DataBind();
            }
        }
        protected void grdTerminals_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            grdTerminals.EditIndex = -1;
            BindTerminalGrid();
        }
        protected void grdTerminals_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            Label lblId = (Label)grdTerminals.Rows[e.RowIndex].FindControl("lblTrmUnqId");
            DropDownList ddlTerminal = (DropDownList)grdTerminals.Rows[e.RowIndex].FindControl("ddlTerminal");
            TextBox txtTASNo = (TextBox)grdTerminals.Rows[e.RowIndex].FindControl("txtTASNo");

            dtTerminals.Rows[e.RowIndex]["TRM_ID"] = ddlTerminal.SelectedValue;
            dtTerminals.Rows[e.RowIndex]["TRM_MNMC"] = ddlTerminal.SelectedItem;
            dtTerminals.Rows[e.RowIndex]["TAS_NO"] = txtTASNo.Text;

            grdTerminals.EditIndex = -1;
            BindTerminalGrid();

        }
        protected void grdTerminals_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            string TrmId = Convert.ToString(grdTerminals.DataKeys[e.RowIndex].Values[0]);

            dtTerminals.Rows.Remove(dtTerminals.Rows[e.RowIndex]);

            BindTerminalGrid();
        }
        protected void grdTerminals_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.Equals("Insert"))
            {
                DropDownList ddlNewTerminal = (DropDownList)grdTerminals.FooterRow.FindControl("ddlNewTerminal");
                TextBox txtTASNo = (TextBox)grdTerminals.FooterRow.FindControl("txtNewTASNo");

                DataRow dr = dtTerminals.NewRow();
                dr["TRM_ID"] = ddlNewTerminal.SelectedValue;
                dr["TRM_MNMC"] = ddlNewTerminal.SelectedItem.Text;
                dr["TAS_NO"] = txtTASNo.Text;
                dtTerminals.Rows.Add(dr);
                BindTerminalGrid();
            }
        }
        protected void grdTerminals_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdTerminals.EditIndex = e.NewEditIndex;
            BindTerminalGrid();
        }

2 个答案:

答案 0 :(得分:0)

我不确定这是正确的解决方案。但是尝试添加Gridview的Empty data text属性。

<asp:gridview id="CustomersGridView" 
    datasourceid="CustomersSqlDataSource" 
    autogeneratecolumns="true"
    emptydatatext="No data in the data source."
    runat="server">

    <emptydatarowstyle backcolor="LightBlue"
      forecolor="Red"/>

  </asp:gridview>

或尝试这样

 <asp:TemplateField>
            <EditItemTemplate>
                <asp:ImageButton ID="imgBtnUpdate" runat="server" Height="32px" 
                    ImageUrl="~/Images/Update.jpg" ToolTip="Update" Width="32px" CommandName="Update"/>
                <asp:ImageButton ID="imgBtnDelete" runat="server" Height="32px" 
                    ImageUrl="~/Images/delete.gif" style="margin-left: 0px" ToolTip="Delete" 
                    Width="32px" CommandName="Delete"/>
                <asp:ImageButton ID="imgBtnCancel" runat="server" Height="32px" 
                    ImageUrl="~/Images/Cancel.png" ToolTip="Cancel" Width="32px" CommandName="Cancel"/>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:ImageButton ID="btnAdd" runat="server" ImageUrl="~/Images/AddNew.gif" 
                     ToolTip="Add New Record" CommandName="Add"/>
            </FooterTemplate>
            <ItemTemplate>
                <asp:ImageButton ID="imgBtnEdit" runat="server" Height="32px" 
                    ImageUrl="~/Images/pencil.png" ToolTip="Edit" Width="32px" CommandName="Edit"/>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Name">
            <EditItemTemplate>
                <asp:TextBox ID="txtName" runat="server" Text='<%# Eval("name") %>'></asp:TextBox>
            </EditItemTemplate>
            <FooterTemplate>
                <asp:TextBox ID="txtNameFooter" runat="server"></asp:TextBox>
            </FooterTemplate>
            <ItemTemplate>
                <asp:Label ID="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
            </ItemTemplate>

答案 1 :(得分:0)

最后我解决了这个问题。解决方案是添加

        if (!Request.Form["__EVENTTARGET"].Split('$').Contains(grdTerminals.ID))
        {
            BindTerminalGrid();
        }

代码块到If(Postback)语句。我应该为每个后备文件重新绑定数据,但前提是导致回发的控件不是“datagrid”。