Web site in Visual Studio 2010 with VB. I created a GridView with a checkbox column, but my code behind is not recognizing the checked rows. Runs through with no error but never sees a checked row.
<asp:GridView
ID="gvwOrderDetails"
DataKeyNames="ID"
runat="server"
BackColor="White"
BorderColor="#DEDFDE"
BorderStyle="None"
BorderWidth="1px"
CellPadding="4"
ForeColor="Black"
GridLines="Vertical">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="OrderSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
Protected Sub btnVoid_Click(sender As Object, e As System.EventArgs) Handles btnVoid.Click
Dim atLeastOneRowVoided As Boolean = False
For Each row As GridViewRow In gvwOrderDetails.Rows
Dim cb As CheckBox = TryCast(row.Cells(0).FindControl("OrderSelector"), CheckBox)
If cb IsNot Nothing AndAlso cb.Checked Then
atLeastOneRowVoided = True
Dim orderID As Integer = _
Convert.ToInt32(gvwOrderDetails.DataKeys(row.RowIndex).Value)
lbl1.Text &= String.Format( _
"This would have voided ProductID {0}<br />", orderID)
End If
Next
End Sub
Related
I have GridView named GridViewTest and the first column in the GridView is a templatefield checkbox named CheckBox1. Im able to loop through all the rows that the GridView contains and display the value in a label named Label2. The issue i'm having is, I only want the rows that are checked to be displayed in the Label2. Can anyone help me?
Protected Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click
Dim str As String = ""
For i As Integer = 0 To GridViewTest.Rows.Count - 1
str = (str + GridViewTest.Rows(i).Cells(0).Text & Convert.ToString(" >> ")) + GridViewTest.Rows(i).Cells(1).Text + " >> " + >GridViewTest.Rows(i).Cells(2).Text + "<br/>"
Next
Label2.Text = str
End Sub
PS: This is VB ASP.NET
<asp:GridView ID="GridViewTest" runat="server"
AutoGenerateColumns="False" BackColor="White" BorderColor="#DEDFDE"
BorderStyle="None" BorderWidth="1px" CellPadding="4"
CssClass="GridviewTable" DataSourceID="SqlDataSourceDetailGrid" EnableTheming="True"
Font-Names="Arial" Font-Size="8pt" ForeColor="Black" GridLines="Vertical"
Height="100%" PageSize="15" TabIndex="25" Width="985px" AllowPaging="True" EnableModelValidation="True">
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" CssClass="Freezing" Font-Bold="True"
ForeColor="#FFFFCC" Wrap="False" />
<RowStyle BackColor="#F7F7DE" VerticalAlign="Middle" Wrap="False" />
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<EditItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="TestColumn1" HeaderText="TestColumn1" />
<asp:BoundField DataField="TestColumn2" HeaderText="TestColumn2" />
<asp:BoundField DataField="TestColumn3" HeaderText="TestColumn3" />
<asp:BoundField DataField="TestColumn4" HeaderText="TestColumn4" />
<asp:BoundField DataField="TestColumn5" HeaderText="TestColumn5" />
<asp:BoundField DataField="TestColumn6" HeaderText="TestColumn6" />
</Columns>
<PagerSettings PageButtonCount="20" Position="TopAndBottom" />
<PagerStyle BackColor="#F7F7DE" Font-Size="10pt" ForeColor="Black"
HorizontalAlign="Center" VerticalAlign="Middle" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
If you are using a template field having a checkbox with an id of CheckBox1, then use the code below in the Page_PreRender event.
For Each row As GridViewRow In GridViewTest.Rows
Dim result As Boolean = DirectCast(row.FindControl("CheckBox1"), CheckBox).Checked
if result = True Then
Label2.Text = string.Format("{0},row:{1} is {3}", Label2.Text , row.RowIndex, result);
End If
Next
Gurus,
Further to My query on Gridview:TemplateField
I have a gridview with ButtonField. I want to change text based on the user type on page load.
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Trim(Request.QueryString("Msg")) <> "" Then
If InStr(Request.QueryString("Msg"), "<") > 0 Then
Response.Write(Mid(Request.QueryString("Msg"), 1, InStr(Request.QueryString("Msg"), "<")))
Else
Response.Write(Request.QueryString("Msg"))
End If
End If
If Page.IsPostBack = False Then
Dim sSeries As String
sSeries = "Aim NBDE Part 1"
Dim o_cmd As SqlCommand
Dim o_reader As SqlDataReader
o_Con = New SqlConnection(GlobalVarC.DataS)
o_Con.Open()
Dim ds As New Data.DataSet
Dim da As SqlDataAdapter
Dim ExamId As String
S_Sql = "SELECT SNo from Exam_Ser where Series='" & sSeries.ToString & "'"
o_cmd = New SqlCommand(S_Sql, o_Con)
o_reader = o_cmd.ExecuteReader
ExamId = ""
Dim ExamIdTmp As String
While o_reader.Read
If ExamId.Equals("") Then
ExamId = ExamId + "SNo = "
Else
ExamId = ExamId + " OR SNo = "
End If
ExamIdTmp = o_reader(0).ToString
ExamId = ExamId + ExamIdTmp
ExamId = ExamId + ""
End While
o_reader.Close()
o_cmd.Dispose()
S_Sql = "SELECT Name from Exam where " & ExamId.ToString
da = New SqlDataAdapter(S_Sql, o_Con)
da.Fill(ds)
GridSubject.DataSource = ds
GridSubject.DataBind()
da.Dispose()
ds.Dispose()
o_Con.Close()
For Each row As GridViewRow In GridSubject.Rows
Dim button As Button
button = DirectCast(row.FindControl("idAppearButton"), Button)
button.Text = "Buy"
Next
End If
End Sub
I am able perform operation on button click from GridSubject_SelectedIndexChanged.
I am trying to get the button from Gridview to change the text. I tried some search and found way to control button on click from 'GridSubject_SelectedIndexChanged' but not on page load.
It will be really helpful if I can get pointers on how to get handle of ButtonField CommandName="ViewResults") on page load.
<asp:ButtonField ButtonType="Button" CommandName="ViewResults" Text="Results" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:ButtonField>
<div id="gridViewId" align="center">
<asp:GridView ID="GridSubject" runat="server" AutoGenerateColumns="False" CellPadding="3"
OnSelectedIndexChanged="GridSubject_SelectedIndexChanged" BackColor="#CCCCCC"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
Width="714px" GridLines="Horizontal"
>
<EditRowStyle Font-Names="Calibri" />
<EmptyDataRowStyle Font-Names="Calibri" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" Font-Names="Calibri" />
<AlternatingRowStyle BackColor="#F7F7F7" Font-Names="Calibri" />
<Columns >
<asp:BoundField DataField="Name" HeaderText="Exam Name">
<ItemStyle Width="140px" HorizontalAlign="Left" VerticalAlign="Top" />
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="100px">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="0">Part 1</asp:ListItem>
<asp:ListItem Value="1">Part 2</asp:ListItem>
<asp:ListItem Selected="True" Value="3">Part 1 & 2</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="100px" HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="idAppearButton" runat="server"
CommandName="MYCOMMAND" Text="Appear">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="ViewResults" Text="Results" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:ButtonField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text="Not Completed"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E7E7FF" ForeColor="#000000" Font-Names="Calibri" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7"
Font-Names="Cambria" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right"
Font-Names="Calibri" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<SortedAscendingCellStyle BackColor="#F4F4FD"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#5A4C9D"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#D8D8F0"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#3E3277"></SortedDescendingHeaderStyle>
</asp:GridView>
</div>
Use the RowDataBound event of the grid view to work with each row as it is bound to the grid, like this:
Protected Sub gridViewId_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
' Find the button to change the text of
Dim theButtonToChangeTextOf As Button = CType(e.Row.FindControl("idAppearButton"), Button)
theButtonToChangeTextOf.Text = theTextYouWantForTheButtonHere
End If
End Sub
Gurus,
I have a gridview with the template field button and I want to change text on the button on the user type on page load.
I am trying to get the button from Gridview to change the text and link but the control always return null. It will be really helpful to understand how to change the text and link on page load.
TryCast(row.Cells(0).Controls(0).FindControl("idAppearButton"),
Button)
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Trim(Request.QueryString("Msg")) <> "" Then
If InStr(Request.QueryString("Msg"), "<") > 0 Then
Response.Write(Mid(Request.QueryString("Msg"), 1, InStr(Request.QueryString("Msg"), "<")))
Else
Response.Write(Request.QueryString("Msg"))
End If
End If
If Page.IsPostBack = False Then
Dim sSeries As String
sSeries = "Aim NBDE Part 1"
Dim o_cmd As SqlCommand
Dim o_reader As SqlDataReader
o_Con = New SqlConnection(GlobalVarC.DataS)
o_Con.Open()
Dim ds As New Data.DataSet
Dim da As SqlDataAdapter
Dim ExamId As String
S_Sql = "SELECT SNo from Exam_Ser where Series='" & sSeries.ToString & "'"
o_cmd = New SqlCommand(S_Sql, o_Con)
o_reader = o_cmd.ExecuteReader
ExamId = ""
Dim ExamIdTmp As String
While o_reader.Read
If ExamId.Equals("") Then
ExamId = ExamId + "SNo = "
Else
ExamId = ExamId + " OR SNo = "
End If
ExamIdTmp = o_reader(0).ToString
ExamId = ExamId + ExamIdTmp
ExamId = ExamId + ""
End While
o_reader.Close()
o_cmd.Dispose()
S_Sql = "SELECT Name from Exam where " & ExamId.ToString
da = New SqlDataAdapter(S_Sql, o_Con)
da.Fill(ds)
GridSubject.DataSource = ds
GridSubject.DataBind()
da.Dispose()
ds.Dispose()
o_Con.Close()
For Each row As GridViewRow In GridSubject.Rows
Dim button As Button
'button = TryCast(row.Cells(0).Controls(0).FindControl("idAppearButton"), Button)
'button.Text = "Buy"
Next
End If
End Sub
<asp:GridView ID="GridSubject" runat="server" AutoGenerateColumns="False" CellPadding="3"
OnSelectedIndexChanged="GridSubject_SelectedIndexChanged" BackColor="#CCCCCC"
BorderColor="#E7E7FF" BorderStyle="None" BorderWidth="1px"
Width="714px" GridLines="Horizontal"
>
<EditRowStyle Font-Names="Calibri" />
<EmptyDataRowStyle Font-Names="Calibri" />
<FooterStyle BackColor="#B5C7DE" ForeColor="#4A3C8C" Font-Names="Calibri" />
<AlternatingRowStyle BackColor="#F7F7F7" Font-Names="Calibri" />
<Columns >
<asp:BoundField DataField="Name" HeaderText="Exam Name">
<ItemStyle Width="140px" HorizontalAlign="Left" VerticalAlign="Top" />
<HeaderStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:TemplateField ItemStyle-Width="100px">
<ItemTemplate>
<asp:RadioButtonList ID="RadioButtonList1" runat="server">
<asp:ListItem Value="0">Part 1</asp:ListItem>
<asp:ListItem Value="1">Part 2</asp:ListItem>
<asp:ListItem Selected="True" Value="3">Part 1 & 2</asp:ListItem>
</asp:RadioButtonList>
</ItemTemplate>
<FooterStyle HorizontalAlign="Left" />
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle Width="100px" HorizontalAlign="Left"></ItemStyle>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:Button ID="idAppearButton" runat="server"
CommandName="MYCOMMAND" Text="Appear">
</asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:ButtonField ButtonType="Button" CommandName="ViewResults" Text="Results" >
<HeaderStyle HorizontalAlign="Center" VerticalAlign="Top" />
</asp:ButtonField>
<asp:TemplateField HeaderText="Status">
<ItemTemplate>
<asp:Label runat="server" Text="Not Completed"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<RowStyle BackColor="#E7E7FF" ForeColor="#000000" Font-Names="Calibri" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="#F7F7F7"
Font-Names="Cambria" />
<PagerStyle BackColor="#E7E7FF" ForeColor="#4A3C8C" HorizontalAlign="Right"
Font-Names="Calibri" />
<HeaderStyle BackColor="#000000" Font-Bold="True" ForeColor="#F7F7F7" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<SortedAscendingCellStyle BackColor="#F4F4FD"></SortedAscendingCellStyle>
<SortedAscendingHeaderStyle BackColor="#5A4C9D"></SortedAscendingHeaderStyle>
<SortedDescendingCellStyle BackColor="#D8D8F0"></SortedDescendingCellStyle>
<SortedDescendingHeaderStyle BackColor="#3E3277"></SortedDescendingHeaderStyle>
</asp:GridView>
You have to use Row.FindControl(id) to find the reference to the control. You cannot use Cell.Text if you use TemplateFields, that works only with BoundFields:
button = DirectCast(row.FindControl("idAppearButton"), Button)
I'm originally having problems with detecting the status of a checkbox in my grid View.
Apparently one of the solutions to the problem would be adding DataKeyNames value to the Grid View. But if i add DataKey value to my Grid View it's not being populated when I run the code.
Can anybody point me on my mistake?
here is the original head of the grid View which can be populated:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px">
Here is the code after i add the DataKeyNames:
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px" DataKeyNames="account_id">
And here is the backend code to populate the gridView
Dim StrQwery As String = "SELECT account_id, account_name bla bla bla"
Dim smd As MySqlCommand
smd = New MySqlCommand(StrQwery, myconn)
smd.CommandType = CommandType.Text
Dim da As New MySqlDataAdapter(smd)
Dim cb As New MySqlCommandBuilder(da)
Dim ds As New DataSet()
da.Fill(ds)
GridView1.DataSource = ds.Tables(0)
GridView1.DataBind()
I can't understand why the values are not filled in Grid View after I add DatakeyNames
the entire code of Grid View
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None" Width="1500px" >
<Columns>
<asp:TemplateField >
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" textAlign="right"
/>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<AlternatingRowStyle BackColor="White" />
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
I've been reading a few posts in here and some are related to my problem, but for some reason the solutions provided dont work for me. Basically I have two GridView controls with checkboxes. They have the exact same code, but just fire different events. And of course have different IDs and their pertinent unique information. Here is the weird thing, one of them works just fine, and the other one doesn't. I will post both here, keep in mind that the one with ID="UnassignElegibilityChk" is the onte that is not working. I've put breakpoints to test whether I am inside the function when I send the postback from the check and nothing.
Note: both of these treeviews are on the same page, so please assume the codebehind directive and all the settings are properly assigned to the page (since one of them is working i would assume there is no problem with that)
This is the one that is not working:
<table><tr><td class="style1">
<asp:GridView ID="ElegibilitySelectedGridview" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID" CellPadding="4"
ForeColor="#333333" GridLines="None" Width="475px">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID0" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="UnassignElegibilityChk" runat="server" AutoPostBack="true" OnCheckedChanged="UnAssignElegibilityRecord"/>
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="Name" HeaderText="Elegible Item" />
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
</td></tr></table>
The one that is working is below:
<table><tr><td class="style1">
<asp:DropDownList ID="ElegibilityGroupDDL" runat="server" AutoPostBack="True" OnSelectedIndexChanged="ElegibilityGroupDDL_SelectedIndexChanged">
<asp:ListItem Value="0">ROOT</asp:ListItem>
</asp:DropDownList>
</td></tr><tr><td class="style1">
<asp:TextBox ID="ElegibilityNameTxt" runat="server" Width="464px"></asp:TextBox>
</td></tr><tr><td class="style1">
<asp:Button ID="ElegibilitySaveBtn" runat="server" Text="Save Elegibility" />
</td></tr><tr><td class="style1">
<asp:Label ID="ElegibilityMsgLbl" runat="server" Text="" style="color: #0066CC"></asp:Label>
</td></tr><tr style="background:silver"><td class="style1">
</td></tr><tr><td class="style1">
<asp:GridView ID="ElegibilityGridView" runat="server"
AutoGenerateColumns="False" DataKeyNames="ID" BackColor="White"
BorderColor="#DEDFDE" BorderStyle="None" BorderWidth="1px" CellPadding="4"
ForeColor="Black" GridLines="Vertical" Width="95%">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="lblID" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="AssignElegibilityChk" runat="server" AutoPostBack="true" OnCheckedChanged="AssignElegibility"/>
</ItemTemplate>
</asp:TemplateField>
<asp:boundfield datafield="Name" HeaderText="Elegible Item" />
</Columns>
<FooterStyle BackColor="#CCCC99" />
<HeaderStyle BackColor="#6B696B" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#F7F7DE" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#F7F7DE" />
<SelectedRowStyle BackColor="#CE5D5A" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FBFBF2" />
<SortedAscendingHeaderStyle BackColor="#848384" />
<SortedDescendingCellStyle BackColor="#EAEAD3" />
<SortedDescendingHeaderStyle BackColor="#575357" />
</asp:GridView>
</td></tr></table>
The working gridview fires this function successfully:
Public Sub AssignElegibility()
If Page.IsPostBack Then
For Each row As GridViewRow In ElegibilityGridView.Rows
Dim checkbox As CheckBox = CType(row.FindControl("AssignElegibilityChk"), CheckBox)
'Check if the checkbox is checked.'
'value in the HtmlInputCheckBox Value property is set as the //value of the delete commands parameter.'
If checkbox.Checked Then
' Retreive the Employee ID'
Dim lblID As Label = CType(row.FindControl("lblID"), Label)
Dim elegibilityID As Integer = Convert.ToInt32(lblID.Text)
Elegibility.AddElegibilityMapping(CInt(Request("ResID")), elegibilityID)
ElegibilitySelectedItems(CInt(Request("ResID")))
ElegibilityGroupDDL_SelectedIndexChanged()
End If
Next
End If
End Sub
The non working one is supposed to fire this code and it isn't.
Public Sub UnAssignElegibilityRecord(ByVal sender As Object, ByVal e As System.EventArgs)
PrintLine("Inside the sub")
If Page.IsPostBack Then
For Each row As GridViewRow In ElegibilitySelectedGridview.Rows
Dim checkbox As CheckBox = CType(row.FindControl("UnassignElegibilityChk"), CheckBox)
'Check if the checkbox is checked.'
'value in the HtmlInputCheckBox Value property is set as the //value of the delete commands parameter.'
If checkbox.Checked Then
' Retreive the Employee ID'
Dim lblID As Label = CType(row.FindControl("lblID0"), Label)
Dim elegibilityID As Integer = Convert.ToInt32(lblID.Text)
Elegibility.RemoveElegibilityMapping(elegibilityID)
ElegibilitySelectedItems(CInt(Request("ResID")))
ElegibilityGroupDDL_SelectedIndexChanged()
End If
Next
End If
End Sub
I don't know if anything of the following solves your problem, but ...
why does your "working" handler of the CheckedChanged has the wrong signature? It needs the sender and the eventsargs.
protected Sub AssignElegibility(ByVal sender As Object, ByVal e As System.EventArgs)
Why do you iterate the Gridrows to get the Checkbox and the row that has changed? This works also:
Dim checkbox As CheckBox = DirectCast(sender,CheckBox)
To get the Row you only have to cast the NamingContainer of your Checkbox to GridViewRow.
Dim row as GridViewRow =DirectCast(checkbox.NamingContainer,GridViewRow)
EDIT: Do you rebind the ElegibilitySelectedGridview-Grid on Postback?
You should only do that when not Page.IsPostback, otherwise events won't fire.