asp.net - Why is the DataItem property null for this DataGridItem here? -
problem
i have buttoncolumn
edit row defined this:
<asp:buttoncolumn datatextfield="job_code" buttontype="linkbutton" headertext="job code" commandname="edit"></asp:buttoncolumn>
and in onitemcommand
handler datagrid
have code:
if e.commandname = "edit" dim o listdatamodel = ctype(e.item.dataitem, listdatamodel) if o nothing exit sub end if ... end if
but e.item.dataitem
null
here.
i've looked through related questions , verified following:
- the
itemtype
ofe.item
setlistitemtype.item
, therefore should allowed housedataitem
. jives msdn documentation. - i leveraged data binding -see code section below.
- i have set
datakeyfield
attribute onasp:datagrid
this:datakeyfield="job_code"
.
data binding code (happens in search method)
using reader sqldatareader = cmd.executereader() dim list list(of listdatamodel) = new list(of listdatamodel) while reader.read() list.add(new listdatamodel { ... }) end while dgsearchresults.datasource = list dgsearchresults.databind() end using
now, search
method onserverclick
event handler input
button. flow user search results , click on 1 of command button edit row, therefore search
method not run when onitemcommand
handler fired.
alright, resolution ended bit convoluted. first ended having store search result set in session
, built dictionary
that:
dim cache dictionary(of string, listdatamodel) = new dictionary(of string, listdatamodel)
and added dictionary
when building listdatamodel
objects. then, in onitemcommand
handler ended having use reflection
this:
dim cache dictionary(of string, listdatamodel) = ctype(session("searchresults"), dictionary(of string, listdatamodel)) if cache nothing exit sub end if dim prop propertyinfo = e.commandsource.gettype().getproperty("text") if prop nothing exit sub end if dim o listdatamodel = cache(prop.getvalue(e.commandsource, nothing).tostring()) if o nothing exit sub end if
as can see first pulled "searchresults" out of session
, tried @ text
property of datagridlinkbutton
. had use reflection
because datagridlinkbutton
class isn't visible. finally, if found property, pulled off value.
though works, , hope solution by-product of else weird in application i'm maintaining, it's not wanted in end. pretty poor interface datagrid
-but asp.net 2.0 , it's old stuff!
Comments
Post a Comment