DataGrid Viewstate

K

kpg

Hola,

I have a datagrid with button columns. I bind the data on
page load, and the table is read only, so I don't need the
datagrid viewstate enabled. When I turn it off however, I
no longer get the button click events. I know events are
passed in the viewstate, but I don't need the data saved in
the viewstate, it adds a considerable amount of data to the
page.

Can I tunn off the datagrid viewstate and still respond to
button column events?

kpg
 
K

kpg

As Winista once said in microsoft.public.dotnet.framework.aspnet
As long as you can provide a mechanism for the DG state to be restored
by PageLoad, the event will fire. Without viewstate DG can't restore
its state.


http://www.netomatix.com


OK, but when I click the button on the datagrid, I get a postback, but
neither the DataGrid1_ItemCommand nor DataGrid1_SelectedIndexChanged
events get called.

All I need to know from the page is which column the user clicked
on, but this info seems to be lost without viewstate enabled.
 
W

Winista

Exactly... if DataGrid state is not resored or for that matter page's state
is not restored, framework does not know where to send the message. You can
use approach where you can set custom IDs or string in __EVENTTARGET and
__EVENTARGS hideen variables from client side when click happens and then on
server you can look for these values to decide from where the event
originated.
 
K

kpg

As Winista once said in microsoft.public.dotnet.framework.aspnet


Works great!


In the DataGrid1_ItemDataBound event I added (see ref below):


If e.Item.ItemType <> ListItemType.Header And _
e.Item.ItemType <> ListItemType.Footer Then

Dim myButton As Button = e.Item.Cells(0).Controls(0)

myButton.Attributes("onclick") = _
"javascript: document.getElementById ('__EVENTTARGET').value=" & _
DataBinder.Eval(e.Item.DataItem, "UserID") & ";"

End If

and in the Page_Load (see ref below):

Dim strTest As String = Page.Request.Params("__EVENTTARGET")


Ref:

http://aspnet.4guysfromrolla.com/articles/090402-1.2.aspx
http://www.eggheadcafe.com/articles/20050609.asp


Thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top