Asked by:
show a message box if gridview checkbox doesn't select in asp.net

Question
-
User-1092307275 posted
I have a <g class="gr_ gr_32 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="32" data-gr-id="32">gridview</g> that has a checkbox in it. I want to delete the rows of <g class="gr_ gr_139 gr-alert gr_spell gr_inline_cards gr_run_anim ContextualSpelling" id="139" data-gr-id="139">gridview</g> that checkbox of them was selected.and if the user doesn't check any checkbox the error message shown to the user. and before deleting the data, Ask the user if they are sure of the operation?
foreach (GridViewRow gvr in GridView1.Rows) { if (((CheckBox)gvr.FindControl("chkDelete")).Checked == true) { Button2.Attributes.Add("onclick", "javascript:return del()"); //delet the value of selected row from EDU_CourseSessionPlan table SqlDataReader dr2; con.Open(); sqlcom.Connection = con; sqlcom.CommandText = "delete from EDU_CourseSessionPlan where CourseId='" + Int32.Parse(LblLessonId2.Text) + "'"; dr2 = sqlcom.ExecuteReader(CommandBehavior.SingleRow); if (dr2.Read()) { validate1 = true; } con.Close(); //delet the value of selected row from EDU_EDU_Course table SqlDataReader dr3; con.Open(); sqlcom.Connection = con; sqlcom.CommandText = "delete from EDU_Course where Id='" + Int32.Parse(LblLessonId2.Text) + "'"; dr3 = sqlcom.ExecuteReader(CommandBehavior.SingleRow); if (dr3.Read()) { validate1 = true; } con.Close(); string myStringVariable = "Your information has been successfully deleted"; ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + myStringVariable + "');", true); ///// refresh the data of Gridview1 Response.Redirect("ShowCourse.aspx?Value1=" + LblSubjectStudy.Text); } else { ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('please checked the rows that you want to delet');", true); } }
<script type="text/javascript"> function del() { var c = confirm("Are you sure you want to remove the selected field?"); if (c) { return true; } else { return false; } } </script>
<asp:GridView ID="GridView2" runat="server" EmptyDataText="هیچ واحدی تعریف نشده است" DataKeyNames="Code" AutoGenerateColumns="False" Width="932px" BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="3" Font-Names="Tahoma" OnSelectedIndexChanged="GridView2_SelectedIndexChanged" OnDataBound="GridView2_DataBound"> <Columns> <asp:BoundField DataField="TRoom" HeaderText="نام اتاق" HeaderStyle-CssClass="header-center" /> <asp:BoundField DataField="DayWeek" HeaderText="روز هفته" HeaderStyle-CssClass="header-center" /> <asp:BoundField DataField="ET" HeaderText="ساعت پایان کلاس" HeaderStyle-CssClass="header-center" /> <asp:BoundField DataField="ST" HeaderText="ساعت شروع کلاس" HeaderStyle-CssClass="header-center"/> <asp:BoundField DataField="LNProfessor" HeaderText="نام خانوادگی استاد" HeaderStyle-CssClass="header-center" /> <asp:BoundField DataField="NProfessor" HeaderText="نام استاد" HeaderStyle-CssClass="header-center" /> <asp:BoundField DataField="TLesson" HeaderText="نام درس" HeaderStyle-CssClass="header-center"/> <asp:BoundField DataField="Code" HeaderText="کد درس" HeaderStyle-CssClass="header-center"/> <asp:TemplateField HeaderText="انتخاب"> <ItemTemplate> <asp:CheckBox ID="chkDelete" runat="server" Checked='<%# Convert.ToBoolean(Eval("IsChecked").ToString()) %>' AutoPostBack="true" /> </ItemTemplate> </asp:TemplateField> </Columns> <FooterStyle BackColor="White" ForeColor="#000066" /> <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" /> <PagerStyle BackColor="White" ForeColor="#000066" HorizontalAlign="Left" /> <RowStyle ForeColor="#000066" /> <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" /> <SortedAscendingCellStyle BackColor="#F1F1F1" /> <SortedAscendingHeaderStyle BackColor="#007DBB" /> <SortedDescendingCellStyle BackColor="#CAC9C9" /> <SortedDescendingHeaderStyle BackColor="#00547E" /> </asp:GridView>
but when I run my program I'm having this problem:
Sunday, January 6, 2019 11:19 AM
All replies
-
User-943250815 posted
To get a control on gridview, you have to first get Row then find control on Row.Cells.
Bellow a short foreach versionforeach (GridViewRow zRow in grdNFE.Rows) { if ((zRow.RowType == DataControlRowType.DataRow)) { zChkBoxItem = ((CheckBox)(zRow.Cells[0].FindControl("chkItem"))); if (zChkBoxItem.Checked) { // Do what you need here } } }
Sunday, January 6, 2019 1:40 PM -
User-893317190 posted
Hi dorsa2,
GridView 's row has a property named RowType, which shows its type(Header,Foot,DataRow,) .
Your checkbox only exists in row of Type DataRow ,so you should choose datarow first(In header row , there is no checkbox, only header of your gridview)
You could write your code as follows.
foreach (GridViewRow row in GridView1.Rows) { if (row.RowType == DataControlRowType.DataRow) { //write your code } }
Best regards,
Ackerly Xu
Monday, January 7, 2019 2:30 AM -
User-1092307275 posted
Thx Dear jzero for your guidance,
what is zChkBoxItem ?!
Monday, January 7, 2019 11:44 AM -
User-943250815 posted
Sorry, I used a vb > c# converter
zChkBoxItem is just a variable to store your checkbox control, so I have all checkbox properties on hand including "Checked"
Monday, January 7, 2019 12:13 PM