Geeks With Blogs

@azamsharp
  • azamsharp The WWDC app says that there are new videos but there is nothing under videos!!! about 7 days ago
  • azamsharp Can I post my opinion on iOS 7 Beta or is it under NDA or something? about 7 days ago
  • azamsharp iOS 7 BETA installed successfully! about 7 days ago
  • azamsharp iOS 7 BETA installed! Now restoring! I am on NET10 which uses AT&T network. Hopefully 3G will still work! Lets c.. about 7 days ago
  • azamsharp The missing of button borders in Xcode 5 makes it hard to determine the clickable area. about 8 days ago
  • azamsharp I might wait for iOS to be released before I can put it on my device! Don't want a bricked or unstable device! about 8 days ago
  • azamsharp Android has many features that no one uses, number of versions that no one upgrades to and thousands of apps that no one pays for. about 8 days ago
  • azamsharp Maybe it is just me but the new iOS 7 UI looks like Windows Phone 8 UI! #maybeIamCrazy about 8 days ago
  • azamsharp Anyone using H20 network upgraded to iOS 7 BETA Keep me updated if you face any problems. about 8 days ago
  • azamsharp @merowing_ You already downloaded it! I cannot even load the developer's website! :( about 8 days ago

AzamSharp Some day I will know everything. I hope that day never comes.

Sometimes we need to have checkboxes in the GridView control and we want that as soon as the Checkbox is clicked/checked the postback happens and we get the value from the first row in the GridView control.

This can be easily done by using the following code:

<asp:GridView ID="GridView1" runat="server"
 AutoGenerateColumns="False" OnRowCommand="GridView1_RowCommand">
            <Columns>
                <asp:BoundField DataField="CategoryID"
                HeaderText="CategoryID" />
                <asp:BoundField DataField="CategoryName"
                HeaderText="CategoryName" />
                <asp:TemplateField HeaderText="Select">
                    <ItemTemplate>
                        <asp:CheckBox ID="CheckBox1"
                        AutoPostBack="true" runat="server"
                        OnCheckedChanged="CheckBox1_CheckedChanged1" />
                        
                    </ItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

And the code behind:

 protected void CheckBox1_CheckedChanged1(object sender, EventArgs e)
    {
        CheckBox checkbox = (CheckBox)sender;
        GridViewRow row = (GridViewRow) checkbox.NamingContainer;
        Response.Write(row.Cells[0].Text); 
    }

Ohh by the way when you click a different checkbox it will show you the new value but does not clear the old checkbox. You can make a simple method "ClearCheckboxSelection" which clears all the checkboxes.

Hope you find this example useful :)

powered by IMHO 1.3

Posted on Tuesday, January 10, 2006 8:45 AM | Back to top


Comments on this post: Gettting Column Value Using Checkbox in GridView Autopostback = true

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
finally a code i can use!!!
Simple and useful!

Thanks!
Left by Alberto on Jul 07, 2006 5:21 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Fantastic!!!!!!! Thanks!!!!!!!1
Left by Daniel on Aug 01, 2006 12:59 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Ya good one
Thanks dude...

Ciao,
Left by Jayender on Aug 08, 2006 6:04 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Really Gr8 Example..
Keep it up...
Left by Raj Vaghela on Oct 20, 2006 9:54 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
thank you!
ill ask you again if i need some assistance again!
Left by natasha_arriell on Oct 22, 2006 11:29 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thanks man! you've just helped one more people on this planet (which is me!). We love you!!! :D
Left by Boodee on Dec 08, 2006 2:33 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Tks :-)
Left by Marcos on Dec 11, 2006 10:50 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
where is the definition for "GridView1_RowCommand"
where i have to specify this

pls clarify me it will be very helpful
Left by suresh on Jan 03, 2007 3:58 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Great job dude - that code solved my problem
Left by Gary on Feb 14, 2007 7:24 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Solved my Problem too,
in case there is a control in the cell:

for example a label:

int ID = Convert.ToInt32(((Label)row.Cells[0].Controls[1]).Text);

greetz
Left by Stefan on Feb 21, 2007 6:31 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
It doesn't work for me! Where is the "GridView1_RowCommand" rountine?
Left by vik on Feb 24, 2007 1:16 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Ok it's work now :) Thank you! This code is very helpful :)
Left by vik on Feb 24, 2007 1:22 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Arun,
If you are using DataKeys then you can use:
CheckBox checkbox = (CheckBox)sender;
GridViewRow row = (GridViewRow)checkbox.NamingContainer;
GridView grid = (GridView)row.NamingContainer;
Response.Write(grid.DataKeys[row.RowIndex].Value);
Left by Rejish on Feb 24, 2007 8:02 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
it gud and easy.
Left by Rajneesh on Jul 16, 2007 7:51 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thanks! :0)
Left by Simon on Jul 16, 2007 8:46 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
What about changing Formview ItemInsertTemplate value or attribute (hide it) ?
Left by Marc on Aug 06, 2007 3:42 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
What about changing Formview ItemInsertTemplate value or attribute (hide it) ?
Actually I mean how to address, update its value, hide it a textbox (in a Formview, InsertItemTemplate) all before databind().
i.e.
1. user un-checks a checkbox that starts as true (fires CheckChanged)
2. since box is checked, hide a textbox and un-hide another.
-thanks
Left by Marc on Aug 06, 2007 3:48 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
It doesn't work for me! Where is the "GridView1_RowCommand" rountine?
Left by rhiane on Aug 20, 2007 5:18 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
How can i get teh checkbox checked event to generate. If i double click the check box on teh grid, gridveiw selectedindex change event is generated.

