PageIndexChanging Without Binding - asp.net

I have datagridview linked with dataSource, this data source get data from table has 9 Million record and I'm searching in this table by name, after Binding data when need to change PageIndex this requires to research in 9 million records by this code:
Any method to change PageIndex without binding it again?
Protected Sub GVCenteralSearch_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles GVCenteralSearch.PageIndexChanging
GVCenteralSearch.PageIndex = e.NewPageIndex
GVCenteralSearch.DataSource = CentralSearch_DS
GVCenteralSearch.DataBind()
End Sub
Notes : This Is Based on WebApp (ASP.net)

Not that I know of. You do have a few options though, you can cache the data so you don't have to get it from the database again. You could also look into javascript alternative to paging the data (jQuery DataTable plugin). Alternatively you can re-write the query to only grab the records you need. This would still take the round trip to get the data displayed, but it won't be as significant.

Related

how to use session in transferring id from one aspx page to another aspx page

I have two aspx pages. The first one contains the gridview list of all the vessels and the other one consists the application form for permit. Once i select a row in the gridview list,
a button for the permit application will be visible, this button once clicked, redirects to the second page which is the application form for permit. The problem is i have to get the id of the vessel from the selected row on the gridview list (first aspx page) and transfer it to the second aspx page. The id should be inserted in the permit application table together with the other data in the permit application form.
It sounds like this is your situation, roughly speaking: you have aspx pages A and B. Your user inputs something on A that is needed by B to render something else. To me the fact that you are rendering a table on B is irrelevant; if you could simply echo the value inputted from A onto B you would know what to do from there. So I am going to ignore the data table stuff.
You could use a session variable to transmit this value from A to B, but you probably shouldn't. There are a few good reasons not to do so; for starters, session variables are persisted in memory and therefore need to be managed.
If you insisted on using session variables, you would handle the event of, say, clicking submit on A by storing the text box's value into a session variable of your choice:
Public Sub Submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
Session("my_session_variable_name") = myTextBox.Text
End Sub
And then in B, in the appropriate lifecycle event (say, on load) you would read the session variable and produce the table or whatever you are trying to accomplish:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
If Session("my_session_variable_name") <> "" Then
id = Session("my_session_variable_name")
End If
But I would highly discourage you from doing it this way. Instead, try to make your B page take in either a query string parameter or a POSTed body member, and read it using Request.QueryString[] or Request.Form[].
You should use CommandName and CommandArgument attributes of the Button.
<asp:Button runat="server" ID="btn" CommandName="Permit" CommandArgument="6" />
But CommandArgument must be bind OnRowDataBound event of the gridview. So when you click you will check command name and get command argument value and pass it to permit form.

How to avoid using Session

