编辑时Gridview消失,并在gridview中单击更新按钮 [英] Gridview disappear while edit and click update button in the gridview

查看:81
本文介绍了编辑时Gridview消失,并在gridview中单击更新按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Gridview disappear click edit field in the gridview and click update button. 

if remove ispostback from page load and run the solution then click update button, gridview is not dissappear but previous values coming from gridview, if i check with break point





我尝试过:



来源COde

___________







What I have tried:

Source COde
___________


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Flow.aspx.cs" Inherits="WebApplication1.Flow" %>

<!DOCTYPE html>



    <title>
    

.headerstyle
{
color:#FFFFFF;border-right-color:#abb079;border-bottom-color:#abb079;background-color: #df5015;padding:0.5em 0.5em 0.5em 0.5em;text-align:center;
}
    


    
    <div>
    <asp:TextBox ID="txtpname" runat="server" /><asp:TextBox ID="txtprice" runat="server" />
<asp:Button ID="btnAdd" runat="server" Text="Add Product" OnClick="btnAdd_Click" />
    </div>
<asp:GridView runat="server" ID="gvDetails" ShowFooter="true" AllowPaging="true" PageSize="10"
     AutoGenerateColumns="false" DataKeyNames="productid,productname,price"
     OnPageIndexChanging="gvDetails_PageIndexChanging" OnRowCancelingEdit="gvDetails_RowCancelingEdit"
OnRowEditing="gvDetails_RowEditing" OnRowUpdating="gvDetails_RowUpdating" 
    OnRowDeleting="gvDetails_RowDeleting" EnableViewState="False"
      >
<HeaderStyle CssClass="headerstyle" />
<columns>
<asp:BoundField DataField="productid" HeaderText="Product Id" ReadOnly="true" />
<asp:TemplateField HeaderText="Product Name">
<itemtemplate>
<asp:Label ID="lblProductname" runat="server" Text='<%# Eval("productname")%>'/>

<edititemtemplate>
<asp:TextBox ID="txtProductname" runat="server" Text='<%# Eval("productname")%>'/>


<asp:TemplateField HeaderText = "Price">
<itemtemplate>
<asp:Label ID="lblPrice" runat="server"  Text='<%# Eval("price")%>'>

<edititemtemplate>
<asp:TextBox ID="txtProductprice" runat="server" Text='<%# Eval("price")%>'/>


<asp:CommandField ShowEditButton="True" ShowDeleteButton="true" />


    



Flow.aspx.cs
______________

<pre>using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebApplication1.Account;
using System.Data;
using System.Data.SqlClient;

namespace WebApplication1
{
    public partial class Flow : System.Web.UI.Page
    {
        BEL objUserBEL = new BEL();
        string Output = string.Empty;
        DataSet ds=new DataSet();
        BLL objUserBLL = new BLL();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                BindGridview();
            }
            
            
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {

            objUserBEL.productname = txtpname.Text;
            objUserBEL.price = txtprice.Text;
            objUserBEL.op_type = "INSERT";
            Output = objUserBLL.InsertUserDetails(objUserBEL);
        }
        protected void BindGridview()
        {
            objUserBEL.op_type = "SELECT";
            
            ds = objUserBLL.GetUserDetails(objUserBEL);
            if (ds.Tables[0].Rows.Count > 0)
            {
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
            }
            else
            {
                ds.Tables[0].Rows.Add(ds.Tables[0].NewRow());
                gvDetails.DataSource = ds;
                gvDetails.DataBind();
                int columncount = gvDetails.Rows[0].Cells.Count;
                gvDetails.Rows[0].Cells.Clear();
                gvDetails.Rows[0].Cells.Add(new TableCell());
                gvDetails.Rows[0].Cells[0].ColumnSpan = columncount;
                gvDetails.Rows[0].Cells[0].Text = "No Records Found";
            }
        }
        protected void gvDetails_RowEditing(object sender, GridViewEditEventArgs e)
        {
            gvDetails.EditIndex = e.NewEditIndex;
            BindGridview();
        }
        protected void gvDetails_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvDetails.EditIndex = -1;
            BindGridview();
        }
        protected void gvDetails_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvDetails.PageIndex = e.NewPageIndex;
            BindGridview();
        }
        protected void gvDetails_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            int productid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["productid"].ToString());
            string productname = gvDetails.DataKeys[e.RowIndex].Values["productname"].ToString();
            string price = gvDetails.DataKeys[e.RowIndex].Values["price"].ToString();
            objUserBEL.price = price;
            objUserBEL.productname = productname;
            objUserBEL.op_type="UPDATE";
            objUserBEL.productid=productid;
            Output=objUserBLL.updatecrudoperations(objUserBEL);
            BindGridview();
           
        }
        protected void gvDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            int productid = Convert.ToInt32(gvDetails.DataKeys[e.RowIndex].Values["productid"].ToString());
            string productname = gvDetails.DataKeys[e.RowIndex].Values["productname"].ToString();
            
            objUserBEL.price=txtprice.Text;
            objUserBEL.productname=productname;
            objUserBEL.op_type="DELETE";
            objUserBEL.productid=productid;
            Output=objUserBLL.deletecrudoperations(objUserBEL);
            BindGridview();
        }
       
        }

    }

推荐答案

最好的事情你会为这种情况做的是 debug 你的代码。在点击 RowUpdating 断点设置为 BindGridView()方法c $ c>事件然后逐步进入您的代码。检查从这一行返回的数据源: ds = objUserBLL.GetUserDetails(objUserBEL);



请记住数据表示控件(如 GridView )将仅显示数据并在页面中呈现(如果您设置的数据源上有数据。
The best thing you would do for this situation is to debug your code. Set a break-point to your BindGridView() method after hitting the RowUpdating event then step-into your code. Check the datasource returned from this line: ds = objUserBLL.GetUserDetails(objUserBEL);

Keep in mind that a data representation control such as GridView will only display data and rendered in the page if there's data on the datasource that you set.


Private Sub gvDetails_RowCommand(sender as Object,e As GridViewCommandEventArgs)处理gvDetails.RowCommand

BindGridview()

End Sub
Private Sub gvDetails_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles gvDetails.RowCommand
BindGridview()
End Sub


这篇关于编辑时Gridview消失,并在gridview中单击更新按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