update.aspx

来自「asp做的新闻系统」· ASPX 代码 · 共 123 行

ASPX
123
字号
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SQL" %>
<html>
   <script language="VB" runat="server">
    '建立数据连接和命令对象 
Dim UConn As SQLConnection

    '在页面装入时用此方法
    Sub Page_Load(Sender As Object, E As EventArgs) 
        '建立与数据库的连接
        UConn = New SQLConnection("server=localhost;uid=NetBBS;pwd=;database=NETBBS")
        If Not (IsPostBack)
            BindGrid()
        End If
    End Sub

    '在点击Edit连接时用到此方法:    
    Sub UDG_Edit(Sender As Object, E As DataGridCommandEventArgs)
        UDG.EditItemIndex = CInt(E.Item.ItemIndex)
        BindGrid()
    End Sub

    '在点击Cancle连接时用到此方法:    
    Sub UDG_Cancel(Sender As Object, E As DataGridCommandEventArgs)
        UDG.EditItemIndex = -1
        BindGrid()
    End Sub

    '在点击UpDate连接时用到此方法:    
    Sub UDG_Update(Sender As Object, E As DataGridCommandEventArgs)
           '创建数据集
        Dim DS As DataSet
        '创建命令对象
        Dim UComm As SQLCommand
        '定义修改数据的sql语句
        Dim UpdateCmd As String = "UPDATE FORUM SET 
[ID]=@fid,[Name]=@fname,Notes=@Notes,FatherID=@FatherID,status=@status where [ID]=@fid"
           '设置命令对象类型            
        UComm = New SQLCommand(UpdateCmd, UConn)

           '获得更改的数据
        UComm.Parameters.Add(New SQLParameter("@fid", SQLDataType.VarChar, 4))
        UComm.Parameters.Add(New SQLParameter("@fname", SQLDataType.VarChar, 50))
        UComm.Parameters.Add(New SQLParameter("@Notes", SQLDataType.VarChar, 500))
        UComm.Parameters.Add(New SQLParameter("@FatherID", SQLDataType.VarChar, 4))
        UComm.Parameters.Add(New SQLParameter("@status", SQLDataType.VarChar, 1))

        Dim Cols As String() = {"@fid","@fname","@Notes","@FatherID","@status"}

           '激活数据连接
        UComm.ActiveConnection.Open()
        Try 
             '执行命令集
            UComm.ExecuteNonQuery()
            Message.InnerHtml = "修改成功!!"
            '改为Edit连接
            UDG.EditItemIndex = -1
           '处理异常
        Catch Exp As SQLException
            If Exp.Number = 2627
                Message.InnerHtml = "相同的记录在数据库中"
            Else
                Message.InnerHtml = "不能更改记录!!"
            End If
            Message.Style("color") = "red"
        End Try
           '关闭数据连接
        UComm.ActiveConnection.Close()
    
           '调用BindGrid()方法
        BindGrid()
    End Sub

    '定义BindGrid()方法
    Sub BindGrid() 
        Dim DS As DataSet
        Dim UComm As SQLDataSetCommand
           '从表forum选出数据
        UComm = new SQLDataSetCommand("select * from forum", UConn)
           '填充数据集
        DS = new DataSet()
        UComm.FillDataSet(DS, "forum")
           '数据的绑定
        UDG.DataSource=DS.Tables("forum").DefaultView
        UDG.DataBind()
End Sub
</script>
<title>
   Update!
</title>
<body style="font: 10pt verdana">
   <BR>
   <CENTER>

  <form runat="server">
    <h3><font face="Verdana">.NET->修改记录</font></h3>
    <span id="Message" MaintainState="false" style="font: arial 11pt;" runat="server"/><p>
    <!--响应对数据库操作的模板-->
    <ASP:DataGrid id="UDG" runat="server"
      Width="800"
      BackColor="#ffffff" 
      BorderColor="black"
      ShowFooter="false" 
      CellPadding=3 
      CellSpacing="0"
      Font-Name="Verdana"
      Font-Size="8pt"
      HeaderStyle-BackColor="#ffffff"
      OnEditCommand="UDG_Edit"
      OnCancelCommand="UDG_Cancel"
      OnUpdateCommand="UDG_Update"
    >
      <property name="Columns">
        <asp:EditCommandColumn EditText="编辑" CancelText="取消" UpdateText="修改" ItemStyle-Wrap="false"/>
      </property>
    </ASP:DataGrid>

  </form>

  </CENTER>
</body>
</html>

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?