Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old July 15th, 2005, 02:39 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default Datagrid with Checkbox

I have a datagrid with a checkbox which displays the boolean result from a database. My code for the datagrid is as follows:

Code:
<asp:DataGrid id="dgQuiz" runat="server" OnEditCommand="dgQuiz_Edit" OnCancelCommand="dgQuiz_Cancel" OnUpdateCommand="dgQuiz_Update" AutoGenerateColumns="False" Width="560px">
                            <columns>
                                <asp:templatecolumn headerstyle-font-bold=True headerstyle-horizontalalign=Center></asp:templatecolumn>
                                <asp:BoundColumn Visible="False" DataField="IDP" SortExpression="id" HeaderText="IDP"></asp:BoundColumn>
                                <asp:templatecolumn headertext="Active">
                                    <itemtemplate>
                                        <asp:checkbox runat="server" enabled="True" name="Checkbox2" id="Checkbox2" checked = '<%# DataBinder.Eval(Container, "DataItem.Active") %>' ></asp:checkbox>
                                    </itemtemplate>
                                    <edititemtemplate>
                                        <asp:checkbox runat="server" name="Checkbox2" id="Checkbox1" checked = '<%# DataBinder.Eval(Container, "DataItem.Active") %>' ></asp:checkbox>
                                    </edititemtemplate>
                                </asp:templatecolumn>
                                <asp:HyperLinkColumn Target="_self" DataNavigateUrlField="IDP" DataNavigateUrlFormatString="QuizQuestions.aspx?TestIDP={0}" DataTextField="Name" HeaderText="Quiz"></asp:HyperLinkColumn>
                                <asp:HyperLinkColumn Target="_self" DataNavigateUrlField="IDP" DataNavigateUrlFormatString="PrintQuizAnswerKey.aspx?TestIDP={0}" DataTextField="Name" HeaderText="Answer Key"></asp:HyperLinkColumn>
                                <asp:HyperLinkColumn Target="_self" DataNavigateUrlField="IDP" DataNavigateUrlFormatString="PrintQuiz.aspx?TestIDP={0}" DataTextField="Name" HeaderText="Print"></asp:HyperLinkColumn>
                                <asp:EditCommandColumn EditText="Edit" CancelText="Cancel" UpdateText="Update" ItemStyle-Wrap="false" HeaderText="Edit Command Column" HeaderStyle-Wrap="false" />

                            </Columns>
                        </asp:DataGrid>
I want to allow the user to change the checkbox value and click update to save the change similar to how this page works: http://samples.gotdotnet.com/quickst...datagrid6.aspx
but I cant figure out how to do the code behind to determine the checkbox value and which row the user is working with...basically I need help with the "dgQuiz_Update" Sub...


-------------------------
Beware of programmers with screwdrivers...
__________________
-------------------------
Beware of programmers with screwdrivers...
 
Old July 15th, 2005, 02:49 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

In the edit command handler, you can get at the checkbox with something like this:

objCheckBox = CType(e.Item.FindControl("Checkbox1"), CheckBox)

Then you can access the objCheckBox properties to get the checked state.

Use the grid's DataKeyField property to set the field that holds the key for the table so you can access in the edit command handler with grid.DataKeys[e.Item.ItemIndex]. Then you'll have the record key to hit the database with an update.

-Peter
 
Old July 15th, 2005, 02:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 100
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to ~Bean~
Default

sweet...I was trying all kinds of things besides the FindControl...I was wrong in thinking that "e" was the Checkbox itself...

thats works perfectly...THANKS!

-------------------------
Beware of programmers with screwdrivers...





Similar Threads
Thread Thread Starter Forum Replies Last Post
CheckBox in Datagrid sumith ASP.NET 1.0 and 1.1 Professional 1 March 21st, 2007 06:25 AM
checkbox in datagrid asad_prog VB.NET 0 November 25th, 2006 02:41 PM
CheckBox in DataGrid Baby_programmer ASP.NET 1.x and 2.0 Application Design 2 March 11th, 2005 12:42 AM
CheckBox in DataGrid Baby_programmer ASP.NET 1.0 and 1.1 Basics 1 March 3rd, 2005 08:33 PM
checkbox in datagrid joekske VB How-To 1 June 3rd, 2004 02:31 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.