If somebody could help me with how this event is generated, I could get my code to work,

Thanks in advance.
Manu
Left by Manu on Aug 31, 2007 9:54 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
The oncheckchanged event isn't firing for me either. I have autopostback="true" and the postback fires if i check the box, but the code is not executed.

<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="CheckBox1_OnCheckedChanged" />

The CheckBox1_OnCheckedChanged sub exists and seems fine, its just not being fired.
Left by TB on Sep 07, 2007 1:48 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
ya, it is quite useful to me.
i really wn't it in some or my work
thanks
Left by hukam on Oct 23, 2007 11:02 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
really very simple. +++
Left by korkut ozkan on Oct 28, 2007 7:42 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
What is GridView1_RowCommand?

This might sound like novoice, but plz help na..
Left by Abdul on Nov 30, 2007 8:43 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Coding in VB with DotNet

add column with checkboxes for each row. my code is behing a submit button. i cannot get gridview control to accept checkbox column as it is unable to parse boolean value

pls assist
Left by adrian on Jan 09, 2008 10:56 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Abdul Remove GridView1_RowCommand then check it
Left by Abhijit B on Jan 17, 2008 11:03 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
when I checked the checkbox inside gridview all the checkboxes become invisible and other control in like textbox in gridview become invisible.

help me out.
Thanks in advance.
Left by shalini on Feb 04, 2008 5:17 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
at last!! much harder than it should be but works a treat - thanks
Left by Tony on Feb 07, 2008 6:59 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Most of them saying It works. Does any one puts ur hands up and give me code for GridView1_RowCommand method. If I remove OnRowCommand then it returns null.
Can Paste Working with all implementation.

Hope atleast one hand will rise tall.

Thanks in Advance.
Left by Guna on Feb 26, 2008 7:57 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thankyou very much for your solution.

It is nice. I can get value of any column from gridview.

Thanks
Left by MohammedAshrafali on Mar 02, 2008 10:00 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
not working
Left by m.omkar on Mar 04, 2008 7:23 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
xzvc xvzxz dfvdfvfdbdbfd
Left by cx xc on Mar 18, 2008 1:01 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thanks
Very helpfull

in vb
Dim ck1 As CheckBox = CType(sender, CheckBox)
Dim dgitem2 As GridViewRow = CType(ck1.NamingContainer, GridViewRow)


Label1.Text = "You selected row " & dgitem2.Cells(0).Text.ToString

Left by antonio torres on Mar 18, 2008 5:25 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thanks, solved my issue.
Left by Herne on Apr 02, 2008 11:22 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thanks a lot !!
Left by Yasin on Apr 07, 2008 6:15 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
The following code only writes "True" to the database, whether or not the checkbox is checked. Any ideas why? The ASP update parameters are type=Boolean and the datatypes in the DB and stored procedure are Bit. I've tried with and without AutoPostBack="True".

<asp:UpdatePanel ID="udp1" UpdateMode="Conditional" runat="server">
<ContentTemplate>
<asp:CheckBox ID="chkEmail" Text="Display?" TextAlign="left" Checked='<%#Bind("Email_Disp")%>' runat="server" OnCheckedChanged="CheckBox1_CheckedChanged" />
</ContentTemplate>
</asp:UpdatePanel>
Left by Bryan on Apr 30, 2008 6:53 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Cool.. Got to try this...
Left by iwilltrythis on May 02, 2008 2:08 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
good article
Left by Manish on Jun 06, 2008 8:37 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
good article really
Left by Manish on Jun 06, 2008 8:37 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Thank you!
Left by Matt on Oct 22, 2010 2:47 PM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Muy buen codigo, gracias desde Peru
Left by Gerardo on Dec 29, 2010 9:17 AM

# re: Gettting Column Value Using Checkbox in GridView Autopostback = true
Requesting Gravatar...
Muy buen codigo, gracias desde Perú
Left by Gerardo on Dec 29, 2010 9:17 AM

Your comment:
 (will show your gravatar)
 


Copyright © Mohammad Azam | Powered by: GeeksWithBlogs.net | Join free