程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 更多編程語言 >> 編程解疑 >> contentplaceholder-gridview 放在ContentPlaceHolder下,不能更新

contentplaceholder-gridview 放在ContentPlaceHolder下,不能更新

編輯:編程解疑
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.Page

Protected 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

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved