Answered by:
Checkbox in Gridview

Question
-
User66371569 posted
Hi i have checkbox inside gridview
what i want to get value of forexsample cells(2) when checked
but i want it frist check only second third ...... ignored
not first row first check whatever row is but in first check only i hope u understand meSaturday, September 29, 2018 3:46 PM
Answers
-
User61956409 posted
Hi thepast,
thepast
forexample I have 5 rows gridview and all rows has checkbox, I want user when checkbox first time, get valueYou can maintain a flag that indicates if the row is user's first checked row, as oned_gk mentioned, you can use session retain that flag etc. The following example works for me, you can refer to it to achieve your requirement.
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="ID" DataField="ID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> </Columns> </asp:GridView> <br /> <asp:Label ID="lblresult" runat="server" Text=""></asp:Label> </div>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then 'bindgrid Session("is_first_checked") = True End If End Sub Protected Sub chk_CheckedChanged(sender As Object, e As EventArgs) Dim chk As CheckBox = CType(sender, CheckBox) Dim grdrow As GridViewRow = chk.Parent().Parent() Dim cellval As String = grdrow.Cells(2).Text If Session("is_first_checked") And chk.Checked Then Session("is_first_checked") = False lblresult.Text = "The text of the third cell in your first checked row is " + cellval End If End Sub
Test Result:
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 1, 2018 5:54 AM
All replies
-
User-1716253493 posted
Hard to understand your requirement, but i hope that it's not harder than understand vb or c#
Dim pass As Boolean = False Dim fisrtchekvalue As String For Each row As GridViewRow In GridView1.Rows If Not pass Then Dim cb As CheckBox = CType(row.FindControl("cb1"), CheckBox) If cb.Checked Then fisrtchekvalue = row.Cells(1).Text pass = True End If End If Next
Saturday, September 29, 2018 5:12 PM -
User66371569 posted
I tried this code but still give me value whenever I checked
I will explain it forexample I have 5 rows gridview and all rows has checkbox I want user when checkbox first time get value for ex. cell(9)
when he check another row nothing happened I want keep first value checked only maybe user chakeck last row or first row or middle I don't care I care only in first check
Sunday, September 30, 2018 6:01 AM -
User-1716253493 posted
use viewstate or session to store pass(boolean)
Use gv databound to set pass = false
use check changed event to set the value and pass = true
only pass = false to set new value
Monday, October 1, 2018 1:11 AM -
User61956409 posted
Hi thepast,
thepast
forexample I have 5 rows gridview and all rows has checkbox, I want user when checkbox first time, get valueYou can maintain a flag that indicates if the row is user's first checked row, as oned_gk mentioned, you can use session retain that flag etc. The following example works for me, you can refer to it to achieve your requirement.
<div> <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"> <Columns> <asp:TemplateField> <ItemTemplate> <asp:CheckBox ID="chk" runat="server" OnCheckedChanged="chk_CheckedChanged" AutoPostBack="true" /> </ItemTemplate> </asp:TemplateField> <asp:BoundField HeaderText="ID" DataField="ID" /> <asp:BoundField HeaderText="Name" DataField="Name" /> </Columns> </asp:GridView> <br /> <asp:Label ID="lblresult" runat="server" Text=""></asp:Label> </div>
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load If Not IsPostBack Then 'bindgrid Session("is_first_checked") = True End If End Sub Protected Sub chk_CheckedChanged(sender As Object, e As EventArgs) Dim chk As CheckBox = CType(sender, CheckBox) Dim grdrow As GridViewRow = chk.Parent().Parent() Dim cellval As String = grdrow.Cells(2).Text If Session("is_first_checked") And chk.Checked Then Session("is_first_checked") = False lblresult.Text = "The text of the third cell in your first checked row is " + cellval End If End Sub
Test Result:
With Regards,
Fei Han
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Monday, October 1, 2018 5:54 AM -
User66371569 posted
Wow It amazing worked 100% thank you so much
Monday, October 1, 2018 6:01 AM