本文介绍了用imageButton和javascript删除asp.net gridview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时删除!!

大家好.有一个忙要问.您介意分享/告诉我我的代码中的错误吗?这是我要完成的工作.
首先,我有一个数据库

Hello guys. Got a favor to ask. Would you mind sharing/telling me the error on my code. Here is what i am trying to accomplish.
first i have a database

ID                Name                     UserID              Role
1                 wew                      wew                 Admin
2                 drk                      drk                 Admin
an so on..           


现在我将其与gridview绑定(没有带有模板字段和绑定字段的数据源.我没有在gridview的列上显示ID,但我在查询中进行了设置.因此,它是; userID [boundfield],Name [超链接] ],Role [boundfield]和Delete [Imagebutton].现在,先生/女士ive在我的删除功能上遇到了问题.它没有删除,所以先生,这是我的代码
客户端:


now i bind it with a gridview(without datasource with templated field and bound fields. I didnt show the ID on the gridview''s column but i set it on my query. So here it is; userID[boundfield], Name[hyperlink], Role[boundfield] and Delete[Imagebutton]. Now Sir/madams ive got a problem on my delete function. it doesnt delete. so sir here is my code
Client Side:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="Users.aspx.vb" Inherits="Admin_AddUser" title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <br />
    <br />
    <div align="left" style="left: 26px; width: 488px; position: static; top: 281px;
        height: 32px">
        <asp:Label ID="Label1" runat="server" Height="32px" Text="USERS" Width="134px"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Add User" Width="131px" /></div>
    <br />
    <asp:GridView ID="GridView1" runat="server" Width="1035px" AutoGenerateColumns="False"  AllowPaging="True" OnPageIndexChanging = "GridView1_OnPageIndexChanging">
      <Columns>

      <asp:BoundField DataField="UserID" HeaderText="User Name" >
          <ControlStyle BorderWidth="200px" />
      </asp:BoundField>

      <asp:TemplateField HeaderText="Name" SortExpression="ImageTitle">
            <ItemTemplate>
            <asp:HyperLink runat="server" ID="HyperLink1" NavigateUrl='<%#"User.aspx?ID=" & Eval("ID")%>' Text='<%# Eval("Name","{0:d}")%>'> </asp:HyperLink>
            <ItemStyle Height="75px" Width="800px" />
            </ItemTemplate>
      </asp:TemplateField>

      <asp:BoundField DataField="Role" HeaderText="Role"  >
          <ControlStyle BorderWidth="200px" />
      </asp:BoundField>

      <asp:TemplateField HeaderText="Delete">
      <ItemTemplate>
          <asp:ImageButton ID="ImageDelete" ImageUrl="" runat="server" CommandName="cmdDelete" CommandArgument='<%# Eval("ID") %>'

        />
      </ItemTemplate>
      </asp:TemplateField>
      </Columns>
    </asp:GridView>
</asp:Content>



服务器端代码:



ServerSide code:

Imports System.Data.SqlClient
Partial Class Admin_AddUser
    Inherits System.Web.UI.Page
    Dim con As New SqlConnection("Data Source=ML0003135586\SQLEXPRESS;Initial Catalog=TestSQL;Integrated Security=True")
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            GridView1.PageIndex = 0
            bind()
        End If
    End Sub
    Protected Sub GridView1_OnPageIndexChanging(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridView1.PageIndexChanging
        GridView1.PageIndex = e.NewPageIndex
        bind()
    End Sub
    Private Function getTable() As DataTable
        Dim adap As New SqlDataAdapter("Select [ID],[UserID],[Name],[Role] from [tblUser] Order by [Name]Asc", con)
        Dim dt As New DataTable()
        adap.Fill(dt)
        Return dt
    End Function
    Private Sub bind()
        Dim dr As DataTable = getTable()
        GridView1.DataSource = dr
        GridView1.DataBind()
    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Response.Redirect("AddUser.aspx")
    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand
        If e.CommandName = "cmdDelete" Then
            Dim ID As Integer = Convert.ToInt32(e.CommandArgument)
        End If
    End Sub
    Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim l As ImageButton = DirectCast(e.Row.FindControl("ImageDelete"), ImageButton)
            l.Attributes.Add("onclick", "javascript:return " & "confirm('Are you sure you want to delete this record " & DataBinder.Eval(e.Row.DataItem, "ID") & "')")
        End If
    End Sub

    Protected Sub GridView1_RowDeleting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewDeleteEventArgs) Handles GridView1.RowDeleting
        Dim ID As Integer = CInt(GridView1.DataKeys(e.RowIndex).Value)
        con.Open()
        Dim cmd As New SqlCommand("delete  [tblUser] where [ID]=" + ID & "", con)
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub


先生,有任何可能的方式,或者我的删除功能代码有误.请帮助并获得更大的力量!


Sir is there any possible way or my code on delete function is wrong. Please help and more power!

推荐答案


这篇关于用imageButton和javascript删除asp.net gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

1403页,肝出来的..

09-06 14:57