Sorry for the previous question I've posted. So here's my problem again, here's my code for the gridview.
`
If e.CommandName = "DeleteItem" Then
Dim gvItems As GridView = CType(sender, GridView)
Dim gvparent As GridViewRow = CType(gvItems.NamingContainer, GridViewRow)
'(gvparent.Cells(2).Text)
If gvItems.Rows.Count = 1 Then
lblPopup.Text = "There must be atleast one row."
mpeError.Show()
Exit Sub
End If
Program.oEquipmentLoan.sTransactionCode = gvparent.Cells(3).Text
'Dim gvItem As GridView = TryCast(gvitems.FindControl("gvItems"), GridView)
'For Each gvRow As GridViewRow In gvitems.Rows
' Dim chkItem As CheckBox = DirectCast(gvRow.FindControl("chkSelectItems"), CheckBox)
' If (chkItem.Checked) Then
' Program.oEquipmentLoan.iItemNumber = gvRow.Cells(2).Text
' oSQLEquipmentLoan.DeleteEquipmentLoanItem(Program.oEquipmentLoan)
' End If
'Next
'loadGVGetTransactionLog()
Dim icount As Integer = 0
For Each gvRow As GridViewRow In gvItems.Rows
Dim chkItem As CheckBox = DirectCast(gvRow.FindControl("chkSelectItems"), CheckBox)
If (chkItem.Checked) Then
icount += 1
End If
Next
If icount > 0 Then
For Each gvRow As GridViewRow In gvItems.Rows
Dim chkItem As CheckBox = DirectCast(gvRow.FindControl("chkSelectItems"), CheckBox)
If (chkItem.Checked) Then
Program.oEquipmentLoan.iItemNumber = gvRow.Cells(2).Text
oSQLEquipmentLoan.DeleteEquipmentLoanItem(Program.oEquipmentLoan)
End If
Next
Else
Dim gvr As GridViewRow = DirectCast(DirectCast(e.CommandSource, Control).NamingContainer, GridViewRow)
Dim Index As Integer = gvr.RowIndex
Dim gvRowSelected As GridViewRow = gvItems.Rows(Index)
Program.oEquipmentLoan.iItemNumber = gvRowSelected.Cells(2).Text
oSQLEquipmentLoan.DeleteEquipmentLoanItem(Program.oEquipmentLoan)
End If
End If
sIndex = gvTransactionLogViewer.PageIndex
blnCount = True
loadGVGetTransactionLog()
End Sub`
So, this code particularly deletes a selected row on a gridview. My problem is that whenever I delete an item, after it is deleted, I can't maintain to view the list.
Image given Below :
The photo shows on the before part is that when I'm selecting an item to delete. and on the after, when the item is deleted. And as you can see, what I need to maintain is the list of the updated items after I delete something and after it refreshes. Thanks! :)
on your .aspx page you need to do the partial postback to prevent the whole page postback.
you can achieve this using either asp.net ajax update panel "partial page rendering" or jquery.
Related
Please bear with me a bit as I try to explain my problem.
I have multiple GridView controls, each with its own checkbox and textboxes.
User must either check the checkboxes or enter data into all textboxes.
If checkbox is not checked and textboxes within a particular gridview control is empty, when a user clicks the NEXT button of a multiview control, an error is raised.
The following code shows two FOR loops inside button click event of the NEXT navigation button.
The first Gridview1 FOR loop works great. If checkbox is not checked and the textboxes are empty, an alert message is displayed and user cannot navigate to next page.
If however, either the checkbox is checked or textboxes are filled with data, user can then successfully navigate to the next page.
The issue is with the second FOR loop for grvspouse gridview control.
If checkbox is not checked and textboxes are empty, the alert box is displayed with message that user must checkbox or enter data into textboxes. This is fine. However, the problem is that user is still taken to the next page.
Is there a way to handle multiple FOR loops inside BTN_NEXT navigation event handler?
Protected Sub btnNext_Click(ByVal sender As Object, ByVal e As EventArgs)
'If the message failed at some point, let the user know
For Each row As GridViewRow In Gridview1.Rows
Dim namesource As TextBox = TryCast(row.FindControl("txtsourcename"), TextBox)
Dim nmesource As String = namesource.Text
Dim addresssource As TextBox = TryCast(row.FindControl("txtsourceaddress"), TextBox)
Dim addrsource As String = addresssource.Text
Dim incomesource As TextBox = TryCast(row.FindControl("txtsourceincome"), TextBox)
Dim incmsource As String = incomesource.Text
Dim ckb As CheckBox = TryCast(row.FindControl("grid1Details"), CheckBox)
Dim checkb As Boolean = ckb.Checked
If checkb = False AndAlso nmesource = "" AndAlso addrsource = "" AndAlso incmsource = "" Then
ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
myMultiView.ActiveViewIndex += 1
lblResult.Visible = True
End If
Next
For Each row As GridViewRow In grvspouse.Rows
Dim namespouse As TextBox = TryCast(row.FindControl("txtspousename"), TextBox)
Dim nmespouse As String = namespouse.Text
Dim addressspouse As TextBox = TryCast(row.FindControl("txtspouseaddress"), TextBox)
Dim addrspouse As String = addressspouse.Text
Dim incomespouse As TextBox = TryCast(row.FindControl("txtspouseincome"), TextBox)
Dim incmspouse As String = incomespouse.Text
Dim ckb2 As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
Dim checkc As Boolean = ckb2.Checked
If checkc = False AndAlso nmespouse = "" AndAlso addrspouse = "" AndAlso incmspouse = "" Then
ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
myMultiView.ActiveViewIndex += 1
lblResult.Visible = True
End If
Next
End Sub
[Edited]
You are facing this problem because you coded it that way....The easiest fix would be to create a STRING VARIABLE object and by each click,the program would check the VARIABLEfor a desired value and if the value matches then it'ld move to the next form.A complete example would look like this :
'Create a VARIABLE named HITCOUNT and keep the value empty at first.
Public class MyProject
Dim HITCOUNT as string = ""
If HITCOUNT = "" Then
For Each row As GridViewRow In Gridview1.Rows
Dim namesource As TextBox = TryCast(row.FindControl("txtsourcename"), TextBox)
Dim nmesource As String = namesource.Text
Dim addresssource As TextBox = TryCast(row.FindControl("txtsourceaddress"), TextBox)
Dim addrsource As String = addresssource.Text
Dim incomesource As TextBox = TryCast(row.FindControl("txtsourceincome"), TextBox)
Dim incmsource As String = incomesource.Text
Dim ckb As CheckBox = TryCast(row.FindControl("grid1Details"), CheckBox)
Dim checkb As Boolean = ckb.Checked
If checkb = False AndAlso nmesource = "" AndAlso addrsource = "" AndAlso incmsource = "" Then
ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
myMultiView.ActiveViewIndex += 1
lblResult.Visible = True
End If
Next
HITCOUNT = "1" 'adding an integer value of 1(you can add anything)
End if
If HITCOUNT = "1" then
For Each row As GridViewRow In grvspouse.Rows
Dim namespouse As TextBox = TryCast(row.FindControl("txtspousename"), TextBox)
Dim nmespouse As String = namespouse.Text
Dim addressspouse As TextBox = TryCast(row.FindControl("txtspouseaddress"), TextBox)
Dim addrspouse As String = addressspouse.Text
Dim incomespouse As TextBox = TryCast(row.FindControl("txtspouseincome"), TextBox)
Dim incmspouse As String = incomespouse.Text
Dim ckb2 As CheckBox = TryCast(row.FindControl("spouseDetails"), CheckBox)
Dim checkc As Boolean = ckb2.Checked
If checkc = False AndAlso nmespouse = "" AndAlso addrspouse = "" AndAlso incmspouse = "" Then
ClientScript.RegisterStartupScript([GetType](), "Confirm", "jAlert('Please enter values on all textboxes or check the checkbox next to each textbox!');", True)
Else
myMultiView.ActiveViewIndex += 1
lblResult.Visible = True
End If
Next
HITCOUNT="2"
End if
'and move on and on
This is just one way and maybe the quickest one.There are many other ways to do so.If you don't wanna use this one, just comment and i'll post another solution
I need to create a table whose first column is an image button that deletes the whole row when clicked (actually it deletes a database row then reloads the HTML table).
The table and the buttons display as expected, but when I click a button, nothing happens.
To make each button clickable, I used AddHandler.
deletion is the sub that handles the clicks, it will get the button's ID (which contains the database table id for the row the user wants to delete) then calls a stored procedure with the ID as parameter and finally reloads the HTML table. I didn't implement it yet but the debugger can't reach it.
The VB code is as follows
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Request.Cookies("myuser") Is Nothing OrElse Request.Cookies("myuser")("isconnected") <> "1" Then
Response.Redirect("/Default.aspx")
Else
N_User = CInt(Request.Cookies("myuser")("N_User"))
Dim dbc As DBConnection
dbc = New DBConnection("192.168.1.45", "CorpDB", "someuser", "xxxxxxx")
Dim dt As DataTable
Try
dt = dbc.getQueryData("select lastname, isnull(firstname,'') from itc where N_User=" & CStr(N_User))
If dt.Rows.Count > 0 Then
End If
Catch ex As Exception
' do nothing for now
End Try
End If
FillTable()
End Sub
Protected Sub deletion(sender As Object, e As ImageClickEventArgs)
' confirm deletion
' Run a stored procedure to delete the comment row
FillTable()
End Sub
Private Sub FillTable()
Dim dbc As DBConnection
Dim dt As DataTable
Dim tr As TableRow
Dim tc As TableCell
Dim ibutton As ImageButton
Dim sqlquery As String
If PickProject.SelectedValue = "" Then
Exit Sub
End If
sqlquery = "select N_follow, Name=isnull(isnull(lastname + ' ','') + Nom, 'Anon'), Date=convert(varchar,DFollow,103), Stage=isnull(FreeField1,''), [Text]=isnull(dbo.RTFtoText(txt),'') from AF_FOLLOW a left outer join USERS u on u.N_User = a.N_User_Create where Numero=" & PickProject.SelectedValue & "order by DFollow desc"
dbc = New DBConnection("192.168.1.45", "CorpDB", "someuser", "xxxxxxx")
Try
dt = dbc.getQueryData(sqlquery)
Catch ex As Exception
' Show something,
Exit Sub
End Try
If dt.Rows.Count > 0 Then
CommentTable.Rows.Clear()
For Each row As DataRow In dt.Rows
' Create a new row
tr = New TableRow
' column #1 : delete button
tc = New TableCell
tc.CssClass = "SelectDel"
ibutton = New ImageButton()
ibutton.ID = "ibutton_" & row.Item("N_follow")
ibutton.ImageUrl = "img/remove_icon.png"
AddHandler ibutton.Click, AddressOf deletion
tc.Controls.Add(ibutton)
tr.Cells.Add(tc)
' Column #2 : Date
tc = New TableCell
tc.Text = row.Item("Date")
tr.Cells.Add(tc)
' Column #3 : Stage
tc = New TableCell
tc.Text = row.Item("Stage")
tr.Cells.Add(tc)
' Column #4 : name
tc = New TableCell
tc.Text = row.Item("Name")
tr.Cells.Add(tc)
' Column #5 : Comment
tc = New TableCell
tc.Text = row.Item("Text")
tr.Cells.Add(tc)
' Add the created row to the table
CommentTable.Rows.Add(tr)
Next
CommentTable.Visible = True
Else
End If
End Sub
For some reason, it's Page_Load that gets triggered when I click a button. But I got the same behavior with a DropDownList, first Page_Load gets fired up, then PickProject_SelectedIndexChanged and the event handler works as intended.
I googled and searched StackOverflow a lot , but none of the solutions suggested seems to work.
What did I miss ?
hi what is my problem is in grid view after page loading i am entering some values in text box but after pressing the next page that page values are disappearing..
how can i save and retrieve the values into the grid view ..
i am able to save it in session but how can i past it into the currect page..
here is my code i am able to save it into the values but
after changing other page again entering into the same page how can i past the values into the corresponding page..
Protected Sub Gridview1_PageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
'Gridview1.PageIndex = e.NewPageIndex
'SetInitialRow()
Dim pageindex As Integer = Gridview1.PageIndex
Response.Write(Gridview1.PageIndex.ToString())
Dim d As Integer = Gridview1.PageCount
Dim texts As String() = New String(Gridview1.PageSize - 1) {}
Dim textBox As TextBox
Dim count As Integer = 0
For Each row As GridViewRow In Gridview1.Rows
textBox = DirectCast(row.FindControl("TextBox1"), TextBox)
If textBox IsNot Nothing Then
texts(count) = textBox.Text
Else
texts(count) = ""
End If
count += 1
Next
Session("one" + "pageindex") = texts
Gridview1.PageIndex= e.NewPageIndex
SetInitialRow()
'Dim sessionint As Integer = Session("page" + "pageindex")
'If Session("page" + "pageindex") IsNot Nothing Then
' Dim textBox1 As TextBox
' Dim texts1 As String() = DirectCast(Session("page" + "pageindex"), String())
' For i As Integer = 0 To Gridview1.Rows.Count - 1
' textBox1 = DirectCast(Gridview1.Rows(i).FindControl("textbox"), TextBox)
' textBox1.Text = texts(i)
' Next
'End If
End Sub
what is your datasource to bind to the gridview ?
These are the 2 ways that I have been doing...
Grab the whole data table and put it into ViewState("dataTable"). Do your filtering of rows and data from there.
Everytime you press 'Next Page', update the rows into the ViewState("dataTable").
Then rebind the gridview with the ViewState("dataTable").
Update the database directly when you press "Next Page".
I am using ItemTemplate & EditTemplate for editing the gridview. (ASP.net + VB).
I click EDIT button then I can check/uncheck those checkbox and amend the textbox value.
When click UPDATE button, it will fire the RowUpdating Event, But I found that when I get the value for update statement, it still get the value before editing, not updated value.
How can I get the latest & updated value ? Thanks.
Joe
The following is the VB code:
Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
'Update the values.
Dim row = Gridview1.Rows(e.RowIndex)
Dim Col1_SL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_SL"), CheckBox)
Dim Col1_VL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_VL"), CheckBox)
Dim Col1_ML = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_ML"), CheckBox)
Dim Col1_PH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_PH"), CheckBox)
Dim Col1_APH = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_APH"), CheckBox)
Dim Col1_TOIL = CType(Gridview1.Rows(e.RowIndex).FindControl("cb1_TOIL"), CheckBox)
Dim Col1_Others = CType(Gridview1.Rows(e.RowIndex).FindControl("tb1_Others"), TextBox)
Dim Col1_RosterKey = CType(Gridview1.Rows(e.RowIndex).FindControl("lb1_rosterkey"), Label)
Using conn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("hris_shiftdutyConnectionString").ConnectionString)
conn.Open()
cmd.Connection = conn
sql = "SET DATEFORMAT dmy;UPDATE troster SET SL='" & Convert.ToInt32(Col1_SL.Checked) & "' where roster_key='" & Col1_RosterKey.Text & "';"
cmd.CommandText = Sql
reader = cmd.ExecuteReader()
conn.Close()
reader.Close()
End Using
'Reset the edit index.
Gridview1.EditIndex = -1
'Bind data to the GridView control.
BindData()
End Sub
The most likely reason will be this.
You are calling BindData() on Page_Load without using !IsPostBack
Protected Sub Page_Load Handles Me.Load
If Not IsPostBack
' Bind Grid only at the first load
' Do not load Grid again at Postbacks
BindData()
End If
End Sub
You have 2 options:
There is a separate RowUpdated method that is fired after updates have been performed by the data source that is bound to the grid view. This method will contain the new values for edits and inserts.
The RowUpdating method should have a parameter of type GridViewEventArgs. This will have a property called NewValues that contains the new values. In your example this was the variable e.
I am creating a gridview programmatically and I want to use the solution to this question: Programmatically access GridView columns and manipulate. In other words, I want to hide the hyperlinkfield and instead display a templatefield if the value of the "submitted" column in the row is 1. I'm listing employment applications and letting the user click "Load Application" if they have not yet submitted an application, or displaying "submitted" in plain text if they have already submitted it. I know how to do this if I create my gridview in the aspx file, but I don't know how to do this when I'm creating my gridview at runtime. Here's my code:
Public Sub getSavedApps(ByVal userID As Integer)
Dim ds As New DataSet
ds = clsData.sqlGetAllApps(userID)
If ds.Tables(0).Rows.Count > 0 Then
popAppSelect.Show()
'let user select which app they want to work on
Dim grdAppSelect As New GridView
'need to find out if this row has a value of 1 or 0 for submitted
Dim submitted As Boolean
'position column
Dim fieldPosition As New BoundField
fieldPosition.DataField = "positionDesired"
fieldPosition.HeaderText = "Position Desired"
If fieldPosition.DataField.ToString = "" Then
fieldPosition.DataFormatString = "<em>No position specified</em>"
End If
Dim colPosition As DataControlField = fieldPosition
'date column
Dim fieldDate As New BoundField
fieldDate.DataField = "dateStarted"
fieldDate.HeaderText = "Date Started"
Dim colDate As DataControlField = fieldDate
Dim strAppID(0) As String
strAppID(0) = "appID"
Dim colLoad As DataControlField
'submitted column
If submitted Then
Dim fieldLoad As New TemplateField
fieldLoad.ItemTemplate = New GridViewTemplate(DataControlRowType.DataRow, "submitted")
colLoad = fieldLoad
Else
Dim fieldLoad As New HyperLinkField
fieldLoad.Text = "<b>Load Application »</b>"
fieldLoad.DataTextFormatString = "{0}"
fieldLoad.DataNavigateUrlFields = strAppID
fieldLoad.DataNavigateUrlFormatString = "?load={0}"
colLoad = fieldLoad
End If
'add the columns to the gridview
With grdAppSelect
.ID = "grdAppSelect"
.CssClass = "grdAppSelect"
.CellPadding = 5
.BorderWidth = "0"
.HeaderStyle.HorizontalAlign = HorizontalAlign.Left
With .Columns
.Add(colPosition)
.Add(colDate)
.Add(colLoad)
End With
.AutoGenerateColumns = False
.DataSource = ds.Tables(0)
.DataBind()
End With
Dim lnkNew As New HyperLink
lnkNew.Text = "<b>Start New Application »</b>"
lnkNew.NavigateUrl = "?load="
Dim strClear As New LiteralControl
strClear.Text = "<br class='clearer' />"
'add the controls to the panel
pnlAppSelect.Controls.Add(grdAppSelect)
pnlAppSelect.Controls.Add(strClear)
'pnlAppSelect.Controls.Add(lnkNew)
Else
'should be apps there but couldn't find them
lblGeneralError.Text = "Could not find any previously started applications."
End If
End Sub
I found the solution. I needed to add an event handler when I created the gridview so that I could access the RowDataBound event and do the evaluation there. Here's the event handler code I used:
AddHandler grdAppSelect.RowDataBound, AddressOf grdAppSelect_RowDataBound
Then I just did my evaluation during RowDataBound like so:
Sub grdAppSelect_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim load As Control = e.Row.Controls(2)
Dim submitted As Control = e.Row.Controls(3)
Dim submittedText As String = e.Row.Cells(3).Text
If submittedText = "True" Then
load.Visible = False
End If
submitted.Visible = False
End If
End Sub