Link to home
Start Free TrialLog in
Avatar of Sirdots
Sirdots

asked on

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

Avatar of digitalZo
digitalZo
Flag of India image

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

sorry a typo

inject*

if you have many checkboxes, you can iterate through them to check whether they are checked or not.
Avatar of Munawar Hussain
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

Avatar of Sirdots
Sirdots

ASKER

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>
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
Avatar of Sirdots

ASKER

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?

Avatar of Sirdots

ASKER

Thanks a lot for your response needo jee. I will be waiting.
ASKER CERTIFIED SOLUTION
Avatar of Munawar Hussain
Munawar Hussain
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Sirdots

ASKER

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#.
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
Avatar of Sirdots

ASKER

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.