DataPager value setting and getting with VB.net - asp.net

Is there some way to trap/get the value that a Data Pager advances to on the button click for Next or Previous record when navigating a ListView control?
And what about telling the pager which page to display programmatically?

you should use the PagePropertiesChanging event to get current page
Protected Sub ListView1_PagePropertiesChanging(sender As Object, e As PagePropertiesChangingEventArgs)
Dim CurrentPage As Integer = (e.StartRowIndex / e.MaximumRows) + 1
Response.Write(CurrentPage.ToString())
End Sub

Related

How to find selected GridView row in page_load event?

I have a page with GridView. The GridView has select button. Normally I use GridView's selected index changed event to do all kinds of operations when user clicks select button. But now I want to do some operations in Page_Load event based on grid view's selected row. Since the Selected_Index_changed event occurs after Page_Load how do I know following things in page load event.
I checked the asp lifecycle and this other question but I dont know how to do this.
How about using a QueryString to transmit which row was selected and then in the Page_Load event get the QueryString parameters? This is an example.
Protected Sub LinkButton1_Command(sender As Object, e As CommandEventArgs)
Dim UserId As Integer = e.CommandArgument 'Here goes whatever value you're trying to pass
Response.Redirect("~/OtherPage.aspx?UserId=" _
& UserId)
End Sub
This is in the OtherPage.aspx
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
UserId = Request.QueryString("UserId")
'Your code
end sub

Toggle column display in ASP.NET gridview

I have two email fields, one a text field (index 15), the other a mailto: hyperlink (index 16), both in a gridview. (And yes, I know identifying via index isn't the best way to go -- just trying to make it work at this point).
When not editing, I need to show only the hyperlink field (making it available for the user to click on). When editing, I need to show only the text field, so they can modify the value.
I've got everything working as needed except that both fields display when the grid is initially shown. If I try to hide the text field in any of the normal ways (hiding cells on RowDataBound or hiding the column upon declaration), then it doesn't show up when editing.
Here's what I'm doing so far. The RowEditing event has the following code:
GridView1.Columns(16).Visible = False
GridView1.Columns(15).Visible = True
The RowCancelingEdit event has the opposite logic, toggling visibility on both fields. And finally the RowUpdating event has the following, which turns the hyperlink display back on:
GridView1.Columns(16).Visible = True
I'm relatively new to ASP.NET, so I definitely don't know all of the constructs available.
How can I hide the text field upon normal grid display, but still have the field available to show when in edit mode?
Try to RowCommand Event and set Edit button CommandName="name"
If e.CommandName = "name" Then
Dim row As GridViewRow = DirectCast(DirectCast(e.CommandSource, LinkButton).NamingContainer, GridViewRow)
Dim lblwwwhid = CType(row.FindControl("txtwwwhid"), Label)
lblwwwhid .visible =false
End if
It dawned on me that I could simply show/hide columns upon the initial databind (which works), as such:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
DataBind()
GridView1.Columns(16).Visible = True
GridView1.Columns(15).Visible = False
End If
End Sub

IF SelectedItem.Equals ??? Then redirect Statement

I have a gridview in my website in am building in Visiual studio 2010. IM new to programming and have a query in reference to redirecting the user to another page based on the results of the gridview.
My gridview works perfectly fine and pulls back items via a selection made in a dropdown list. These items have and id assigned (1 for Weekly & 2 for Monthly).
My users selects the Weekly or Montly option from the dropdown and the grid view is populated with this data. (This part works perfectly fine).
Once the results are displayed i want the user to then press the 'Create' button and for them to be directed to the correct Weekly.aspx or Monthly.aspx page based on the selected item from the dropdown list.
So far i have the following code which seems to redirect the user to the Monthly.aspx page for either selection from the dropdown list.
Protected Sub btnCreate_Agenda_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnCreate_Agenda.Click
If DropDownList1.SelectedItem.Equals("1") Then
Response.Redirect("Weekly.aspx")
Else
Response.Redirect("Monthly.aspx")
End If
End Sub
Can anyone point me in the right direction?
In my page load event i would also like to make sure that for the webpage item 1 (Weekly) is the default selection. I have tried doing this by adding the following code to the page_load event but i have no just with the results.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
DropDownList1.SelectedItem.Equals("1")
End Sub
You need to assign the value to the dropdown or choose the index of the item, not compare the current value to something.
DropDownList1.SelectedValue = "1"
Or
DropDownList1.SelectedIndex = 1
The SelectedItem property will return a ListItem object, so that will never be equal to the string "1". Use the SelectedValue property instead:
If DropDownList1.SelectedValue = "1" Then
Response.Redirect("Weekly.aspx")
Else
Response.Redirect("Monthly.aspx")
End If
Regarding setting the default, the SelectedItem property is a read-only property. Use the SelectedIndex or SelectedValue properties to select an item:
DropDownList1.SelectedValue = "1"

Reset values inside a repeater

I have a repeater which displays products. Users can select a Size - dropdownlist and an Amount - textbox. Then they press Order or Cancel. When they press Cancel I would like the values of Size and Amount to return to their default values.
Protected Sub lbtnCancel_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lbtnCancel.Click
rpt.ProductList is the repeater name
lblFeedback.Text = ("")
lblFeedback.ForeColor = Drawing.Color.Black
End Sub
Any help is welcome!
I am using VB.NET.
What if you rebind your repeater on the Cancel Click, so it will again be populated with your Default Values ?
Use the rptProductList_ItemCommand event to catch the repeateritem line and play with the controls as you please ...

Accessing dynamically added control from GridView Footer row

I have GridView control in my asp.net page with auto generated fields
there is only footer template is present under asp:TemplateField.
I can bind this gridview with any of databae table as depend on user
selection. Now want to add new record in database so I have added text
boxes on footer template cells at runtime depend on number of columns
on table. But when I accessing these text boxes from footer template
on gridview_RowCommand event its not retriving textbox control.
this is the code:
SGridView.ShowFooter = True
For i As Integer = 0 To ctrList.Count
Dim ctr As Control = CType(ctrList.Item(i), Control)
SGridView.FooterRow.Cells(i + 1).Controls.Add(ctr)
Next
the ctrList contain Controls textBox, checkbox dropdowlist ect.
there is all ok
but when i wont to get the text or value or checked value of controls i can't cast the controls in rowcommand event
Here is the code:
If e.CommandName = "Add" Then
Dim ctrList As ControlCollection = SGridView.FooterRow.Controls
For Each ctr As Control In ctrList
If TypeOf ctr Is TextBox Then
Dim name As TextBox = CType(ctr, TextBox)
Dim val As String = name.Text
End If
Next
End If
this excample is for the textBox control.
Plese suggest me how can I get footer control textboxes. So that I can
save data in database.
When you are dealing with Dynamic controls you have to keep a close eye on the .net page lifecycle.
Control data is bound in the Load event, so your not able to access postback data in that event when your doing your conrol generation in the Load event too. I usualy try to create dynamic controls in the Init of the page, and do any values processing on the LoadComplete or PreRender event so I can make sure they have recieved their values from the postback before you try to read them.
Take a look at the full description of the ASP.NET page lifecycle events and whats going on. This should help you navigate the creation and use of Dynamic generated controls.
Try to create your Controls in the Grdiview's RowCreated Event which will be raised on every Postback.
Private Sub Grid1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles Grid1.RowCreated
Select Case e.Row.RowType
Case DataControlRowType.Footer
'add controls to row
End Select
End Sub

Resources