473,397 Members | 2,099 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Asp.net viewstate query

Apologies if this is in the wrong group, but I need some help. I have been
developing with a DataGrid and OleDb and everything is fine, except that when
I want to sort columns (which is fine ordinarily) it forgets the sorting
information when I tell the grid that I want to edit.

I am using a form level variable to hold the ordering part of the SQL
statement, but it doesn't use it when I click (and run) the grid editing
procedure - I really can't see why it will not remember it (it is probably
just me being thick though!).

It's anoying, everything is fine except that if I wanted to sort the grid,
then edit an item, it resorts the grid back to it's default state!

My code is below, and any help would be great! Thanks, James.

<%@ Page Language="VB" %>
<%@ Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral,
PublicKeyToken=6f763c9966660626" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

' Insert page code here
'
dim dbconn as new OleDbConnection
dim dbcommand as new OleDbCommand
Dim gSortExpr as string

sub Page_Load
IF NOT Page.IsPostback then
'Using textbox1 for debugging.
textbox1.text = "NOT Postback: " & gSortExpr
bindGrid(gSortExpr)
else
textbox1.text = "POSTBACK:" & gSortExpr
end if
end sub

sub bindGrid()
dbconn.connectionstring =
("Provider=Microsoft.Jet.OLEDB.4.0;data source=c:\test.mdb")
dbconn.Open()
dbcommand.commandtext = "SELECT * FROM tblPerson " & gSortExpr
dbcommand.connection = dbconn

dim dbreader as OleDbDataReader = dbcommand.executereader()

Try

dtgPerson.DataSource = dbReader
dtgPerson.DataBind()

Finally
' always call Close when done reading.
dbReader.Close()
dbconn.Close()

End Try

lblStatus.text = lExpr

end sub

sub expandPersonDetails(sender as Object, e as DataGridCommandEventArgs)
'runs when Details link is clicked
end sub

sub runSort(sender as Object, e as DataGridSortCommandEventArgs)
'runs when the grid is sorted.
gSortExpr = e.SortExpression
bindGrid()
end sub

sub dtgPerson_Edit(sender As Object, e As DataGridCommandEventArgs)

dtgPerson.EditItemIndex = e.Item.ItemIndex
bindGrid()
'lblStatus.text = "Edit Mode On"
'runs when edit button is clicked
end sub

sub dtgPerson_Update(sender As Object, e As DataGridCommandEventArgs)

Dim strid as integer = e.Item.Cells(1).Text
Dim strfName as String = CType(e.Item.Cells(2).Controls(0),
TextBox).Text
Dim strsName as String = CType(e.Item.Cells(3).Controls(0),
TextBox).Text

dbconn.connectionstring = ("Provider=Microsoft.Jet.OLEDB.4.0;data
source=c:\test.mdb")
dbconn.Open()
dbcommand.commandtext = "UPDATE tblPerson SET PersonFname = '" &
strfName & "', PersonSname = '" & strsName & "' WHERE PersonID = " & strid
dbcommand.connection = dbconn

try
dbcommand.executenonquery()
finally
dbconn.Close()
End Try

dtgPerson.EditItemIndex = -1
BindGrid()

lblStatus.text = "Data Updated. Data View Mode On"
end sub
sub dtgPerson_Cancel(sender As Object, e As DataGridCommandEventArgs)

dtgPerson.EditItemIndex = -1
bindGrid()

'runs when edit button is clicked
end sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
<asp:DataGrid id="dtgPerson" runat="server"
OnCancelCommand="dtgPerson_Cancel" OnUpdateCommand="dtgPerson_Update"
OnEditCommand="dtgPerson_Edit" OnSortCommand="runSort" AllowSorting="True"
OnItemCommand="expandPersonDetails" EditItemStyle-BackColor="#00ffff"
Backcolor="#eeeeee" AutoGenerateColumns="False">
<HeaderStyle backcolor="Black" forecolor="White"
font-bold="True" horizontalalign="Center" />
<AlternatingItemStyle backcolor="White" />
<Columns>
<asp:EditCommandColumn EditText="Edit Info"
ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" />
<asp:BoundColumn DataField="PersonID" HeaderText="Person
ID" ReadOnly="True" SortExpression="ORDER BY PERSONID ASC" />
<asp:BoundColumn DataField="PersonFname"
HeaderText="Given Name" SortExpression="ORDER BY PersonFname ASC" />
<asp:BoundColumn DataField="PersonSname"
HeaderText="Family Name" SortExpression="ORDER BY PersonSname ASC" />
<asp:ButtonColumn Text="Details" HeaderText="FAQ
Details" />
</Columns>
</asp:DataGrid>
</p>
<p>
<asp:Label id="lblStatus" runat="server"></asp:Label>
</p>
<p>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox>
</p>
</form>
</body>
</html>
Dec 11 '05 #1
0 864

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: John Kirksey | last post by:
I have a page that uses an in-place editable DataGrid that supports sorting and paging. EnableViewState is turned ON. At the top of the page are several search fields that allow the user to filter...
1
by: Bigal | last post by:
If I want to create a ListBox filled by a database query, it is not recommended to enable the viewstate of the form, because it does not make sense to reconstruct the whole and costs performance....
4
by: Nick Weg | last post by:
Hello We have a web application developed in .NET using VB.NET. We get the error below every now and then and can't figure out how to correct it. The viewstate this error refers to is pretty...
6
by: OHM | last post by:
Hi, I'n new to ASP.NET and I have a question maybe someone could answer for me. I realise that viewstate holds information about the state of the controls such as the values of text boxes etc....
1
by: ronniek | last post by:
Hi, I have a web form which contain several ASCXs inside I use the ViewState to keep some data between states one ASCX is a menu bar which provides a menu selection then the Page itself need...
3
by: Hadar | last post by:
Hi, I'm still a little bit confused with how managing a webcontrol state over postbacks... Should I use the form's post values to populate the control on the server side when possible, or...
1
by: Frank | last post by:
Hi all, Assuming datagrid's viewstate is enabled, then upon postback 1. its data will be populated automatically if AutoGenerateColumns is true 2. no data is populated if AutoGenerateColumns...
6
by: clsmith66 | last post by:
Is it possible to store the same information about a control that would be saved in the ViewState in a Session state? I have a page with three treeview controls and if I enable the view state for...
12
by: Alan Silver | last post by:
Hello, I have a page that gets passed an ID in the query string. It then uses this ID to pull info out of a database and populate controls on the page. When the page is posted back, the query...
9
by: =?Utf-8?B?TUNN?= | last post by:
I'm sure the answer to my question varies depending on the situation, but I am looking for a general "best practice". If I have an asp.net application and I load certain data from a database,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.