asp.net - pass ID value to OnCheckedChange event in code behind -
i have gridview has 1 checkbox each row of data.
when checkbox checked or unchecked, need update bit flag in database.
i'm trying use oncheckedchanged event.
it fire, getting null error has id.
my problem is, i'm not quite sure how needed id oncheckedchanged event code.
i need id update appropriate row in database.
i found few relavent questions on stackoverflow none of supplied answers helped me out.
thanks!
my gridview code:
<asp:gridview id="gvlisting" runat="server" autogeneratecolumns="false"> <columns> <asp:templatefield headertext="item ready"> <itemtemplate> <asp:checkbox id="isready" runat="server" autopostback="true" oncheckedchanged="isready_checkedchanged" checked='<%#isready(cint(eval("isready")))%>'/> </itemtemplate> </asp:templatefield> <asp:templatefield headertext="item name"> <itemtemplate> <asp:label id="itemname" runat="server" text='<%#cleanstring(eval("itemname").tostring())%>'></asp:label> </itemtemplate> </asp:templatefield> </columns> </asp:gridview>
code behind:
public sub isready_checkedchanged(byval sender object, byval e eventargs) dim box checkbox = directcast(sender, checkbox) if box.checked = true 'custom code executing sql update statement db.executenonquery(addisreadyflag, new sqlparameter("@itemid", me.id)) else db.executenonquery(removeisreadyflag, new sqlparameter("@itemid", me.id)) end if end sub
me.id
id
of page
since me
current page-instance.
you add template-field id:
<asp:templatefield headertext="item-id" visible="false"> <itemtemplate> <asp:label id="itemid" runat="server" text='<%# eval("itemid") %>'></asp:label> </itemtemplate> </asp:templatefield>
now can use namingcontainer
row , findcontrol
label:
public sub isready_checkedchanged(byval sender object, byval e eventargs) dim box checkbox = directcast(sender, checkbox) dim row = directcast(box.namingcontainer, gridviewrow) dim itemid = directcast(row.findcontrol("itemid"), label).text if box.checked = true 'custom code executing sql update statement db.executenonquery(addisreadyflag, new sqlparameter("@itemid", itemid)) else db.executenonquery(removeisreadyflag, new sqlparameter("@itemid", itemid)) end if end sub
Comments
Post a Comment