Need help with checkbox command in gridview

I have a column of toggle checkboxes in a gridview. When I ‘check’ a box I want to run an immediate update sql command, then show the label at the bottom of the page. The delete imagebutton works perfectly, it deletes and the label shows; I essentially copied the code for the “Update” in my onrowcommand code behind. My Update command isn’t executing. Why not?

[ASCX]

<asp:GridView ID="gvApplicants" runat="server" DataSourceID="dsApplicants" EnablePersistedSelection="True" DataKeyNames="StudentID" OnRowCommand="gvApplicants_RowCommand"> <Columns> <asp:BoundField DataField="StudentID" HeaderText="StudentID" SortExpression="StudentID" /> <asp:TemplateField HeaderText="Contacted" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="center"> <ItemTemplate> <asp:CheckBox ID="cbContacted" AutoPostBack="True" CommandName="Update" OnCheckedChanged="cbContacted_onCheckChanged" runat="server" /> </ItemTemplate> </asp:TemplateField> <asp:ButtonField ButtonType="Image" CommandName="Delete" HeaderImageUrl="~/Portlets/CUS/ICS/Portlet.OrientationRoster/trashcan.gif" ImageUrl="~/UI/Common/Images/PortletImages/Icons/delete.png" /> </Columns> </asp:GridView> <asp:SqlDataSource ID="dsApplicants" runat="server" DeleteCommand="DELETE FROM CUS_Orient_applicants WHERE StudentID = @StudentID" SelectCommand="SELECT * FROM CUS_Orient_applicants WHERE SessionID = @sessID" UpdateCommand="UPDATE CUS_Orient_applicants SET Contacted = 'C' where StudentID = @StudentID"> <SelectParameters> <asp:ControlParameter ControlID="ddlOrientationSelection" Name="sessID" PropertyName="SelectedValue" /> </SelectParameters> <DeleteParameters> <asp:ControlParameter Name="StudentID" ControlID="gvApplicants" /> </DeleteParameters> <UpdateParameters> <asp:ControlParameter Name="StudentID" ControlID="gvApplicants" /> </UpdateParameters> </asp:SqlDataSource> <asp:Label ID="lblDeletedResults" runat="server" EnableViewState="false" Visible="false"></asp:Label> 

[.CS]

protected void gvApplicants_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "Update") { int nRowIndex = Int32.Parse(e.CommandArgument.ToString()); //gets the rows index Int32 StudentID = Convert.ToInt32(gvApplicants.DataKeys[nRowIndex].Value); //selects the studentid column from the defined index lblDeletedResults.Text += string.Format("You have contacted StudentID: {0}", StudentID); lblDeletedResults.Visible = true; } if (e.CommandName == "Delete") { int nRowIndex = Int32.Parse(e.CommandArgument.ToString()); //gets the rows index Int32 StudentID = Convert.ToInt32(gvApplicants.DataKeys[nRowIndex].Value); //selects the studentid column from the defined index lblDeletedResults.Text += string.Format("You have deleted StudentID: {0} from this session.", StudentID); lblDeletedResults.Visible = true; } } 

I’m just at a loss and my brain hurts.

Thanks!

submitted by /u/leijae
[link] [comments]

Leave a Reply