|
-
September 15th, 2009, 02:09 AM
#1
Paging a Gridview Problem
Hi there,
I am new to C# / .NET and programming in general and I have come across a problem where I have enabled paging on a gridview and I can't get results on subsequent pages. The first page displays correctly but the subsequent pages don't work.
Basically I have a page with loads of search boxes and a search button and a clear button. The search button is populating the Gridview and I have paging unabled for the gridview. I think my problem is that I need to put something in:
"protected void grResults_PageIndexChanging(object sender, GridViewPageEventArgs e)"
But I don't need to recall everything that's in my search button part of the code.
Any ideas people.
Thanks
Alex
//code
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class RegisterSearch : System.Web.UI.Page
{
protected void btnSearch_Click(object sender, EventArgs e)
{
// Define data objects
SqlConnection conn;
DataSet dataSet = new DataSet();
SqlDataAdapter adapter;
// Read the connection string from Web.config
string connectionString = ConfigurationManager.ConnectionStrings["Register"].ConnectionString;
// Initialize connection
conn = new SqlConnection(connectionString);
String strWhere;
int FCount;
strWhere = "";
FCount = 0;
//Fornames
if (txtFornames.Text == "")
{
strWhere = strWhere + "";
}
else
{
strWhere = "fornames LIKE '%" + txtFornames.Text + "%'";
FCount = 1;
}
//Surname
if (txtSurname.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "surname LIKE '%" + txtSurname.Text + "%'";
}
//Flat
if (txtFlat.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "flat LIKE '%" + txtFlat.Text + "%'";
}
//House Name
if (txtHousename.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "housename LIKE '%" + txtHousename.Text + "%'";
}
//House Number
if (txtHousenumber.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "housenumber LIKE '%" + txtHousenumber.Text + "%'";
}
//Street Name
if (txtStreetname.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "streetname LIKE '%" + txtStreetname.Text + "%'";
}
//Sub Street
if (txtSubstreet.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "substreet LIKE '%" + txtSubstreet.Text + "%'";
}
//Street Add1
if (txtStreetadd1.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "streetadd1 LIKE '%" + txtStreetadd1.Text + "%'";
}
//Street Add2
if (txtStreetadd2.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "streetadd2 LIKE '%" + txtStreetadd2.Text + "%'";
}
//Street Add3
if (txtStreetadd3.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "streetadd3 LIKE '%" + txtStreetadd3.Text + "%'";
}
//Street Add4
if (txtStreetadd4.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "streetadd4 LIKE '%" + txtStreetadd4.Text + "%'";
}
//Postcode
if (txtPostcode.Text == "")
{
strWhere = strWhere + "";
}
else
{
if (FCount >= 1)
{
strWhere = strWhere + " AND ";
}
strWhere = strWhere + "Postcode LIKE '%" + txtPostcode.Text + "%'";
}
if (strWhere == "")
{ messageLabel.Text = "Your search criteria doesn't match any records in the system."; }
else
{
String strSQL = "SELECT district, elno, fornames, surname, flat, housename, housenumber, substreet, streetname, streetadd1, streetadd2, streetadd3, streetadd4, postcode " +
" FROM elector_2008 WHERE " + strWhere;
// Create adapter
adapter = new SqlDataAdapter(strSQL, conn);
// Fill the DataSet
adapter.Fill(dataSet, "Register");
// Initialize the adapter with a new command
adapter.SelectCommand = new SqlCommand(strSQL, conn);
// Bind the grid to the DataSet
grResults.DataSource = dataSet;
grResults.DataBind();
grResults.Visible = true;
}
}
protected void btnClear_Click(object sender, EventArgs e)
{
txtFornames.Text = "";
txtSurname.Text = "";
txtFlat.Text = "";
txtHousename.Text = "";
txtHousenumber.Text = "";
txtStreetname.Text = "";
txtSubstreet.Text = "";
txtStreetadd1.Text = "";
txtStreetadd2.Text = "";
txtStreetadd3.Text = "";
txtStreetadd4.Text = "";
txtPostcode.Text = "";
messageLabel.Text = "";
grResults.Visible = false;
}
protected void grResults_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
}
}
-
September 15th, 2009, 05:13 AM
#2
Re: Paging a Gridview Problem
Please use code-tags to post code! ( [code ] your code here [/ code] , but then without the spaces)
-
September 15th, 2009, 05:30 AM
#3
Re: Paging a Gridview Problem
hi AlexSpod,
there is several procedure :
put whole the query in session or viewstate that why u needn't call every time and when you bind the data source, use 'strSQL' getting from ViewState instead of 'strWhere'
U didn't provide the .aspx code, I think u should define gridview like :
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
OnPageIndexChanging="GridView1_PageIndexChanging"
PageSize="5">
and in code behind write like :
protected void GridView1_PageIndexChanging(object sender,
GridViewPageEventArgs e)
{
Response.Write("<br>NewpageIndex: " + e.NewPageIndex);
GridView1.PageIndex = e.NewPageIndex;
GridView1.DataSource = GetDataSource();
GridView1.DataBind();
}
please try to follow this.
In another process and more professional is Stored Procedure.
You know about using stored Procedure of SQL server?? If not please try to use this.. Hope this can help u.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|