asp.net DataGrid 多行编辑出现问题

我想实现Datagrid的多行编辑,但是点击CheckBox控件无反应,提交以后和原来的值一样。源代码如下,请各位高人指点错在哪里。

<%@ Page Language="vb" AutoEventWireup="false" Codebehind="SelectNew.aspx.vb" Inherits="YQLJ.SelectNew"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <title>SelectNew</title>
  <meta name="GENERATOR" content="Microsoft Visual Studio .NET 7.1">
  <meta name="CODE_LANGUAGE" content="Visual Basic .NET 7.1">
  <meta name="vs_defaultClientScript" content="JavaScript">
  <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
 </HEAD>
 <body>
  <form id="Form1" method="post" runat="server">
   <FONT face="宋体">
    <asp:DataGrid id="DataGrid1" runat="server" BorderColor="White" BorderStyle="Ridge" CellSpacing="1"
     BorderWidth="2px" BackColor="White" CellPadding="3" GridLines="None" AutoGenerateColumns="False"
     Width="848px">
     <SelectedItemStyle Font-Bold="True" ForeColor="White" BackColor="#9471DE"></SelectedItemStyle>
     <ItemStyle ForeColor="Black" BackColor="#DEDFDE"></ItemStyle>
     <HeaderStyle Font-Bold="True" ForeColor="#E7E7FF" BackColor="#4A3C8C"></HeaderStyle>
     <FooterStyle ForeColor="Black" BackColor="#C6C3C6"></FooterStyle>
     <Columns>
      <asp:BoundColumn DataField="ID" HeaderText="ID"></asp:BoundColumn>    (说明:ID 为数据库中的主键) 
      <asp:BoundColumn DataField="HL" HeaderText="HL"></asp:BoundColumn>    (说明:HL 为数据库中的描述列)
      <asp:TemplateColumn HeaderText="Title">
       <ItemTemplate>
        <asp:CheckBox id=CheckBox1 runat="server" Checked='<%# DataBinder.eval(Container.DataItem,"Valigate") %>'>    (说明:Valigate为数据库中的Boolen类型的列)
        </asp:CheckBox>
       </ItemTemplate>
      </asp:TemplateColumn>
     </Columns>
     <PagerStyle HorizontalAlign="Right" ForeColor="Black" BackColor="#C6C3C6"></PagerStyle>
    </asp:DataGrid>
    <P>
   </FONT>
   <asp:Button id="Button1" runat="server" Text="生成数据"></asp:Button></P>
   <P><FONT face="宋体"></FONT></P>
  </form>
 </body>
</HTML>


后台代码如下:


Imports System
Imports System.Data
Imports System.Data.OleDb


Public Class SelectNew
    Inherits System.Web.UI.Page

#Region " Web 窗体设计器生成的代码 "

    '该调用是 Web 窗体设计器所必需的。
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents DataGrid1 As System.Web.UI.WebControls.DataGrid
    Protected WithEvents Button1 As System.Web.UI.WebControls.Button

    '注意: 以下占位符声明是 Web 窗体设计器所必需的。
    '不要删除或移动它。
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: 此方法调用是 Web 窗体设计器所必需的
        '不要使用代码编辑器修改它。
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        BindData()
    End Sub


    Sub BindData()
        Dim connection As New OleDbConnection(ConnectionString)

        Dim adapter As New OleDbDataAdapter("select * from YQLJTable", connection)
        Dim dataset As New DataSet
        adapter.Fill(dataset, "myTable")
        DataGrid1.DataSource = dataset.Tables("myTable").DefaultView
        DataGrid1.DataBind()
    End Sub


    Private ReadOnly Property ConnectionString() As String
        Get
            Return ConfigurationSettings.AppSettings("MM_CONNECTION_STRING_YQLJConnection")
        End Get
    End Property

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim value1 As String
        Dim Item As DataGridItem           '定义DataGrid对象,用于定义DataGrid的每一行.

        Dim MyControl As CheckBox           '把 MyControl 定义为 CheckBox 控件.

        Dim connection As New OleDbConnection(ConnectionString)
        Dim command As New OleDbCommand
        connection.Open()

        For Each Item In DataGrid1.Items

            MyControl = Item.FindControl("CheckBox1")   'MyControl从每行的CheckBox控件中获取值.

 

            If (Not MyControl Is Nothing) Then

                If MyControl.Checked = True Then
                    value1 = MyControl.Checked.TrueString

                Else
                    value1 = MyControl.Checked.FalseString
                End If

                Response.Write("value=" + value1 + "<br>")     '我在这里写出出value1的值,查找问题的出处,发现Value1的值不会因为用户选取CheckBox1而改变,只是按照数据库提取出来的值显示。
            Else

                Response.Write("Control not exist ...")

            End If


            Dim id As String = Item.Cells(0).Text
            command.Connection = connection
            command.CommandText = "update  YQLJTable  set valigate =" + value1 + " where ID  =" + id
            command.ExecuteNonQuery()

        Next Item

        connection.Close()

        BindData()

    End Sub
End Class

posted on 2004-11-17 19:07  Neo0820  阅读(2249)  评论(1编辑  收藏  举报

导航