可以啊给你一个例子 Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load '-----------Judge Session is time out------------- If Session("UserID") Is Nothing Then Response.Write("<script language=javascript>alert('²Ù×÷³¬Ê±,ÇëÖØеǽ!');</script>") Response.Write("<script language=javascript>window.open('Login.aspx','_parent');</script>") Response.End() End If '----------------------End------------------- If Not IsPostBack Then bind() End If End Sub Sub bind() Dim d As DAL Dim str As String = "Select * from dept" DG_Dept.DataSource = d.ExecuteReader(str) DG_Dept.DataBind() End Sub Private Sub add1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add1.Click Dim conn As SqlConnection Dim d As New DAL Dim constr As String = d.ConStr Dim str As String Dim dt As DataTable Dim drow As DataRow Dim da As SqlDataAdapter Dim ds As New DataSet Dim obj As SqlCommandBuilder Dim path As String Try conn = New SqlConnection(constr) str = "select * from dept" da = New SqlDataAdapter(str, conn) da.Fill(ds, "dept") dt = ds.Tables("dept") drow = dt.NewRow() drow("deptname") = Name.Text dt.Rows.Add(drow) obj = New SqlCommandBuilder(da) da.Update(ds, "dept") path = Server.MapPath("file") + "\" + Name.Text CreateFolder(path) bind() Catch ex As Exception Response.Write(ex.ToString) End Try End Sub Sub CreateFolder(ByVal path As String) Dim di As New DirectoryInfo(path) If di.Exists = False Then di.Create() End If End Sub Private Sub DG_Dept_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Dept.ItemCommand If e.CommandName = "Del" Then Dim str As String = "delete from dept where deptID=" + CStr(DG_Dept.DataKeys(e.Item.ItemIndex)) Dim d As DAL d.ExecuteNonQuery(str) bind() End If End Sub Private Sub DG_Dept_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_Dept.ItemDataBound If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim l As LinkButton = CType(e.Item.Cells(1).FindControl("delete"), LinkButton) l.Attributes.Add("onclick", "javascript:return confirm('ÄãÈ·¶¨É¾³ýÕâÌõ¼Ç¼Âð?');") End If End Sub
'-----------Judge Session is time out-------------
If Session("UserID") Is Nothing Then
Response.Write("<script language=javascript>alert('²Ù×÷³¬Ê±,ÇëÖØеǽ!');</script>")
Response.Write("<script language=javascript>window.open('Login.aspx','_parent');</script>")
Response.End()
End If
'----------------------End-------------------
If Not IsPostBack Then
bind()
End If
End Sub
Sub bind()
Dim d As DAL
Dim str As String = "Select * from dept"
DG_Dept.DataSource = d.ExecuteReader(str)
DG_Dept.DataBind()
End Sub
Private Sub add1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles add1.Click
Dim conn As SqlConnection
Dim d As New DAL
Dim constr As String = d.ConStr
Dim str As String
Dim dt As DataTable
Dim drow As DataRow
Dim da As SqlDataAdapter
Dim ds As New DataSet
Dim obj As SqlCommandBuilder
Dim path As String
Try
conn = New SqlConnection(constr)
str = "select * from dept"
da = New SqlDataAdapter(str, conn)
da.Fill(ds, "dept")
dt = ds.Tables("dept")
drow = dt.NewRow()
drow("deptname") = Name.Text
dt.Rows.Add(drow)
obj = New SqlCommandBuilder(da)
da.Update(ds, "dept")
path = Server.MapPath("file") + "\" + Name.Text
CreateFolder(path)
bind()
Catch ex As Exception
Response.Write(ex.ToString)
End Try
End Sub
Sub CreateFolder(ByVal path As String)
Dim di As New DirectoryInfo(path)
If di.Exists = False Then
di.Create()
End If
End Sub Private Sub DG_Dept_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles DG_Dept.ItemCommand If e.CommandName = "Del" Then
Dim str As String = "delete from dept where deptID=" + CStr(DG_Dept.DataKeys(e.Item.ItemIndex))
Dim d As DAL
d.ExecuteNonQuery(str)
bind()
End If End Sub Private Sub DG_Dept_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DG_Dept.ItemDataBound
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
Dim l As LinkButton = CType(e.Item.Cells(1).FindControl("delete"), LinkButton)
l.Attributes.Add("onclick", "javascript:return confirm('ÄãÈ·¶¨É¾³ýÕâÌõ¼Ç¼Âð?');")
End If
End Sub
<form id="Form1" method="post" runat="server">
<div align="center" style="Z-INDEX: 101; LEFT: 8px; WIDTH: 100%; POSITION: absolute; TOP: 8px">
<TABLE id="Table1" width="80%">
<TR>
<TD class="header" align="right">²¿ÃŹÜÀí</TD>
</TR>
<TR>
<TD>
<asp:DataGrid id="DG_Dept" runat="server" Width="100%" Font-Size="9pt" AutoGenerateColumns="False"
DataKeyField="DeptID" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" BackColor="White"
CellPadding="4">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399" BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC" BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="deptname" HeaderText="²¿ÃÅÃû³Æ" HeaderStyle-Width="80%" ItemStyle-Width="80%"></asp:BoundColumn>
<asp:TemplateColumn HeaderText="[ɾ³ý]" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
<ItemTemplate>
<asp:LinkButton runat="server" Text="[ɾ³ý]" ID="delete" CommandName="Del" CausesValidation="false"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099" BackColor="#FFFFCC"></PagerStyle>
</asp:DataGrid></TD>
<TR>
<TD height="10"></TD>
</TR>
<TR>
<TD>
ÇëÊäÈ벿ÃÅÃû³Æ:
<asp:TextBox id="Name" runat="server" Width="232px"></asp:TextBox> [
<asp:LinkButton Runat="server" ID="add1">Ìí¼Óв¿ÃÅ</asp:LinkButton>]
<asp:RequiredFieldValidator id="RequiredFieldValidator1" runat="server" ErrorMessage="²¿ÃÅÃû³Æ²»ÄÜΪ¿Õ£¡" Display="None"
ControlToValidate="Name"></asp:RequiredFieldValidator>
<asp:ValidationSummary id="ValidationSummary1" runat="server" ShowMessageBox="True" ShowSummary="False"></asp:ValidationSummary></TD>
</TR>
</TABLE>
</div>
</form>
</body>