Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

GridView Sorting Problem

Status
Not open for further replies.

DotNetDunce

Programmer
Aug 23, 2001
47
US
I have a dynamically created gridview that when sorted only sorts to descending order. The problem is that the sort is firing twice. I've looked over the forum and google and the only suggestion I've seen is to make sure the the page has not ispostback in page_load. With this in mind I'm sure I'm having the same problem BUT this is a search page. Nothing is loaded onto the screen until the search button is clicked and then the click event is fired which contains a databind command. The sorting event also contains this command. What do I do to stop it from binding twice?

Thanks in advance for your help!
 
Here's the code. Thanks again!

[Code Search_click()]
Dim DBdataset As DataSet
Dim DBAdapter As OleDbDataAdapter

connection = New OleDbConnection(connectionString)

' Create a OleDbDataAdapter for the table.
DBAdapter = New OleDbDataAdapter()


' A table mapping names the DataTable.
DBAdapter.TableMappings.Add("Table", "Tickets")

' Open the connection.
connection.Open()

' Create a OleDbCommand to retrieve Tickets data.
Dim DBSelectCommand As OleDbCommand
Dim sStat As String
sStat = "New"

If IsNumeric(txtSearch.Text) Then
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [ticketno] = " & txtSearch.Text, connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND [ticketno] = " & txtSearch.Text, connection)
End If
ElseIf Not IsNumeric(txtSearch.Text) And txtSearch.Text <> "" Then
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%'", connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND ([lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%')", connection)
End If
Else
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets", connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets WHERE status <> '" & sStat & "'", connection)
End If
End If

DBSelectCommand.CommandType = CommandType.Text

' Set the OleDbDataAdapter's SelectCommand.
DBAdapter.SelectCommand = DBSelectCommand

' Fill the DataSet.
DBdataset = New DataSet("Tickets")

DBAdapter.Fill(DBdataset)

connection.Close()

Dim Dbdatatable As DataTable
Dbdatatable = DBdataset.Tables(0)

DBView = Dbdatatable.DefaultView

Me.GridView1.DataSource = DBdataset
Me.GridView1.DataBind()
[/Code]

[Code GridView1_Sorting()]
Dim DBdataset As DataSet
Dim DBAdapter As OleDbDataAdapter
connection = New OleDbConnection(connectionString)

' Create a OleDbDataAdapter for the table.
DBAdapter = New OleDbDataAdapter()


' A table mapping names the DataTable.
DBAdapter.TableMappings.Add("Table", "Tickets")

' Open the connection.
connection.Open()

