Click here to Skip to main content
15,909,466 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi, What I want to do is to have a gridview on my asp.net page that has checkboxes in the first column and below the grid there is to be a button. Now I databind the grid to a table which contains only three attributes namely ID, Name and Email. Now what I want to do is that when the button is clicked files should be created in a folder by the names of the corresponding checked checkboxes.
Here is what I have so far, but what the code does is that even if a single checkbox is checked it will loop through the gridview and make some files. Here is the code:
C#
foreach (GridViewRow row in GridView1.Rows)
           {
               // Access the CheckBox
               CheckBox cb = (CheckBox)row.FindControl("StatusUpdate");
               if (cb.Checked || cb != null)
               {
                   //int ID = Convert.ToInt32(GridView1.DataKeys[row.RowIndex].Value);
                   int index = row.RowIndex;
                   string name = GridView1.Rows[index].Cells[1].Text;
                   File.Create("D:\\"+name+".txt");
               }
               else
               {
               }
           }
           File.AppendAllText(@"E:\Try.text", "No box checked" + Environment.NewLine);
Posted
Comments
Dholakiya Ankit 15-Jul-13 7:51am    
you can have some javascript code on click of that folders will be created
09hadi 15-Jul-13 8:04am    
Aint there anyway to do it without javascript?
Dholakiya Ankit 15-Jul-13 8:13am    
yes i am adding solution which may help you

add onclick function like

<checkbox önclick="checkme(this)">
function checkme()
{
if(this.check==true)
{

//take one hiddenfield
hiddenfield.value=this.id;
}
}

and in hidden field on hiddenfield valuechanged="hidden_changed"

now in c#

protected void hidden_changed
{
string aa= hiddenfieldid.value
//here you get id of checkbox checked

}
and you will get files for checked checkbox only
this is logic only
 
Share this answer
 
v2
Comments
09hadi 16-Jul-13 3:13am    
Thanks for your help, the problem wasnt with the condition, but the postback launched by the button on click
Dholakiya Ankit 16-Jul-13 3:17am    
ok
nikhil-vartak 16-Jul-13 7:18am    
Did this solution solved your problem?
Dholakiya Ankit 16-Jul-13 7:44am    
YES I HAVE BUT I HAVE NO CODE TO SHOW YOU I HAVE LOGIC ONLY TRY THIS ONE WE WILL DO SOLUTION POST CODE THEN IF NOT SUCCEED
Files are created because your if condition evaluates to True always.

if (cb.Checked || cb != null)


This condition is true even if the checkbox is not checked because you have applied || (or) and the checkbox is found ie its not null.

Simply change || to && that will make sure your code creates file only if "checkbox is found and is checked too"!
 
Share this answer
 
Comments
09hadi 16-Jul-13 3:13am    
Thanx for your help but the problem is not with the condition it is with the postback
nikhil-vartak 16-Jul-13 7:19am    
Did you try the && operator!?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900