i have an interface that contains data-grid. When i add element to data-grid, i add it also into List property that is the data-source of my Data-grid. Here the declaration of my list in code-behind:
Public Property listeSpecialite() As List(Of RECSPECIALITECONCOURS)
Get
Return Session("specialite")
End Get
Set(ByVal value As List(Of RECSPECIALITECONCOURS))
Session("specialite") = value
End Set
End Property
and here is the code when i add element to Data Grid :
Protected Sub gridsecialite_ItemCommand(source As Object, e As System.Web.UI.WebControls.DataGridCommandEventArgs) Handles gridsecialite.ItemCommand
If e.CommandName = "Insert" Then
Dim dropSpecialite As DropDownList = CType(e.Item.FindControl("txtSpecialite_Footer"), DropDownList)
Dim specialite As New RECSPECIALITECONCOURS
specialite.CODESPECIALITE = IGS.ChercherParIdInt(Of GENSPECIALITE)(CInt(dropSpecialite.SelectedValue))
listeSpecialite.Add(specialite)
gridsecialite.DataSource = listeSpecialite
gridsecialite.DataBind()
End If
End Sub
and in user click save, i save all element in my list to database.
how can i save element of the list without the use of session. (my boss say that it's not good to store list of element in session-performance reason)
You can use ViewState to store listeSpecialite. Just make sure to understand how it works to ensure that match what you need.
Viewstate has its own drawbacks. More process time for serialization/deserialization, encoding/deocding in B64 and for the page lifecycle in load/restore viewstate of the page, etc. Also, by default, viewstate is sent to the client in a hidden field. This increase bandwidth.
Anyway, if your list has not to be persisted out of your current view I would use Viewstate.
EDIT:
With list of 1000 items viewstate will be hurge. How about read from data base persisted items and keep in viewstate just the added items?. You could retrieve items of database, retrieve items from viewstate, combine and bind to datagrid. It is another strategy to avoid session and hurge viewstate at the cost of read database every postback.
As I said, there is no silver bullet... :-P

PagedDataSource not working with previous/next pages

I'm having a hard time with this scenario and hoping someone can help me clarify where I'm missing a step/logic. I tried searching online but I couldn't find an example that addressed this issue.
We are setting up a search page that when first loads shows a bunch of options (e.g., textboxes, checkboxes, etc). The user will fill out the form and submit the form back to itself. Once posted back, the page will build and run a SQL query (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3) against the database with the user's options and then the results will show up. This part works ok.
The part that breaks down is when I am trying to add pagination. The result set is a DataTable bound to a Repeater. I am using a PagedDataSource to add pagination. Pagination works great for the first page but for subsequent pages, it does not work. Basically, instead of returning the next page of the result requested (e.g., SELECT ID FROM Customers WHERE Company = 'Acme' AND AmtDue = 3), the results returned is the SQL query before appending the user's search options (e.g., SELECT ID FROM Customers).
I think my basic problem is that I'm not sure how to distinguish a normal Page.IsPostBack and paginating through results. The reason why this is causing problems is because I don't want to be regathering form data, rebuilding query, and requerying the DB.
The examples that I found online involved pagination of a DataTable that is rebuilt every time the page loads (e.g., Not Page.IsPostBack).
Here is a rough outline of our code:
Public dtCustomers As DataTable = New DataTable()
Sub Page_Load(ByVal Sender As Object, ByVal E As EventArgs) Handles Me.Load
' When the page first loads, show the search form with search options.
If Not Page.IsPostBack Then
ShowSearchForm()
' Once the form is submitted, show the search results.
Else
' ----------------------------------------
' This is the part that I'm having trouble with. How do I skip the following two steps (GatherFormData() and BuildDT()) when the user requests a subsequent page?
GatherFormData()
dtCustomers = BuildDT()
' ----------------------------------------
BindData()
End If
End Sub
Sub BindData()
Dim objPDS As PagedDataSource = New PagedDataSource()
objPDS.DataSource = dtCustomers.DefaultView
objPDS.AllowPaging = True
objPDS.PageSize = intNumPerPage
objPDS.CurrentPageIndex = Me.ViewState("_CurrentPage")
rptSearchResults.DataSource = objPDS
End Sub
' Subroutine called when the previous page button is pressed.
Sub GoToPrevPage()
' Set viewstate variable to the previous page.
Me.ViewState("_CurrentPage") -= 1
BindData()
End Sub
' Subroutine called when the next page button is pressed.
Sub GoToNextPage()
' Set viewstate variable to the next page.
Me.ViewState("_CurrentPage") += 1
BindData()
End Sub
Note: I understand that the DataTable will have to be put into cache or a session variable, but haven't decided the best method.
Please excuse the code outline but the actual code is massive so the simplification is to make it easier to get to the heart of the issue.
Let me know if any of that was unclear. Thanks in advance!
I am assuming that you are going to store your data in session/cache (I would prefer the later but there can be use case for storing it in session) - you can store the key in the view-state and presence of key could be use to determine if post-back is for pagination or not. For example,
if (ViewState["dataKey"] == null)
{
// first form submittal, do the search
GatherFormData();
dtCustomers = BuildDT();
// store it in cache
ViewState["dataKey"] = Guid.NewGuid().ToString(); // create a key
Cache.[ViewState["dataKey"].ToString()] = dtCustomers; // TODO use Add method to control expiration etc
}
Its important that you clear the view-state when the search is reset (or similar condition)
Lastly, it is also possible to do the paging from database/data-store (for example, using ranking functions in SQL Server) so that you need not have to store the search results into the session/cache but rather do a database trip at each post-back. Such approach is useful when the complete result set size can be huge.

gridview filtering via joined tables

I'm trying to display data which comes from a join on two tables (or more) in a gridview
I want to be able to filter the result set via user given input (text boxes on page)
I have tried the standard tutorials but cannot find something which goes beyond displaying one table result sets in the gridview. If I go through the configure gridview and configure datasource wizard for any datasource (sqlDatasource, object, entitydatasource), when I use multiple tables I cannot use the 'where' parameters in the wizard, and therefore need to have the selecting code in the code-behind, but i'm unsure where exactly to put this.
e.g. if I have
Protected Sub button1_Click(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles button1.Click
sqldatasource1.selectcommand = "select * from yourtable where modelfamily
like '%" & textbox1.text & "%' or description like '%" &
textbox1.text & "%'"
sqldatasource1.databind()
End Sub
then this code will help with a search button for part 2 of my question, e.g. if button1 is a search button then I can incorporate the text box values in the query and bind,
but where would I place the intiial data binding code, in page_load? And where shall I place the other other code? Please could you provide me a link to a solution, or better help me here please.
I want to be able to:
use entity data source to do this (how can I do this multiple join in entity framework, or other source if much easier)
I want to display all the results intially (coming from two tables) with paging and sorting enabled
if text box has a value and search button clicked, then subset of the data based on the value is shown, and is also page-able/sort-able within itself.
Please can you show me examples of where would the code sit, which events and what will it look like?
Have a look at this Inner Join statement and gridview

How do i get row keys / cell values & call a method when a row is selected in the standard .net 2.0 Gridview

I have standard .net 2.0 gridview control from which i want to get row keys or cell values from the grid when a row is selected.
I also need to call a method each time a row is selected.
Does anybody know How i can do this using ASP.net & VB?
I assume you mean selection by the Select command. That fires a SelectedIndexChanged event. and from there on you have the SelectedDataKey.Value (or .Values) for the Key.
Protected Sub GridView1_SelectedIndexChanged(ByVal sender As Object, _
ByVal e As EventArgs) _
Handles GridView1.SelectedIndexChanged
Dim key As Object = GridView1.SelectedDataKey.Value
End Sub
Add a button or link button to your gridView.
For the command name put in "Select"
In the designer window, double click your gridView and your selected index changed method will be automatically generated.
I prefer using third-party grid like the Telerik one which allows server and client selection as well as postback on click to process some logic.
Dick

Resources