[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Elimination confirmation message before checking check boxes

Posted on 2008-02-05
11
Medium Priority
?
429 Views
Last Modified: 2012-06-22
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

0
Comment
Question by:Sirdots
  • 5
  • 4
  • 2
11 Comments
 
LVL 10

Expert Comment

by:digitalZo
ID: 20829673
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
 
LVL 10

Expert Comment

by:digitalZo
ID: 20829681
sorry a typo

inject*

if you have many checkboxes, you can iterate through them to check whether they are checked or not.
0
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20830102
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
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 

Author Comment

by:Sirdots
ID: 20832141
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
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20832242
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
 

Author Comment

by:Sirdots
ID: 20832244
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
 

Author Comment

by:Sirdots
ID: 20832365
Thanks a lot for your response needo jee. I will be waiting.
0
 
LVL 12

Accepted Solution

by:
Munawar Hussain earned 2000 total points
ID: 20835905
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
 

Author Comment

by:Sirdots
ID: 20836188
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
 
LVL 12

Expert Comment

by:Munawar Hussain
ID: 20836264
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
 

Author Comment

by:Sirdots
ID: 20870499
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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
Stellar Phoenix SQL Database Repair software easily fixes the suspect mode issue of SQL Server database. It is a simple process to bring the database from suspect mode to normal mode. Check out the video and fix the SQL database suspect mode problem.
Suggested Courses

591 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question