Solved

DataGrid & ViewState & TemplateColumns

Posted on 2004-08-27
9
376 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
One of the pain points with developing AJAX, JavaScript, JQuery, and other client-side behaviors is that JavaScript doesn’t allow for cross domain request for pulling content. For example, JavaScript code on www.johnchapman.name could not pull conte…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

707 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

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now