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.
<% [at] Page Language="VB" %>
<% [at] Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI"
Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral,
PublicKeyToken=6f763c9966660626" %>
<% [at] 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>
Re: ASP.NET Viewstate query
James Vickers wrote:
> Apologies if this is in the wrong group,
It is the wrong group, but no apologies are needed. Here is my canned reply:
There was no way for you to know it, but this is a classic asp newsgroup.
While you may be lucky enough to find a dotnet-knowledgeable person here who
can answer your question, you can eliminate the luck factor by posting your
question to a group where those dotnet-knowledgeable people hang out. I
suggest microsoft.public.dotnet.framework.aspnet.
--
Microsoft MVP - ASP/ASP.NET
Please reply to the newsgroup. This email account is my spam trap so I
don't check it very often. If you must reply off-line, then remove the
"NO SPAM"
Re: ASP.NET Viewstate query
Bob,
Thank you for reply - you are correct, there is no way - maybe a name of
classic.asp would be more suitable these days of this group?
James.
PS: I see you are a MVP in Asp/Asp.Net - can you see what the problem is?
"Bob Barrows [MVP]" wrote:
> James Vickers wrote:
> > Apologies if this is in the wrong group,
>
> It is the wrong group, but no apologies are needed. Here is my canned reply:
>
> There was no way for you to know it, but this is a classic asp newsgroup.
> While you may be lucky enough to find a dotnet-knowledgeable person here who
> can answer your question, you can eliminate the luck factor by posting your
> question to a group where those dotnet-knowledgeable people hang out. I
> suggest microsoft.public.dotnet.framework.aspnet.
>
>
> --
> Microsoft MVP - ASP/ASP.NET
> Please reply to the newsgroup. This email account is my spam trap so I
> don't check it very often. If you must reply off-line, then remove the
> "NO SPAM"
>
>
>
Re: ASP.NET Viewstate query
We've been agitating for that for years. It's not that simple to change the
name of a newsgroup.
James Vickers wrote:
> Bob,
>
> Thank you for reply - you are correct, there is no way - maybe a name
> of classic.asp would be more suitable these days of this group?
>
> James.
>
> PS: I see you are a MVP in Asp/Asp.Net - can you see what the problem
> is?
>
> "Bob Barrows [MVP]" wrote:
>
>> James Vickers wrote:
>>> Apologies if this is in the wrong group,
>>
>> It is the wrong group, but no apologies are needed. Here is my
>> canned reply:
>>
>> There was no way for you to know it, but this is a classic asp
>> newsgroup. While you may be lucky enough to find a
>> dotnet-knowledgeable person here who can answer your question, you
>> can eliminate the luck factor by posting your question to a group
>> where those dotnet-knowledgeable people hang out. I suggest
>> microsoft.public.dotnet.framework.aspnet.
>>
>>
>> --
>> Microsoft MVP - ASP/ASP.NET
>> Please reply to the newsgroup. This email account is my spam trap so
>> I don't check it very often. If you must reply off-line, then remove
>> the "NO SPAM"
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Re: ASP.NET Viewstate query
James Vickers wrote:
> Bob,
>
> Thank you for reply - you are correct, there is no way - maybe a name
> of classic.asp would be more suitable these days of this group?
>
> James.
>
> PS: I see you are a MVP in Asp/Asp.Net - can you see what the problem
> is?
No, I would have answered if I had. I'm a relative newcomer where .Net is
concerned. Unfortunately, MS did not create a separate ASP.Net MVP group
when they released it.
Bob
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
Re: ASP.NET Viewstate query
The gSortExpr is a local variable and is nowhere persisted in your code from
post to post. Is textbox1.text showing its value or not?
There are many ways to store it, the most simple of it would be to store in
a hidden field that will get posted back with the form. (Don't forget to
encode it with HTML encode if necessary; or at least make sure that the
string doesn't contains isolated double quotes.)
Also, using the trace.axd function would be easier than using label for
debugging your application.
--
Sylvain Lafontaine, ing.
MVP - Technologies Virtual-PC
E-mail: http://cerbermail.com/?QugbLEWINF
"James Vickers" <JamesVickers [at] discussions.microsoft.com> wrote in message
news:3D9052D4-26DB-43F3-A3DC-63D727493DF8 [at] microsoft.com...
> 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.
>
> <% [at] Page Language="VB" %>
> <% [at] Register TagPrefix="wmx" Namespace="Microsoft.Matrix.Framework.Web.UI"
> Assembly="Microsoft.Matrix.Framework, Version=0.6.0.0, Culture=neutral,
> PublicKeyToken=6f763c9966660626" %>
> <% [at] 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>
>