Elimination confirmation message before checking check boxes

I am reading a folder which has files and displaying
the files in a grid with checkboxes. users can check the
box to delete any files they want

I have a command button. If a user does not check a box
and clicks on the command button, My message is displayed
 "The checked file(s) will be deleted .. ok .. cancel

I do not want this. I want to return a message that will
inform the user to select files and not that prompt.

Here is my javascript doing this
------------------------------------
 
<script type="text/javascript">      
        function confirm_delete()
        {
            if (confirm("The checked file(s) will be deleted?")==true)
                  return true;
            else
                  return false;
        }
    </script>
 
 
Here is my page load event
------------------------------
 
protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            btndelete.Attributes.Add("onclick", "return confirm_delete();");
            if (!Page.IsPostBack)
            {
                RetrieveDirectoryInfo();
            }
        }
        catch (Exception ex)
        {
            lblError.Text = "Error encountered";
            Response.Write(ex.Message);
        }
    }
 
 
Here is my delete key event i.e the command button
---------------------------------------------------------
 
protected void btndelete_Click(object sender, EventArgs e)
      {
          try
          {
              for (int i = 0; i < dgFiles.Rows.Count; i++)
              {
                  GridViewRow row = dgFiles.Rows[i];
                  bool isChecked = ((CheckBox)row.FindControl("chkdel")).Checked;
                  if (isChecked)
                  {
                      File.Delete(dgFiles.Rows[i].Cells[0].Text);
                  }       
              }
 
              RetrieveDirectoryInfo();
          }
          catch (Exception ex)
          {
              lblError.Text = "Error encountered";
              Response.Write(ex.Message);
          }
      }

Open in new window

SirdotsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

digitalZoCommented:
you can add the if-else condition to check whether the checkboxes are checked. if they are not then injet the javascript alert to the button attribute prompting the user to make a selection. if they are, then you can use your normal code:


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
 
 
 
        Try
            If CheckBox1.Checked = False Then
                btndelete.attributes.add("onclick", "javascript:alert('Please select the files!');")
            Else
                btndelete.Attributes.Add("onclick", "return confirm_delete();")
            End If
 
            If Not Page.IsPostBack Then
                RetrieveDirectoryInfo()
            End If
        Catch ex As Exception
            lblError.Text = "Error encountered"
            Response.Write(ex.Message)
        End Try
   

Open in new window

0
digitalZoCommented:
sorry a typo

inject*

if you have many checkboxes, you can iterate through them to check whether they are checked or not.
0
Munawar HussainPrincipal Software EngineerCommented:
hi,

in your javascript function you may use "getElementsByTagName' to find all checkboxes.
then filter the checkboxes on the base of specific string part in the checkboxes names which will give you all checkboxes for gridview, iterate through to find if any of the checkboxes is checked or not and then display message accordingly.

Thanks

lets suppose I have named the checkboxes in datagrid like this.
chkMyGidCheckBox1,chkMyGidCheckBox2,chkMyGidCheckBox3,
 
now in JS function i will check it like this
<script type="text/javascript">      
        function confirm_delete()
        {
        var i=0;
        var IsAnyChecked=false;
        var myCheckBoxList=document.form1.getElementsByTagName("input");      
 
        for(;i<myCheckBoxList.length -1;i++)
        {
            if(myCheckBoxList[i].id.indexOf("chkMyGidCheckBox") <> -1)
            {
               if(myCheckBoxList[i].checked=true)
                {
                 isAnyChecked=true
                 break;
                }
            }
         }  
        if(isAnyChecked == true)
        {
            if (confirm("The checked file(s) will be deleted?")==true)
            {return true;}
            else
            {return false}
        }
        else
        {
        alert("No record is selected, please select one before delete");
        return false;
        }
   }    
</script>

Open in new window

0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

SirdotsAuthor Commented:
Thanks guys for your responses. needo jee, here is my html part below. I am using a gridview and it only has an id. I do not have separate names for the checkboxes. How do I now apply your javascript with this.
Again, if the checkboxes are not checked I dont want to see the message if the user clicks the command button. I will prefer an alert that says please check boxes before you can delete.

Thanks again.


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript">      
        function confirm_delete()
        {
            if (confirm("The checked file(s) will be deleted?")==true)
                  return true;
            else
                  return false;
        }
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div style="height:292px;width:500px;overflow:auto">
    <div>
       
        <asp:GridView ID="dgFiles" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="226px">
        <Columns>
            <asp:BoundField DataField="FullName" HeaderText="File Full Path" />
           
            <asp:TemplateField HeaderText="Select File(s)">
                <EditItemTemplate>
                    <asp:CheckBox ID="Chkdel" runat="server" />
                </EditItemTemplate>
                <ItemTemplate>  
                    <asp:CheckBox ID="Chkdel" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
           
        </Columns>    
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <div>&nbsp</div>
        <div><asp:Button ID="btndelete" Text="Delete File(s)" runat="server" OnClick="btndelete_Click" /></div>
        <div><asp:Label ID="lblError" runat="server" Height="39px" Width="285px" Font-Bold="True" ForeColor="Red"></asp:Label></div>
        </div>
        </div>
    </form>
