我正在开发一个asp.net VB网页。我创建了一个网格视图,它将在按钮单击事件中添加数据,但在第一个按钮单击事件中,它会在网格视图中添加两行,并且在第一个按钮单击情形正常后。如何在第一次活动中避免这种重复?
这里我附上了代码和一个屏幕打印。我需要避免使用空值的行。
Imports System.Data
Imports System.Data.SqlClient
Imports System.Collections
Partial Class add_stock
Inherits System.Web.UI.Page
Private numOfColumns As Integer = 1
Private ctr As Integer = 0
Private tablepanel As Table = Nothing
Dim row As New TableRow()
Dim cell As New TableCell()
Dim tb9 As New Button
Dim panel2 As New Panel
Dim DT As New DataTable
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Not IsPostBack Then
tablepanel = New Table()
tablepanel.ID = "tableBuild"
Session("table") = tablepanel
ViewState("ctr") = ctr
'BindGridview()
End If
ctr = CType(ViewState("ctr"), Int32)
tablepanel = DirectCast(Session("table"), Table)
Panel1.Controls.Add(tablepanel)
DT.Columns.Add("ID")
DT.Columns.Add("Name")
DT.Columns(0).AutoIncrement = True
End Sub
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim drCurrentRow As DataRow = Nothing
If GridView1.Rows.Count > 1 Then
AddNewRow()
ElseIf GridView1.Rows.Count = 1 Then
AddNewRow()
Else
BindGridview()
AddNewRow()
End If
'Place the table with textboxes on the page
'-------- numOfColumns = 1
'-------- GenerateTable(numOfColumns)
'End of the table display with textboxes
' Add row to grid view
End Sub
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As EventArgs)
ClientScript.RegisterStartupScript(Me.GetType, "Button_Alert", "alert('Button clicked!');", True)
numOfColumns = 1
'Generate the Table based from the inputs
GenerateTable(numOfColumns)
End Sub
Protected Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
GridView1.ShowFooter = True
End Sub
Protected Sub BindGridview()
Dim dt As New DataTable()
'dt.Columns.Add("rowid", GetType(Integer))
dt.Columns.Add("Item Number", GetType(String))
dt.Columns.Add("Item Category", GetType(String))
dt.Columns.Add("Lead Time", GetType(String))
dt.Columns.Add("Supplier", GetType(String))
dt.Columns.Add("Item Quantity", GetType(String))
dt.Columns.Add("Re-order Level", GetType(String))
dt.Columns.Add("Unit Price", GetType(String))
dt.Columns.Add("Retails Price", GetType(String))
dt.Columns.Add("Item Details", GetType(String))
Dim dr As DataRow = dt.NewRow()
dr("Item Number") = String.Empty
dr("Item Category") = String.Empty
dr("Lead Time") = String.Empty
dr("Supplier") = String.Empty
dr("Item Quantity") = String.Empty
dr("Re-order Level") = String.Empty
dr("Unit Price") = String.Empty
dr("Retails Price") = String.Empty
dr("Item Details") = String.Empty
dt.Rows.Add(dr)
ViewState("Curtbl") = dt
GridView1.DataSource = dt
'GridView1.DataBind()
End Sub
Private Sub AddNewRow()
Dim rowIndex As Integer
If ViewState("Curtbl") IsNot Nothing Then
Dim dt As DataTable = DirectCast(ViewState("Curtbl"), DataTable)
Dim drCurrentRow As DataRow = Nothing
If dt.Rows.Count > 1 Then
Dim rowcount As Integer = dt.Rows.Count
drCurrentRow = dt.NewRow()
' drCurrentRow("rowid") = rowcount + 1
dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
dt.Rows.Add()
rowIndex += 1
ViewState("Curtbl") = dt
GridView1.DataSource = dt
GridView1.DataBind()
MsgBox("1st")
ElseIf dt.Rows.Count = 1 Then
Dim rowcount As Integer = dt.Rows.Count
'drCurrentRow = dt.NewRow()
' drCurrentRow("rowid") = rowcount + 1
MsgBox(rowcount + 1)
dt.Rows(rowcount - 1)("Item Number") = TextBox21.Text
dt.Rows(rowcount - 1)("Item Category") = DropDownList4.Text
dt.Rows(rowcount - 1)("Lead Time") = TextBox19.Text
dt.Rows(rowcount - 1)("Supplier") = DropDownList3.Text
dt.Rows(rowcount - 1)("Item Quantity") = TextBox22.Text
dt.Rows(rowcount - 1)("Re-order Level") = TextBox17.Text
dt.Rows(rowcount - 1)("Unit Price") = TextBox23.Text
dt.Rows(rowcount - 1)("Retails Price") = TextBox20.Text
dt.Rows(rowcount - 1)("Item Details") = TextBox18.Text
dt.Rows.Add()
rowIndex += 1
ViewState("Curtbl") = dt
GridView1.DataSource = dt
GridView1.DataBind()
MsgBox("2nd")
Else
Dim rowcount As Integer = dt.Rows.Count
drCurrentRow = dt.NewRow()
' drCurrentRow("rowid") = rowcount + 1
dt.Rows(1)("Item Number") = TextBox21.Text
dt.Rows(1)("Item Category") = DropDownList4.Text
dt.Rows(1)("Lead Time") = TextBox19.Text
dt.Rows(1)("Supplier") = DropDownList3.Text
dt.Rows(1)("Item Quantity") = TextBox22.Text
dt.Rows(1)("Re-order Level") = TextBox17.Text
dt.Rows(1)("Unit Price") = TextBox23.Text
dt.Rows(1)("Retails Price") = TextBox20.Text
dt.Rows(1)("Item Details") = TextBox18.Text
dt.Rows.Add()
rowIndex += 1
ViewState("Curtbl") = dt
GridView1.DataSource = dt
GridView1.DataBind()
MsgBox("3rd")
End If
Else
Response.Write("ViewState Value is Null")
End If
End Sub
Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim dt As DataTable = TryCast(ViewState("Curtbl"), DataTable)
Dim RowToDelete As Integer = e.RowIndex
If dt.Rows.Count > 0 Then
dt.Rows.RemoveAt(RowToDelete)
dt.AcceptChanges()
GridView1.DataSource = dt
End If
GridView1.DataBind()
End Sub
Protected Sub OnRowDataBound(sender As Object, e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim item As String = e.Row.Cells(0).Text
For Each button As Button In e.Row.Cells(2).Controls.OfType(Of Button)()
If button.CommandName = "Delete" Then
button.Attributes("onclick") = "if(!confirm('Do you want to delete " + item + "?')){ return false; };"
End If
Next
End If
End Sub
End Class
答案 0 :(得分:0)
在上面的代码中,您只需要注释AddNewRow()方法,就像在BindGridView()中一样,您正在为表创建新行并将其绑定到Grid,因此无需在此处调用AddNewRow()。请查看以下代码。
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim drCurrentRow As DataRow = Nothing
If GridView1.Rows.Count > 1 Then
AddNewRow()
ElseIf GridView1.Rows.Count = 1 Then
AddNewRow()
Else
BindGridview()
'AddNewRow()
End If
'Place the table with textboxes on the page
'-------- numOfColumns = 1
'-------- GenerateTable(numOfColumns)
'End of the table display with textboxes
' Add row to grid view
End Sub