Link to home
Start Free TrialLog in
Avatar of raterus
raterusFlag for United States of America

asked on

DataGrid & ViewState & TemplateColumns

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
Avatar of AerosSaga
AerosSaga

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
Avatar of raterus

ASKER

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
are you simply asking why viewstate size increases in the second statement?

Avatar of raterus

ASKER

yes, and how to turn that off.  And why the viewstate doesn't increase using the first statement.
Avatar of raterus

ASKER

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
Avatar of raterus

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of AerosSaga
AerosSaga

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of raterus

ASKER

Yeah, but unfortunately I can't accept my answer and get the points..so here!
lol thank you raterus, I wish I would have had a better one for you.

Regards,

Aeros