Gridview edit Row going to next page in asp.net - asp.net

When i am trying to edit a editable gridview by clicking on edit
button. (so without inserting data) when i navigate to next page of
the grid.i saw that grid row which i edited is being open to edit
without clicking edit button. in 1st page(1st image) of the grid u can
see that i am trying to edit 6th row
in 2nd page of the grid you can see that 6th row is changed to
editable mode without even clicking on edit button
vb.net code
Protected Sub BindGridSubMeter()
Dim conMRI As New ConnectionMRI()
Dim ds As DataSet = conMRI.NabersSubMetergetData(ddlBldgId.SelectedItem.Value, ddlRating.SelectedItem.Value)
TextExclusions.Text = ds.Tables(1).Rows(0).Item(0).ToString()
UpdatePanelExclusions.update()
With GridSubMeter
.DataSource = ds.Tables(0)
.DataBind()
End With
End Sub
Protected Sub GridSubMeter_RowEditing(sender As Object, e As System.Web.UI.WebControls.GridViewEditEventArgs) Handles GridSubMeter.RowEditing
GridSubMeter.EditIndex = e.NewEditIndex
Me.BindGridSubMeter()
End Sub
Protected Sub GridSubmeter_PageIndexChanging(sender As Object, e4 As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridSubMeter.PageIndexChanging
GridSubMeter.PageIndex = e4.NewPageIndex
Me.BindGridSubMeter()
End Sub

You do not reset the EditIndex on PageIndexChanging. The edit index is based on the visible items, not the total.
Protected Sub GridSubmeter_PageIndexChanging(sender As Object, e4 As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GridSubMeter.PageIndexChanging
GridSubMeter.EditIndex = -1
GridSubMeter.PageIndex = e4.NewPageIndex
Me.BindGridSubMeter()
End Sub

Related

how to redirect to another page, that generates a gridview, after clicking a button in the first page

i have some different buttons in a page after clicking them i want them to redirect me to a page that has a gridview , but for each button it gives a different gridview which are generated by datatables . may you help to find ,how can i do that in asp.net, vb ??
i did two pages at the gridview page i made methods that are binding different gridviews for each button , and in the first page where i have the button click events i did the redirecting to this gridview page and called the corresponding methods. it redirects me but doen't give me any gridview at all :(
Call in button action
Response.Redirect("Your Page Name.aspx")
and call in that page a BindGrid Function that you create to bind the gridview
You can redirect by using
Response.Redirect("GridView.aspx")
At the same time you can use only one aspx by using session
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Session.Add("CheckButton", Button1Clicked)
Response.Redirect("GridView.aspx")
End Sub
Protected Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Session.Add("CheckButton", Button2Clicked)
Response.Redirect("GridView.aspx")
End Sub
After that at your GridView.aspx on your pageload
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim CheckButton As String = Session("CheckButton")
Dim query As String
If (CheckButton = "Button1Clicked") Then
query = "SELECT * FROM Table1"
Else If (CheckButton = "Button2Clicked") Then
query = "SELECT * FROM Table2"
Else
Response.Write("<script>alert('Error!')</script>")
End If
SqlDataSource1.SelectCommand = query
SqlDataSource1.DataBind()
End Sub

Programmatically databind listview while using datapager

I have a listview and two data pagers. My listview is hooked up to a data source whose data is ordered randomly. ORDER BY NEWID()
As you can imagine, each time I select a page or click next/prev page all the data is randomized making the datapager quite useless.
I figured I could set the datasource when the page is not posted back and then programmatically set the datasource to the listview but now when selecting a page or clicking the next/prev buttons, the page just simply doesn't change..
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
lvListMembres.DataSource = dv
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End If
End Sub
What am I missing / is there a better way to do this. The order by MUST be random and I MUST have the data pagers separate from the listview.
Thank you!
Take the code between the If statement and place it in a separate function.
Add a method of storing the data, in this particular case I'll store it as a DataTable in a session.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not IsPostBack Then
' Clear datatable from session
Session("CourtierList_dt") = Nothing
' Set datasource to listview and save in a session variable
SetCourtierList()
End If
End Sub
Protected Sub SetCourtierList()
Dim dt As DataTable
If Session("CourtierList_dt") Is Nothing Then
Dim dv As DataView = sdsMembres.Select(DataSourceSelectArguments.Empty)
Session("CourtierList_dt") = dv.ToTable
End If
dt = Session("CourtierList_dt")
lvListMembres.DataSource = dt
lvListMembres.DataBind()
DataPager2.PagedControlID = "lvListMembres"
DataPager3.PagedControlID = "lvListMembres"
DataPager2.DataBind()
DataPager3.DataBind()
End Sub
Also, make sure to add the PagePropertiesChanging event to the listview
Protected Sub lvListMembres_PagePropertiesChanging(sender As Object, e As System.Web.UI.WebControls.PagePropertiesChangingEventArgs) Handles lvListMembres.PagePropertiesChanging
DataPager2.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
DataPager3.SetPageProperties(e.StartRowIndex, e.MaximumRows, False)
SetCourtierList()
End Sub
And there you have it, hope this is helpful for someone.

Failure to update textbox.text

I have a relatively simple ASP.NET problem (I should think) that regrettably I am unable to solve by myself. What I am trying to do is the following:
On a page I load a number of controls (Text Boxes) programmatically;
following this load the user should be able to select a value to load into the Textbox from a panel control that is added to the page following the click of a button
Once the panel is closed, the selected text from the panel should be loaded into the textbox
However, in the vb.net statements below when run the "test" string never makes it to the textbox - any help with resolving this would be greatly appreciated.
Public Class test
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Controls_Load()
End Sub
Public Sub Controls_Load()
Dim ttf_tb As New TextBox With {.ID = "ttf_tb"}
Master_Panel.Controls.Add(ttf_tb)
Dim ttf_button As New Button
Master_Panel.Controls.Add(ttf_button)
AddHandler ttf_button.Click, AddressOf TTF_BUTTON_CLICK
End Sub
Public Sub TTF_BUTTON_CLICK(sender As Object, e As EventArgs)
Dim str As String = sender.id
Dim panel As New Panel
panel.ID = "TTF_Panel"
panel.Width = 300
panel.Height = 300
Master_Panel.Controls.Add(panel)
panel.BackColor = Drawing.Color.Black
panel.Style.Add(HtmlTextWriterStyle.Position, "absolute")
panel.Style.Add(HtmlTextWriterStyle.Left, "200px")
panel.Style.Add(HtmlTextWriterStyle.Top, "100px")
panel.Style.Add(HtmlTextWriterStyle.ZIndex, "100")
Dim CL_Button As New Button
CL_Button.ID = "TTF_Close_" & Replace(str, "TTF_Button_", "")
panel.Controls.Add(CL_Button)
AddHandler CL_Button.Click, AddressOf TTF_Close_Button_Click
End Sub
Public Sub TTF_Close_Button_Click(sender As Object, e As EventArgs)
Dim ttf_tb As TextBox = Master_Panel.FindControl("ttf_tb")
ttf_tb.Text = "Test"
Dim panel As Panel = FindControl("TTF_Panel")
Master_Panel.Controls.Remove(panel)
End Sub
End Class
I think you need to re-create your controls in the Page_Init method. It's been a while since I've done web forms but I think it's something like:
When a user clicks the button a post back is fired. This re-creates a new instance of your class, creates the controls on the page, assigns any form values then calls your Page_Load event.
The problem is you are creating your controls too late, so the forms values are never assigned correctly.
You should create / recreate your dynamic controls in the Init event:
Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
Controls_Load()
End Sub
This should allow you to maintain their state across PostBacks.
For more information about this topic, see the MSDN article on the ASP.NET Page Life Cycle.

How to get paging on IEnumerator object

I have list of rows stored in object IEnumerator.
I need to display the row items as paging so that On Next button click it would display next item from the object while on pressing Previous button it should display the previous row.
My failed attempted code below...
Dim en As IEnumerator
Dim row As DataRow
Dim dt As DataTable dt = loadTips() // return datatable
en = dt.Rows.GetEnumerator()
Protected Sub btn_nxt_Click(sender As Object, e As EventArgs) Handles btn_nxt.Click
While en.MoveNext
row = en.Current
Response.Write(row(0))
End While
End Sub
Protected Sub btn_pre_Click(sender As Object, e As EventArgs) Handles btn_pre.Click
' code to move previous row
End Sub
You need to store the current page and simplify the enumerator to just those items.
Skip and Take are helper methods to simplify this.

VB.NET CheckboxList doubles when button is clicked. Why?

Every time I click the Delete Button, the check marks double in quantity:
Public Class About
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End Sub
Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnDelete.Click
For count As Integer = 0 To CheckBoxList1.Items.Count - 1
If CheckBoxList1.Items(count).Selected Then
File.Delete("C:\Images\" & CheckBoxList1.Items(count).ToString)
End If
Next
End Sub
End Class
The checkboxlist isn't refreshed so that the checkbox that I deleted is removed from the checkboxlist. How do I accomplish that? Thanks!
On a button click, Page_Load is ran again, so the code that adds the checkboxes is ran a second time.
Add a check for Page.IsPostBack, and only add the checkboxes if it is not a postback.
If Not Page.IsPostBack Then
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If
(I hope syntax is right... Not used to VB)
the whole content of the Page_Load event handler has to be executed only once in your case, so rewrite it like this:
If Not IsPostBack Then
Dim di As New IO.DirectoryInfo("C:\Images\")
Dim imageArray As IO.FileInfo() = di.GetFiles()
Dim image As IO.FileInfo
'clear imageArray
'list the names of all images in the specified directory
For Each image In imageArray
CheckBoxList1.Items.Add(image.Name)
Next
End If

Resources