Click here to Skip to main content
15,904,024 members
Please Sign up or sign in to vote.
1.33/5 (3 votes)
See more:
I have a gridview and one of the columns (checkboxes)
I want to print the items I checked and enable the checkallbox,but my code dose not work
this is my code:
C#
int CheckedCount = 0;
            int tripNum = -1;
            int BusNumber = 0;
            string MyDate = "";
            string MyTime = "";
            foreach (GridViewRow R in GridView1.Rows)
            {
                CheckBox C = R.FindControl("CheckBox1") as CheckBox;
                if (C != null)
                {
                    if (C.Checked)
                    {
                        CheckedCount++;
                        tripNum = int.Parse(R.Cells[1].Text);
                        BusNumber = int.Parse(R.Cells[6].Text);
                        MyDate = R.Cells[4].Text;
                        MyTime = R.Cells[5].Text;
                        if (CheckedCount > 1)
                        {
                            Console.Write("Only one trip can be selected.", Response);
                            return;
                        }
                    }
                }


other thing I want to know , What is the good palce [no, I cannot fix all those typo anymore! — SA] to put this code ,Is it in an event of (gridview) or in an event of (button)

Thanks for all
Posted
Updated 10-Apr-11 16:21pm
v3
Comments
dedoooo 10-Apr-11 7:01am    
sorry, this code (<pre><pre lang="cs">) is not related of my code and I do not know how can I am editing my qustion
How can I upload my code?
OriginalGriff 10-Apr-11 7:19am    
Question editing is currently broken: the site owners have been notified, and will fix it as soon as they can - all you actually need to do when editing works is disable the "Ignore HTML..." option at the bottom. That will remove the pre tags and format your code better!
Sergey Alexandrovich Kryukov 10-Apr-11 22:23pm    
Here is how: uncheck "Ignore HTML", select radio button "allow HTML", replace angular brackets with HTML entities < >, escape everything else to be escaped.
--SA
dedoooo 10-Apr-11 7:14am    
protected void Page_Load(object sender, EventArgs e)
{

int CheckedCount = 0;
int tripNum = -1;
int BusNumber = 0;
string MyDate = "";
string MyTime = "";
foreach (GridViewRow R in GridView1.Rows)
{
CheckBox C = R.FindControl("CheckBox1") as CheckBox;
if (C != null)
{
if (C.Checked)
{
CheckedCount++;
tripNum = int.Parse(R.Cells[1].Text);
BusNumber = int.Parse(R.Cells[6].Text);
MyDate = R.Cells[4].Text;
MyTime = R.Cells[5].Text;
if (CheckedCount > 1)
{
Console.Write("Only one trip can be selected.", Response);
return;
}
}
}
}
OriginalGriff 10-Apr-11 7:22am    
When you say "it doesn't work", what do you mean? What is it doing that it shouldn't, or not doing that it should? Is there an error message?

1 solution

The best place would be in the browser - something that may be simplified using jQuery.
jQuery gridview plugins[^]

Web applications split their execution/logic between browser (like FireFox or IE) and web server (like IIS).

To make anything happen server side the page has to perform some sort of communication with the server (like a postback, or calling a web service). So something (like a button) in the browser has to trigger that action.

Update
Work your way through:
http://msdn.microsoft.com/en-us/library/ms229601.aspx[^]
Then go through:
http://www.asp.net/web-forms[^]

This provides a thorough grounding on the basics of .Net development and asp.net.

You might also benefit from reading:
The Data Model Resource Book: A Library of Universal Data Models for All Enterprises, Revised Edition, Volume 1[^]

Good luck :)
Regards
Espen Harlinn
 
Share this answer
 
v3
Comments
dedoooo 10-Apr-11 7:40am    
Because Iam new in c# , I do n't know for e.g the code of checkbox where can I put it .Now, the code not work with me and I think I put it in wrong place .


I put now in (Page_Load) event is this true?
Espen Harlinn 10-Apr-11 7:55am    
Try creating a Windows Forms based application to begin with - the application model is simpler and allows you to consentrate on taking your first steps when it comes to c# and .Net.
Sandeep Mewara 10-Apr-11 11:56am    
My 5++!
Espen Harlinn 10-Apr-11 12:02pm    
Thanks Sandeep!
Sergey Alexandrovich Kryukov 10-Apr-11 22:24pm    
Agree, my 5.
--SA

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