问题描述
- gridview 放在ContentPlaceHolder下,不能更新
-
gridview 放在ContentPlaceHolder下,为什么点击编辑后的修改,再单击更新后,连Gridview都不显示了。而单独页面就没有任何问题
前台:
<%@ Page Title="MastPage" Language="vb" AutoEventWireup="false" MasterPageFile="~/Site1.Master" CodeBehind="Custmor.aspx.vb" Inherits="CRM_Seetop.Custmor" %><asp:Panel ID="Panel1" runat="server" Visible="True"> <div style="height:30px;text-align:center "> <span style="color: #FF3300">签约客户</span> <span style="color: #0033CC">管理</span>系统! </div> <div> <asp:GridView ID="gvCustmor" runat="server" AutoGenerateColumns="False" DataKeyNames="KID" BackColor="White" BorderColor="#3366CC" BorderStyle="None" BorderWidth="1px" CellPadding="4" onpageindexchanging="gvCustmor_PageIndexChanging" onrowcancelingedit="gvCustmor_RowCancelingEdit" onrowdatabound="gvCustmor_RowDataBound" onrowdeleting="gvCustmor_RowDeleting" onrowediting="gvCustmor_RowEditing" onrowupdating="gvCustmor_RowUpdating" onsorting="gvCustmor_Sorting"> <RowStyle BackColor="White" ForeColor="#003399" /> <Columns> <asp:CommandField HeaderText="编辑" ShowEditButton="True" /> <asp:CommandField HeaderText="删除" ShowDeleteButton="True" /> <asp:BoundField DataField="KID" HeaderText="KID" ReadOnly="True" SortExpression="KID" /> <asp:TemplateField HeaderText="简称" SortExpression="简称"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("简称")%>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("简称")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="ComAbbr" SortExpression="ComAbbr"> <EditItemTemplate> <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("ComAbbr")%>'></asp:TextBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="Label2" runat="server" Text='<%# Bind("ComAbbr")%>'></asp:Label> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="#99CCCC" ForeColor="#003399" /> <PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" /> <SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" /> <HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" /> </asp:GridView> </div>
/asp:Content
后台代码:
Imports System.Data.SqlClient
Public Class Custmor
Inherits System.Web.UI.PageProtected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then gvCustmor.AllowPaging = True gvCustmor.PageSize = 15 gvCustmor.AllowSorting = True ViewState("SortExpression") = "KID ASC" ' Populate the GridView. bind() End If End Sub Public Sub bind() Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("ApplicationServices").ToString()) ' Create a DataSet object. Dim dsCustmor As New DataSet() Dim uN As String = "" If Session("username") Is Nothing Then uN = Page.User.Identity.Name Else uN = Session("username") End If Dim strSelectCmd As String = "SELECT KID,简称,ComAbbr,公司名称,CompanyName,Web,总机电话,直线电话,公司传真,产品分类,保护状态 FROM kData WHERE (负责业务='" + uN + "')" Dim da As New SqlDataAdapter(strSelectCmd, conn) conn.Open() da.Fill(dsCustmor, "Custmor") Dim dvCustmor As DataView = dsCustmor.Tables("Custmor").DefaultView dvCustmor.Sort = ViewState("SortExpression").ToString() gvCustmor.DataSource = dvCustmor gvCustmor.DataBind() End Using End Sub Protected Sub gvCustmor_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) ' Make sure the current GridViewRow is a data row. If e.Row.RowType = DataControlRowType.DataRow Then If e.Row.RowState = DataControlRowState.Normal OrElse e.Row.RowState = DataControlRowState.Alternate Then ' Add client-side confirmation when deleting. DirectCast(e.Row.Cells(1).Controls(0), LinkButton).Attributes("onclick") = "if(!confirm('Are you certain you want to delete this person ?')) return false;" End If End If If gvCustmor.Visible = False Then Panel1.Visible = True gvCustmor.Visible = True End If End Sub Protected Sub gvCustmor_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs) gvCustmor.PageIndex = e.NewPageIndex bind() End Sub Protected Sub gvCustmor_RowEditing(ByVal sender As Object, ByVal e As GridViewEditEventArgs) gvCustmor.EditIndex = e.NewEditIndex bind() End Sub Protected Sub gvCustmor_RowCancelingEdit(ByVal sender As Object, ByVal e As GridViewCancelEditEventArgs) gvCustmor.EditIndex = -1 bind() End Sub Protected Sub gvCustmor_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs) Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString()) Dim cmd As New SqlCommand() cmd.Connection = conn cmd.CommandText = "UPDATE kData SET 简称 = @LastName, ComAbbr = @FirstName WHERE KID = @KID" cmd.CommandType = CommandType.Text Dim strKID As String = gvCustmor.Rows(e.RowIndex).Cells(2).Text Dim strLastName As String = DirectCast(gvCustmor.Rows(e.RowIndex).FindControl("TextBox1"), TextBox).Text Dim strFirstName As String = DirectCast(gvCustmor.Rows(e.RowIndex).FindControl("TextBox2"), TextBox).Text cmd.Parameters.Add("@KID", SqlDbType.Int).Value = strKID cmd.Parameters.Add("@LastName", SqlDbType.NVarChar, 50).Value = strLastName cmd.Parameters.Add("@FirstName", SqlDbType.NVarChar, 50).Value = strFirstName conn.Open() cmd.ExecuteNonQuery() End Using gvCustmor.EditIndex = -1 bind() lbtnAdd.Visible = True End Sub Protected Sub gvCustmor_RowDeleting(ByVal sender As Object, ByVal e As GridViewDeleteEventArgs) Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("SQLServer2005DBConnectionString").ToString()) ' Create a command object. Dim cmd As New SqlCommand() ' Assign the connection to the command. cmd.Connection = conn cmd.CommandText = "DELETE FROM Kdata WHERE KID = @KID" cmd.CommandType = CommandType.Text Dim strKID As String = gvCustmor.Rows(e.RowIndex).Cells(2).Text cmd.Parameters.Add("@KID", SqlDbType.Int).Value = strKID ' Open the connection. conn.Open() ' Execute the command. cmd.ExecuteNonQuery() End Using gvCustmor.EditIndex = -1 ' Rebind the GridView control to show data after deleting. bind() End Sub ' GridView.Sorting Event Protected Sub gvCustmor_Sorting(ByVal sender As Object, ByVal e As GridViewSortEventArgs) Dim strSortExpression As String() = ViewState("SortExpression").ToString().Split(" "c) ' If the sorting column is the same as the previous one, ' then change the sort order. If strSortExpression(0) = e.SortExpression Then If strSortExpression(1) = "ASC" Then ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "DESC" Else ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC" End If Else ' If sorting column is another column, ' then specify the sort order to "Ascending". ViewState("SortExpression") = Convert.ToString(e.SortExpression) & " " & "ASC" End If ' Rebind the GridView control to show sorted data. bind() End Sub End Class
解决方案
问题找到了:
GridView的EnableViewState属性设置为false,GridView的一些事件就不会执行,比如romcommand事件 rowupdating事件等
本文来自:读书人网(http://www.reader8.cn/)原文链接:http://www.reader8.cn/jiaocheng/20120316/1615649.html
解决方案二:
http://bbs.csdn.net/topics/350202683
时间: 2024-07-28 16:51:38