</body>
</html>
0
Munawar HussainPrincipal Software EngineerCommented:
Dear fellow, I know you have one checkbox but when you bind the gridview with datasournce and view in browsers .. for each row you will have a checkbox which Griview will give names after your actual name.

Further I will spend time to give you working sample as per your actual code
thanks
0
SirdotsAuthor Commented:
Thanks digitalZo: I am using C# asp.net. I guess your code is vb.net. The id of my checkbox is Chkdel. I was unable to get to the property in page load. What do I do?

0
SirdotsAuthor Commented:
Thanks a lot for your response needo jee. I will be waiting.
0
Munawar HussainPrincipal Software EngineerCommented:
hi
this is the complete working code for you case.


VB FILES
Imports System.Data
Imports System.Data.SqlClient
Partial Class Default2
    Inherits System.Web.UI.Page   
 
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            btndelete.Attributes.Add("onclick", "confirmdelete();")
            Dim dt As New DataTable()
            dt.Columns.Add("FullName")
            dt.Rows.Add("Munawar1")
            dt.Rows.Add("Munawar2")
            dt.Rows.Add("Munawar3")
            dt.Rows.Add("Munawar4")
            dt.Rows.Add("Munawar5")
            dt.Rows.Add("Munawar6")
            dt.Rows.Add("Munawar7")
            dt.Rows.Add("Munawar8")
            dgFiles.DataSource = dt
            dgFiles.DataBind()
        End If
    End Sub
 
    Protected Sub btndelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btndelete.Click
        Dim a As String = "mm"
    End Sub
End Class
 
 
ASPX PAGE
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="Default2" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
    <script type="text/javascript" language ="javascript">      
        function confirmdelete()
        {
        var i=0;      
        var myCheckBoxList=document.form1.getElementsByTagName("input");      
 
        for(i=0;i<myCheckBoxList.length-1;i++)
        {
            if(myCheckBoxList[i].id.indexOf("Chkdel") != -1)
            {
               if(myCheckBoxList[i].checked==true)
                {                 
                if (confirm("The checked file(s) will be deleted?")==true)
                {return true;}
                else
                {return false}
                break;
                }
            }
         }         
    
        }
    </script>
 
</head>
<body>
    <form id="form1" runat="server">
     
    <div style="height:292px;width:500px;overflow:auto">
    <div>
        
        <asp:GridView ID="dgFiles" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="226px">
        <Columns>
            <asp:BoundField DataField="FullName" HeaderText="File Full Path" />
            
            <asp:TemplateField HeaderText="Select File(s)">
                <EditItemTemplate>
                    &nbsp;
                </EditItemTemplate>
                <ItemTemplate>  
                    <asp:CheckBox ID="Chkdel" runat="server" />
                </ItemTemplate>
            </asp:TemplateField>
            
        </Columns>     
            <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
            <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
            <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
            <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
            <AlternatingRowStyle BackColor="White" />
        </asp:GridView>
        <div>&nbsp</div>
        <div><asp:Button ID="btndelete" Text="Delete File(s)" runat="server"   /></div>
        <div><asp:Label ID="lblError" runat="server" Height="39px" Width="285px" Font-Bold="True" ForeColor="Red"></asp:Label></div>
        </div>
        </div>
   </form>
</body>
</html>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
SirdotsAuthor Commented:
Thanks a lot. I appreciate all your help. I guess you forgot that I use C#. I dont use vb.net. I might need to do this using C#.
0
Munawar HussainPrincipal Software EngineerCommented:
nothing is with VB.Net or C# .. most of the code you need is in ASPX file that is same of both VB.Net and C# ..
only  a little i did in form load . just a sample (you need ur own code to fill the grid)

if (!IsPostBack)
        {
           
            btndelete.Attributes.Add("onclick", "confirmdelete();");
            DataTable  dt =new DataTable();
            dt.Columns.Add("FullName");
            dt.Rows.Add("Munawar1");
            dt.Rows.Add("Munawar2");
            dt.Rows.Add("Munawar3");
            dt.Rows.Add("Munawar4");
            dt.Rows.Add("Munawar5");
            dt.Rows.Add("Munawar6");
            dt.Rows.Add("Munawar7");
            dt.Rows.Add("Munawar8");
            dgFiles.DataSource = dt;
            dgFiles.DataBind();
}


only this is what i written in vb.net now is in C#

thanks
0
SirdotsAuthor Commented:
Thanks for your help Needo Jee.  This is helpful. Although the page still performs a post back if you click on the button but no message comes up. I will work on other parts.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.