Solved

DataGrid & ViewState & TemplateColumns

Posted on 2004-08-27
9
409 Views
Last Modified: 2008-02-01
Hi,
I'm using TemplateColumns to cut down on the amount of viewstate my datagrid takes up.  I need my datagrid to be able to sort, so I can't completely disable the viewstate.

I've ran into a bit of a pickle though, which is really confusing me as to why it is doing this.  I need a hyperlink in one of the columns, so when you click on it, some javascript will run.  Here is my code within the datagrid.

--This template column takes up no additional viewstate
<asp:templatecolumn>
  <itemtemplate>
    <a href="#" onclick="someFunction('123456789);">Select</a>
  </itemtemplate>
</asp:templatecolumn>

--This template column takes up oogles of viewstate...The bound column is a value of 9 digits
<asp:templatecolumn>
  <itemtemplate>
    <a href="#" onclick="someFunction('<%# DataBinder.Eval(Container.DataItem, "myColumn"')">Select</a>
  </itemtemplate>
</asp:templatecolumn>

I don't understand this, I'm not using a webcontrol, so I don't see how the datagrid can persist this in the viewstate, anyone offer any help as how I can get this to function correctly, without taking up a ton of viewstate?

--Michael
0
Comment
Question by:raterus
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11914438
You could always write your own little sort routine and then rebind to free up the viewstate space, something like this:

 Private Sub dg_SortCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridSortCommandEventArgs) Handles dg.SortCommand
        Dim cnn As New SqlClient.SqlConnection(ConfigurationSettings.AppSettings("EmeraldConnStr"))
        Dim cmd As New SqlClient.SqlCommand
        Dim da As New SqlClient.SqlDataAdapter(cmd)
        Dim ds As New DataSet
        Dim dt As New DataTable
        Dim Query As String
        Dim SortString, SortOrder As String
        If Session("SortOrder") = "" Then
            SortOrder = "ASC"
        End If
        If Session("SortOrder") = "ASC" Then
            SortOrder = "DESC"
        End If
        If Session("SortOrder") = "DESC" Then
            SortOrder = "ASC"
        End If
        Query = "SELECT * FROM sschecter.JobNumbers JN ORDER BY JN." & e.SortExpression.ToString & " " & SortOrder
        Session("SortOrder") = SortOrder
        cmd.CommandText = Query
        cmd.Connection = cnn
        cnn.Open()
        da.SelectCommand = cmd
        da.Fill(dt)
        dg.DataSource = dt
        dg.DataKeyField = "JobNumber"
        dg.DataBind()
        cnn.Close()
        cmd.Dispose()
        cnn.Dispose()
        dg.Visible = True
    End Sub

Regards,

Aeros
0
 
LVL 33

Author Comment

by:raterus
ID: 11914555
This problem doesn't have to do with the columns being about to sort, the templatecolumns are the displayed data that is being sorted.  

I've got quite a nice little setup for sorting already, I've extended my datagrid class to provide the asc/desc feature while adding a purty little triangle in the header to indicate the direction/column the datagrid is currently sorted.  Then the Datagrid exposes a new property that tells me which way the user wants to sort, and I rebind the datagrid based on that when I hit a sortcommand.  Works incredibly great, but alas, I don't think it matters for this post.

--Michael
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11914595
are you simply asking why viewstate size increases in the second statement?

0
ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

 
LVL 33

Author Comment

by:raterus
ID: 11914660
yes, and how to turn that off.  And why the viewstate doesn't increase using the first statement.
0
 
LVL 33

Author Comment

by:raterus
ID: 11914697
Lemme guess, the LiteralControls the databinding syntax is creating... :-)  If that's it, no need to tell me how to turn it off, already on it
--Michael
0
 
LVL 33

Author Comment

by:raterus
ID: 11914711
actually I know a "way" to disable the viewstate in the literalcontrols (using ItemDataBound), but if you have a better way, I'm all ears
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11915266
well I guess you answered your own question;)
0
 
LVL 33

Author Comment

by:raterus
ID: 11915969
Yeah, but unfortunately I can't accept my answer and get the points..so here!
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11915985
lol thank you raterus, I wish I would have had a better one for you.

Regards,

Aeros
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question