' Create a OleDbCommand to retrieve Tickets data.
Dim sStat As String
sStat = "New"
Dim DBSelectCommand As OleDbCommand
If IsNumeric(txtSearch.Text) Then
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [ticketno] = " & txtSearch.Text, connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND [ticketno] = " & txtSearch.Text, connection)
End If
ElseIf Not IsNumeric(txtSearch.Text) And txtSearch.Text <> "" Then
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where [lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%'", connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] from Tickets Where status <> '" & sStat & "' AND ([lname] LIKE '%" & txtSearch.Text & "%' OR [product] LIKE '%" & txtSearch.Text & "%' OR [company] LIKE '%" & txtSearch.Text & "%')", connection)
End If
Else
If Session("UserType") = "Admin" Then
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets", connection)
Else
DBSelectCommand = New OleDbCommand("SELECT [ticketno], [lname], [company], [product], [date_entered], [assigned], [status] FROM Tickets WHERE status <> '" & sStat & "'", connection)
End If
End If

DBSelectCommand.CommandType = CommandType.Text

' Set the OleDbDataAdapter's SelectCommand.
DBAdapter.SelectCommand = DBSelectCommand

' Fill the DataSet.
DBdataset = New DataSet("Tickets")

DBAdapter.Fill(DBdataset)

connection.Close()

Dim Dbdatatable As DataTable
Dbdatatable = DBdataset.Tables(0)

DBView = Dbdatatable.DefaultView

Dim sDirection As String
If ViewState("sortDirection") Is Nothing Then
ViewState("sortDirection") = SortDirection.Ascending
sDirection = " ASC"
ElseIf ViewState("sortDirection") = SortDirection.Ascending Then
ViewState("sortDirection") = SortDirection.Descending
sDirection = " DESC"
ElseIf ViewState("sortDirection") = SortDirection.Descending Then
ViewState("sortDirection") = SortDirection.Ascending
sDirection = " ASC"
Else
ViewState("sortDirection") = SortDirection.Ascending
sDirection = " ASC"
End If

DBView.Sort = e.SortExpression & sDirection
Me.GridView1.DataSource = DBView
Me.GridView1.DataBind()
[/Code]
 
Here is the html source code.

HTML:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml"[/URL] >
<head><title>
	Search
</title>
<script language="javascript">
    String.prototype.replaceAll = function (replaceValue, newValue)
{
 var functionReturn = this;

 while ( true )
 {
  var currentValue = functionReturn;

  functionReturn = functionReturn.replace(replaceValue, newValue);
  if ( functionReturn == currentValue )
   break;
 }

 return functionReturn;
};
function toPhoneNumber(textValue)
{
 var functionReturn = new String(textValue);

 functionReturn = functionReturn.replaceAll('-', '');
 functionReturn = functionReturn.replaceAll('(', '');
 functionReturn = functionReturn.replaceAll(')', '');
 functionReturn = functionReturn.replaceAll(' ', '');

 if ( functionReturn.length >= 10 )
 {
  var tempText = new String('');

  tempText = '(' + functionReturn.substr(0, 3) + ') ';
  tempText += functionReturn.substr(3, 3);
  tempText += '-';
  tempText += functionReturn.substr(6, 4);

  functionReturn = tempText;
 }

 return functionReturn;
}



</script>
<style type="text/css">
	.ctl00_Menu1_0 { background-color:white;visibility:hidden;display:none;position:absolute;left:0px;top:0px; }
	.ctl00_Menu1_1 { font-family:Arial;font-weight:bold;text-decoration:none; }
	.ctl00_Menu1_2 { font-family:Arial;font-weight:bold;width:166px; }
	.ctl00_Menu1_3 { font-family:Arial;font-weight:bold; }
	.ctl00_Menu1_4 { font-family:Arial;font-weight:bold; }

</style></head>
<body>
    <form name="aspnetForm" method="post" action="search.aspx" id="aspnetForm">
<div>
<input type="hidden" name="__EVENTTARGET" id="__EVENTTARGET" value="" />
<input type="hidden" name="__EVENTARGUMENT" id="__EVENTARGUMENT" value="" />
<input type="hidden" name="__VIEWSTATE" id="__VIEWSTATE" value="/wEPDwUKMTk2ODc0OTc4NQ8WAh4Nc29ydERpcmVjdGlvbgsqJ1N5c3RlbS5XZWIuVUkuV2ViQ29udHJvbHMuU29ydERpcmVjdGlvbgEWAmYPZBYCAgMPZBYKAgMPDxYCHgRUZXh0BRdXZWxjb21lLCBNZWxpbmRhIEt5emFyIWRkAgUPDxYCHwEFBUFkbWluZGQCBw88KwANAQwUKwAGZGRkZBQrAAIWAh8BBQ9DaGFuZ2UgUGFzc3dvcmRkFCsAAhYCHgdFbmFibGVkZ2RkAgsPDxYCHgdWaXNpYmxlZ2RkAg0PZBYCAgkPPCsADQEADxYGHgtfIURhdGFCb3VuZGceCVBhZ2VDb3VudAIBHgtfIUl0ZW1Db3VudAICZBYCZg9kFggCAQ9kFg5mD2QWAmYPDxYEHwEFAjE0HgtOYXZpZ2F0ZVVybAUXZGV0YWlsLmFzcHg/dGlja2V0bm89MTRkZAIBDw8WAh8BBQdKT0hOU09OZGQCAg8PFgIfAQUUSE9XQVJEIEpPSE5TT04sIElOQy5kZAIDDw8WAh8BBQtJQURTIEF1dGhvcmRkAgQPDxYCHwEFCjEwLzE3LzIwMDdkZAIFDw8WAh8BBQpHcmVnZyBHZWlzZGQCBg8PFgIfAQUIQXBwcm92ZWRkZAICD2QWDmYPZBYCZg8PFgQfAQUBOB8HBRZkZXRhaWwuYXNweD90aWNrZXRubz04ZGQCAQ8PFgIfAQUFQ0xPV05kZAICDw8WAh8BBQtDTE9XTlMgUiBVU2RkAgMPDxYCHwEFEklBRFMgQ29uZmlndXJhdGlvbmRkAgQPDxYCHwEFCTkvMjYvMjAwN2RkAgUPDxYCHwEFBiZuYnNwO2RkAgYPDxYCHwEFCUNvbXBsZXRlZGRkAgMPDxYCHwNoZGQCBA8PFgIfA2hkZBgBBSNjdGwwMCRDb250ZW50UGxhY2VIb2xkZXIxJEdyaWRWaWV3MQ9nZAyN4Sjtv8bvPLaDyYjHYWVueix1" />
</div>

<script type="text/javascript">
<!--
var theForm = document.forms['aspnetForm'];
if (!theForm) {
    theForm = document.aspnetForm;
}
function __doPostBack(eventTarget, eventArgument) {
    if (!theForm.onsubmit || (theForm.onsubmit() != false)) {
        theForm.__EVENTTARGET.value = eventTarget;
        theForm.__EVENTARGUMENT.value = eventArgument;
        theForm.submit();
    }
}
// -->
</script>


<script src="/DiacapTracker/WebResource.axd?d=qxqOhE8ltcakyDUFjCOkMg2&amp;t=632966621117219625" type="text/javascript"></script>


<script src="/DiacapTracker/WebResource.axd?d=Q3pM3KubhW9vfS_WVnk0jQ2&amp;t=632966621117219625" type="text/javascript"></script>
    <div>
        &nbsp;<table>
            <tr>
                <td colspan="2" valign="top">
                    <img id="ctl00_Image1" src="images/Logo.bmp" style="border-width:0px;" /></td>
            </tr>
            <tr>
                <td style="text-align: left; width: 90px; height: 236px;" valign="top">
                    <span id="ctl00_AuthUser">Welcome, Melinda Kyzar!</span><br />
                    <br />
                    <a href="#ctl00_Menu1_SkipLink"><img alt="Skip Navigation Links" src="/DiacapTracker/WebResource.axd?d=jNkXo862cFpTBcmeBzWRVA2&amp;t=632966621117219625" width="0" height="0" style="border-width:0px;" /></a><table id="ctl00_Menu1" class="ctl00_Menu1_2" cellpadding="0" cellspacing="0" border="0">
	<tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n0">
		<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1" href="New.aspx">New Ticket</a></td>
			</tr>
		</table></td>
	</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n1">
		<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1" href="Browse.aspx">Browse Tickets</a></td>
			</tr>
		</table></td>
	</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n2">
		<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1" href="search.aspx">Search Tickets</a></td>
			</tr>
		</table></td>
	</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n3">
		<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1" href="password.aspx">Change Password</a></td>
			</tr>
		</table></td>
	</tr><tr onmouseover="Menu_HoverStatic(this)" onmouseout="Menu_Unhover(this)" onkeyup="Menu_Key(this)" id="ctl00_Menu1n4">
		<td><table cellpadding="0" cellspacing="0" border="0" width="100%">
			<tr>
				<td style="white-space:nowrap;width:100%;"><a class="ctl00_Menu1_1" href="admin.aspx">Admin</a></td>
			</tr>
		</table></td>
	</tr>
</table><a id="ctl00_Menu1_SkipLink"></a>
                    <br />
                    
                    <input type="submit" name="ctl00$btnLogOut" value="Log Out" id="ctl00_btnLogOut" /></td>
                <td valign="top" style="text-align: left; height: 236px; width: 850px;">
                    
    <hr style="width: 782px" />
    &nbsp;<input name="ctl00$ContentPlaceHolder1$txtSearch" type="text" id="ctl00_ContentPlaceHolder1_txtSearch" style="font-family:Arial;" />
    <input type="submit" name="ctl00$ContentPlaceHolder1$btnSearch" value="Search" id="ctl00_ContentPlaceHolder1_btnSearch" style="font-family:Arial;" />
    <br />
    <br />
    <br />
        
        
    <div>
	<table cellspacing="0" rules="all" border="1" id="ctl00_ContentPlaceHolder1_GridView1" style="font-family:Arial;border-collapse:collapse;">
		<tr style="color:White;background-color:Blue;">
			<th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$ticketno')" style="color:White;">Ticket Number</a></th><th align="center" scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$lname')" style="color:White;">Last Name</a></th><th align="center" scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$company')" style="color:White;">Company</a></th><th align="center" scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$product')" style="color:White;">Product</a></th><th scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$date_entered')" style="color:White;">Date Entered</a></th><th align="center" scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$assigned')" style="color:White;">Assigned To</a></th><th align="center" scope="col"><a href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$GridView1','Sort$status')" style="color:White;">Status</a></th>
		</tr><tr>
			<td align="center"><a href="detail.aspx?ticketno=14">14</a></td><td align="center">JOHNSON</td><td>HOWARD JOHNSON, INC.</td><td>IADS Author</td><td align="center">10/17/2007</td><td>Gregg Geis</td><td>Approved</td>
		</tr><tr>
			<td align="center"><a href="detail.aspx?ticketno=8">8</a></td><td align="center">CLOWN</td><td>CLOWNS R US</td><td>IADS Configuration</td><td align="center">9/26/2007</td><td>&nbsp;</td><td>Completed</td>
		</tr>
	</table>
</div>

                </td>
            </tr>
        </table>
    </div>
    
<div>

	<input type="hidden" name="__EVENTVALIDATION" id="__EVENTVALIDATION" value="/wEWCwKG5Zy0BwKenYa6CgKnpt8nAve684YCAq/UyswHAou2p5sPArOz2YMEAteXm5oEAoLQ7r0GAqK4gcEKAsHEwYcFucysdusmjpdu/0xxuXhs26IkFT8=" />
</div>

<script type="text/javascript">
<!--
var ctl00_Menu1_Data = new Object();
ctl00_Menu1_Data.disappearAfter = 500;
ctl00_Menu1_Data.horizontalOffset = 0;
ctl00_Menu1_Data.verticalOffset = 0;
ctl00_Menu1_Data.hoverClass = 'ctl00_Menu1_4';
ctl00_Menu1_Data.hoverHyperLinkClass = 'ctl00_Menu1_3';
// -->
</script>
</form>
</body>
</html>
 
Actually I meant the HTML source view from Visual Studio, not the rendered page.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top