62,046
社区成员
发帖
与我相关
我的任务
分享
'每绑定一行,将会触发该事件过程,在其中将给删除按钮添加JavaScript事件
Sub htqp_ItemDataBound(ByVal sender As Object, ByVal e As DataGridItemEventArgs)
'这个判断语句表示,只有对于数据行才执行,对于标题栏和脚注栏则不执行
If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then
'下面找到删除按钮控件,它其实是一个LinkButton控件
Dim lbtnDel As LinkButton '定义一个LinkButton控件
lbtnDel = e.Item.Cells(4).Controls(0) '它位于第0列第0个控件
'下面添加JavaScript事件
lbtnDel.Attributes.Add("onclick", "return confirm('您真的要删除 " & e.Item.DataItem("bzzrm_mc") & " 吗?');")
End If
End Sub
Sub dg_htqp_delete(ByVal sender As Object, ByVal e As DataGridCommandEventArgs)
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
'取得登陆用户的用户类型
Dim stryhlx As String
stryhlx = Session("yhlx")
'If Session("yhm") = "admin" Then
strsql = "delete from tab_bzzrm where id=" & dg_htqp.DataKeys(CInt(e.Item.ItemIndex))
Dim cmd As New OleDbCommand(strsql, conn)
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
dg_htqp.EditItemIndex = -1
Call mydatabind_htqp()
'Else
'Response.Write("<script language='javascript'>alert('对不起,您无删除权限。');</" & "script>")
'End If
End Sub
<asp:DataGrid ID="dg_htqp" OnItemDataBound="htqp_ItemDataBound" Font-Size="14px" Width="100%" AutoGenerateColumns="false" AllowPaging="true" AllowCustomPaging="true" DataKeyField="id" PageSize="50" OnPageIndexChanged="dg_htqp_page" OnDeleteCommand="dg_htqp_delete" PagerStyle-HorizontalAlign="Right" PagerStyle-Mode="NumericPages" runat="server">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<img id="Img1" alt="~/img/jt_green.jpg" src="~/img/jt_green.jpg" runat="server" visible='<%# IIF (container.dataitem("beian")="", True, False) %>' /><img id="Img2" src="~/img/jt_red.jpg" alt="~/img/jt_green.jpg" runat="server" visible='<%# IIF (container.dataitem("beian")<>"", True, False) %>' />
</ItemTemplate>
</asp:TemplateColumn>
<asp:TemplateColumn HeaderText="班组长任免">
<HeaderStyle Width="400px" />
<ItemTemplate>
<asp:HyperLink ID="HyperLink1" Text='<%# container.dataitem("bzzrm_mc") %>' NavigateUrl='<%# "bzzrm_lct.aspx?id=" & container.dataitem("id") %>' Target="_blank" runat="server" >
</asp:HyperLink>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn HeaderText="时间" DataField="bzzrm_date" DataFormatString="{0:d}">
<HeaderStyle Width="120px" />
</asp:BoundColumn>
<asp:BoundColumn HeaderText="单位" DataField="bzzrm_dw">
<HeaderStyle Width="120px" />
</asp:BoundColumn>
<asp:ButtonColumn Text="删除" CommandName="delete" ItemStyle-Wrap="false" />
</Columns>
</asp:DataGrid>
End Sub
'单击导航栏的页码时执行该过程
Sub dg_htqp_page(ByVal sender As Object, ByVal e As DataGridPageChangedEventArgs)
dg_htqp.CurrentPageIndex = e.NewPageIndex
Call mydatabind_htqp()
End Sub
'分页功能数据绑定
Sub mydatabind_htqp()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim adp As New OleDbDataAdapter(strsql, conn)
Dim ds As New DataSet()
Dim intstart As Long = dg_htqp.CurrentPageIndex * dg_htqp.PageSize
adp.Fill(ds, intstart, dg_htqp.PageSize, "tab_bzzrm")
dg_htqp.VirtualItemCount = counttotal()
dg_htqp.DataSource = ds.Tables("tab_bzzrm").DefaultView
dg_htqp.DataBind()
End Sub
'If Session("yhm") = "admin" Then
strsql = "delete from tab_bzzrm where id=" & dg_htqp.DataKeys(CInt(e.Item.ItemIndex))
'分页功能数据绑定
Sub mydatabind_htqp()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim adp As New OleDbDataAdapter(strsql, conn)
Dim ds As New DataSet()
Dim intstart As Long = dg_htqp.CurrentPageIndex * dg_htqp.PageSize
adp.Fill(ds, intstart, dg_htqp.PageSize, "tab_bzzrm")
dg_htqp.VirtualItemCount = counttotal()
dg_htqp.DataSource = ds.Tables("tab_bzzrm").DefaultView
dg_htqp.DataBind()
End Sub
'计算总页数
Function counttotal()
Dim conn As New OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=" & Server.MapPath("../app_data/mydb.mdb"))
Dim strsql As String
Dim stryhm_dw, stryhm As String
'登陆用户名
stryhm = Session("yhm")
'登陆用户所属单位
stryhm_dw = Session("yhdw")
'取得登陆用户的级别
Dim stryhjb As String
stryhjb = Session("yhjb")
If stryhjb = "cjgly" Then
strsql = "select * from tab_bzzrm order by id desc"
Else
strsql = "select * from tab_bzzrm where bzzrm_yhm= '" & stryhm & "' order by id desc"
End If
Dim cmd As New OleDbCommand(strsql, conn)
conn.Open()
counttotal = cmd.ExecuteScalar()
conn.Close()
End Function