编辑Gridview错误(单击“编辑"按钮页面后变为白色) [英] editing Gridview error ( after clicking edit button page went white)
本文介绍了编辑Gridview错误(单击“编辑"按钮页面后变为白色)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,您在这里遇到了问题,因为您看到我想通过gridview更新数据库.我的问题是,当我按下编辑"按钮时,页面变白了(看起来像是空的).我不知道问题出在哪里.请帮我
这是我的代码
设计源
Hi Guys, Got a problem here, as you see I want to update my database via gridview. my problem is when i hit Edit button, my page went white (looks like empty). I dont know where the problem is . Plss help me
here is my code
Design Source
<pre lang="xml"><%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title>Untitled Page</title>
<style type="text/css">
/* A scrolable div */
.GridViewContainer
{
overflow: auto;
}
/* to freeze column cells and its respecitve header*/
.FrozenCell
{
background-color:yellow;
position: relative;
cursor: default;
left: expression(document.getElementById("GridViewContainer").scrollLeft-2);
}
/* for freezing column header*/
.FrozenHeader
{
background-color:yellow;
position: relative;
cursor: default;
top: expression(document.getElementById("GridViewContainer").scrollTop-2);
z-index: 10;
}
/*for the locked columns header to stay on top*/
.FrozenHeader.locked
{
z-index: 99;
}
</style>
</head>
<body>
<form id="form1" runat="server">
<div id="GridViewContainer" class="GridViewContainer" style="width:789px;height:345px;" >
<asp:GridView ID="GridView1" CssClass="GridView" runat="server"
AutoGenerateColumns="False"
OnRowEditing="GridView1_RowEditing"
OnRowUpdating="GridView1_RowUpdating"
OnRowCancelingEdit = "GridView1_RowCancelingEdit"
Height="338px" Width="784px" >
<HeaderStyle CssClass="FrozenHeader" />
<Columns>
<asp:CommandField ButtonType="Button" ShowEditButton="True"/>
<asp:BoundField DataField="CourseTitle" HeaderText="CourseTitle" >
<HeaderStyle CssClass="FrozenCell" />
<ItemStyle CssClass="FrozenCell" />
</asp:BoundField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%#Eval("ID")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtID" Text=''<%# Eval("ID")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="CourseCode">
<ItemTemplate>
<%#Eval("CourseCode")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtCourseCode" Text=''<%# Eval("CourseCode")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Objectives">
<ItemTemplate>
<%#Eval("Objectives")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtObjectives" Text=''<%# Eval("Objectives")%>'' />
</EditItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Duration">
<ItemTemplate>
<%#Eval("Duration")%>
</ItemTemplate>
<EditItemTemplate>
<asp:TextBox runat="server" ID="txtDuration" Text=''<%# Eval("Duration")%>'' />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
</div>
</form>
</body>
</html>
我的aspx.vb
My aspx.vb
<pre lang="vb">Imports System.Web
Imports System
Imports System.Data
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If (IsPostBack = False) Then
Dim strSQL As String
Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]"
connection.Open()
Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
''myCommand.Parameters.AddWithValue("@CostCenter", drpcc.Text)
GridView1.DataSource = myCommand.ExecuteReader()
GridView1.DataBind()
End If
End Sub
Protected Sub Gridview1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim txtID As TextBox = row.FindControl("txtID")
''Dim txtCourseTitle As TextBox = row.FindControl("txtCourseTitle")
Dim txtCourseCode As TextBox = row.FindControl("txtCourseCode")
Dim txtObjectives As TextBox = row.FindControl("txtObjectives")
Dim txtDuration As TextBox = row.FindControl("txtDuration")
End Sub
Protected Sub Gridview1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
GridView1.DataBind()
End Sub
Protected Sub Gridview1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
GridView1.DataBind()
End Sub
End Class
推荐答案
将所有代码放在page_load事件中的一个函数中,并在gridview_editing事件中而不是gridview1.databind()和gridview_rowcancelingedit
place all code in page_load event in one function and call it in gridview_editing event instead of gridview1.databind() and also in gridview_rowcancelingedit
中调用
<pre lang="vb">Protected Sub Gridview1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating
Dim row As GridViewRow = GridView1.Rows(e.RowIndex)
Dim txtID As TextBox = row.FindControl("txtID")
''Dim txtCourseTitle As TextBox = row.FindControl("txtCourseTitle")
Dim txtCourseCode As TextBox = row.FindControl("txtCourseCode")
Dim txtObjectives As TextBox = row.FindControl("txtObjectives")
Dim txtDuration As TextBox = row.FindControl("txtDuration")
End Sub
Protected Sub Gridview1_RowEditing(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridView1.RowEditing
GridView1.EditIndex = e.NewEditIndex
BindGrid()
End Sub
Protected Sub Gridview1_RowCancelingEdit(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCancelEditEventArgs) Handles GridView1.RowCancelingEdit
GridView1.EditIndex = -1
BindGrid()
End Sub
Public Sub BindGrid()
Dim strSQL As String
Dim connection As SqlClient.SqlConnection = New SqlClient.SqlConnection("Data Source=ML0003135586;Initial Catalog=TestSQL;Integrated Security=True")
strSQL = "SELECT [ID], [CourseTitle], [CourseCode], [Objectives], [Duration] FROM [tblTrainingPlan]"
connection.Open()
Dim myCommand As SqlClient.SqlCommand = New SqlClient.SqlCommand(strSQL, connection)
GridView1.DataSource = myCommand.ExecuteReader()
GridView1.DataBind()
connection.Close()
End Sub
将所有代码放在page_load事件中的一个函数中,并在gridview_editing事件中而不是gridview1.databind()以及gridview_rowcancelingedit中对其进行调用
试试吧!
祝您编码愉快! ^ _ ^
place all code in page_load event in one function and call it in gridview_editing event instead of gridview1.databind() and also in gridview_rowcancelingedit
Try!
Happy Coding! ^_^
这篇关于编辑Gridview错误(单击“编辑"按钮页面后变为白色)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文