?
Solved

DataGrid & ViewState & TemplateColumns

Posted on 2004-08-27
9
Medium Priority
?
446 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 2000 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

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…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

762 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