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
Related
Aim: Gridview with checkbox on each line, user then clicks a button and all ticked lines are actioned in the button code. I need the ID's of all the records where the CheckBox is checked.
Error:
Object reference not set to an instance of an object.
on row: ID_Current = row.FindControl("ID").ToString
if I change the to ID_Current = DirectCast(row.FindControl("cbSelect"), CheckBox).Checked I get 'True' as a result but I already know that and want to get the ID.
Aspx-Code with my Gridview:
<asp:Panel ID="ActionGrid" runat="Server">
<h2>Actions To Edit</h2>
<asp:GridView ID="GridView3" runat="server" AllowPaging="True" AllowSorting="True" AlternatingRowStyle-CssClass="alt" AutoGenerateColumns="False" CssClass="mGrid" DataKeyNames="UPRN" DataSourceID="SqlDataSource3" EmptyDataText="There are no data records to display." Font-Size="Medium" PagerStyle-CssClass="pgr" Width="1000px">
<%-- AutoGenerateEditButton="True"--%>
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField ControlStyle-Width="50px" DataField="UPRN" HeaderText="UPRN" ReadOnly="True" SortExpression="UPRN" />
<asp:BoundField DataField="LocationItemPosition" HeaderText="Location Item Position" ReadOnly="True" SortExpression="LocationItemPosition" />
<asp:TemplateField HeaderText="Surveye" SortExpression="SurveyDate">
<ItemTemplate>
<asp:Label ID="lblSurveyDate" runat="server" Text='<%# apFunctionCharacters.fncDateTidy(Eval("SurveyDate"))%>' />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="ItemRef" HeaderText="Item Ref" ReadOnly="True" SortExpression="ItemRef" />
<asp:BoundField DataField="OverallRiskCategory" HeaderText="Overall Risk Category" ReadOnly="True" SortExpression="OverallRiskCategory" />
<asp:BoundField DataField="Comments" HeaderText="Comments" ReadOnly="True" SortExpression="Comments" />
</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" />
</asp:GridView>
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
<asp:Label ID="lblTest" runat="server" Text="Label"></asp:Label>
</asp:Panel>
Submit button:
<asp:Button ID="btnChangeToNA" runat="server" CssClass="Button" Text="Change to NA" />
.aspx.vb stored procedure/action code
Protected Sub btnChangeToNA_Click(sender As Object, e As EventArgs) Handles btnChangeToNA.Click
For Each row As GridViewRow In GridView3.Rows
Dim ID_Current As String = ""
'read the label
If DirectCast(row.FindControl("cbSelect"), CheckBox).Checked Then
ID_Current = row.FindControl("ID").ToString
' change value
'############stored procedure here
Dim connection As SqlConnection
Dim command As New SqlCommand
Dim ds As New DataSet
Dim ConnectionString1 As String = System.Configuration.ConfigurationManager.ConnectionStrings("MY_SQL").ToString()
connection = New SqlConnection(ConnectionString1)
connection.Open()
With command
.Connection = connection
.CommandText = "spChangeValue "
.CommandType = CommandType.StoredProcedure
.Parameters.Clear()
.Parameters.AddWithValue("#ID_Current", ID_Current)
.ExecuteNonQuery()
End With
'#############################
lblTest.Text += ID_Current
End If
'Loop
Next
End Sub
With FindControl you find controls(what suprise) not strings. And you have to pass the ID of the control not a property like Id.
So this doesnt make much sense:
Dim ID_Current As String = row.FindControl("ID").ToString()
Instead you could store the ID in a HiddenField, so on aspx:
<asp:HiddenField ID="HiddenID" runat="server" Value='<%# Eval("ID_Current") %>' />
Now you can get the reference to this HiddenField with FindControl:
Dim hiddenID As HiddenField = DirectCast(row.FindControl("HiddenID"), HiddenField)
Dim ID_Current As String = hiddenID.Value
You need a control with the id "ID"
<ItemTemplate>
<asp:HiddenField ID="ID" runat="server" Value='<%# FillMeSomehow%>' />
<asp:CheckBox ID="cbSelect" runat="server" AutoPostBack="false" />
</ItemTemplate>
How can I delete an entire row in my datagridview.. I already have a delete link in my datagrid..
Here is my markup code in vb
<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%--ADD THE DELETE LINK BUTTON--%>
<asp:LinkButton ID="LinkButton2" Runat="server" OnClientClick ="return confirm('Are you sure you?');"
CommandName="Delete">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="EmployeeID" HeaderText="Locker ID" />
<asp:BoundField DataField="Name" HeaderText="Name" />
<asp:BoundField DataField="EmployeeNo" HeaderText="EmployeeNo" />
<asp:BoundField DataField="Email" HeaderText="Email" />
<asp:BoundField DataField="Location" HeaderText="Location" />
</Columns>
<HeaderStyle BackColor="#003366" BorderColor="#336699" BorderStyle="Solid" ForeColor="White" />
<PagerStyle BackColor="#003366" BorderColor="#336699" ForeColor="White" />
<RowStyle BackColor="White" ForeColor="#003366" />
<SelectedRowStyle BackColor="White" ForeColor="#6600FF" />
<SortedAscendingCellStyle BackColor="#CCCCCC" />
</asp:GridView>
And when I click the delete link this error shows
"The GridView 'EmployeeHallway' fired event RowDeleting which wasn't handled. "
Can anyone help me what to do next
You are using Delete as the CommandName for the delete link so it will automatically creates a RowDeleting event. So you have to implement it like this:
You have to add OnRowDeleting event as below:
<asp:GridView ID="EmployeeHallway" runat="server" AutoGenerateColumns="False" AutoGenerateDeleteButton="False" AutoGenerateSelectButton="True" Height="93px" HorizontalAlign="Center" PageSize="6" style="margin-bottom: 0px; text-align: center;" Width="768px">
<RowStyle ForeColor="#003399" HorizontalAlign="Center" OnRowDeleting="EmployeeHallway_RowDeleting"/>
And at the code-behind:
Public Sub EmployeeHallway_RowDeleting(sender As Object, e As GridViewDeleteEventArgs)
End Sub
Add OnRowDeleting="OnRowDeleting" in aspx page
Saving Your DataTable in ViewState("dt") and then Do Like this:
Protected Sub OnRowDeleting(sender As Object, e As GridViewDeleteEventArgs)
Dim index As Integer = Convert.ToInt32(e.RowIndex)
Dim dt As DataTable = TryCast(ViewState("dt"), DataTable)
dt.Rows(index).Delete()
ViewState("dt") = dt
BindGrid()
End Sub
Protected Sub BindGrid()
EmployeeHallway.DataSource = TryCast(ViewState("dt"), DataTable)
EmployeeHallway.DataBind()
End Sub
I am trying to export selected columns from GridView to Excel, and every time I run the code an error pops up with some new .dll file name. What could be the issue? Please find the code below for your reference.
<asp:Panel ID="general" runat="server" BorderColor="#333333" BorderStyle="Double" CssClass="emailpnl" Width="800px">
<asp:GridView ID="bestpractgrv" runat="server" AutoGenerateColumns="False" OnLoad="bestpractgrv_Load" CellPadding="3" GridLines="Vertical" Width="100%" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px" AllowPaging="True" OnPageIndexChanging = "OnPaging">
<AlternatingRowStyle BackColor="Gainsboro" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkboxSelectAll" runat="server" onclick="CheckAllEmp(this);" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID ="boolcb" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<asp:Label ID="idlbl" runat="server" Text='<%# Bind("iID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Discipline ID">
<ItemTemplate>
<asp:Label ID="discplnlbl" runat="server" Text='<%# Bind("iDisciplineID")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Date Created">
<ItemTemplate>
<asp:Label ID="dateclbl" runat="server" Text='<%# Bind("dDateCreated")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Title">
<ItemTemplate>
<asp:Label ID="bptitlelbl" runat="server" Text='<%# Bind("cBPTitle")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
</asp:Panel>
VB Code
Private Sub PopulateCheckBoxArray()
Dim CheckBoxArray As ArrayList
If ViewState("CheckBoxArray") IsNot Nothing Then
CheckBoxArray = DirectCast(ViewState("CheckBoxArray"), ArrayList)
Else
CheckBoxArray = New ArrayList()
End If
Dim CheckBoxIndex As Integer
Dim CheckAllWasChecked As Boolean = False
Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)
Dim checkAllIndex As String = "chkboxSelectAll-" & bestpractgrv.PageIndex
If chkAll.Checked Then
If CheckBoxArray.IndexOf(checkAllIndex) = -1 Then
CheckBoxArray.Add(checkAllIndex)
End If
Else
If CheckBoxArray.IndexOf(checkAllIndex) <> -1 Then
CheckBoxArray.Remove(checkAllIndex)
CheckAllWasChecked = True
End If
End If
For i As Integer = 0 To bestpractgrv.Rows.Count - 1
If bestpractgrv.Rows(i).RowType = DataControlRowType.DataRow Then
Dim chk As CheckBox = DirectCast(bestpractgrv.Rows(i).Cells(0).FindControl("boolcb"), CheckBox)
CheckBoxIndex = bestpractgrv.PageSize * bestpractgrv.PageIndex + (i + 1)
If chk.Checked Then
If CheckBoxArray.IndexOf(CheckBoxIndex) = -1 AndAlso Not CheckAllWasChecked Then
CheckBoxArray.Add(CheckBoxIndex)
End If
Else
If CheckBoxArray.IndexOf(CheckBoxIndex) <> -1 OrElse CheckAllWasChecked Then
CheckBoxArray.Remove(CheckBoxIndex)
End If
End If
End If
Next
ViewState("CheckBoxArray") = CheckBoxArray
End Sub
And I get the error on following line
Dim chkAll As CheckBox = DirectCast(bestpractgrv.HeaderRow.Cells(0).FindControl("chkboxSelectAll"), CheckBox)
This is the error message
An exception of type 'System.NullReferenceException' occurred in App_Web_pqx0e3wy.dll but was not handled in user code
After every compilation the dll name changes, while App_Web_XXXX.dll remains constant
Hope you have a good weekend.
At long last I have got some editing/delete eventer to work with LINQ support.
I have an Add record event which I know is working, but after trying a part, I do not know how I am adding some textbox's in my footer.
So it is a row from header and down to footer, without the move to the right or the left.
Can some help me !?
My code is
<asp:GridView ID="gdview" runat="server" AutoGenerateColumns="False" DataKeyNames="test_id" OnRowCancelingEdit="gdview_RowCancelingEdit" OnRowDeleting="gdview_RowDeleting" OnRowEditing="gdview_RowEditing" OnRowUpdating="gdview_RowUpdating"
BackColor="White" BorderColor="#CCCCCC" BorderStyle="None" BorderWidth="1px" CellPadding="4" ForeColor="Black" GridLines="Horizontal" ShowFooter="true">
<Columns>
<asp:BoundField HeaderText="Test CAT" DataField="test_cat">
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="Test INFO" DataField="test_info">
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="Test NUMBER" DataField="test_number">
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:BoundField HeaderText="Test DATE" DataField="test_datetime">
<ItemStyle Height="20px" Width="150px" />
</asp:BoundField>
<asp:CommandField ShowEditButton="True">
<ItemStyle Width="100px" />
</asp:CommandField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="lnkdel" runat="server" Text="Delete" CommandName="Delete" OnClientClick="return confirm('Do you want to delete?')"></asp:LinkButton>
</ItemTemplate>
<ItemStyle Width="100px" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCC99" ForeColor="Black" />
<HeaderStyle BackColor="#333333" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="White" ForeColor="Black" HorizontalAlign="Right" />
<SelectedRowStyle BackColor="#CC3333" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F7F7F7" />
<SortedAscendingHeaderStyle BackColor="#4B4B4B" />
<SortedDescendingCellStyle BackColor="#E5E5E5" />
<SortedDescendingHeaderStyle BackColor="#242121" />
</asp:GridView>
Ok but how with this then work, if i use templates, how do it know what cell to get values from !??
Protected Sub gdview_RowUpdating(ByVal sender As Object, ByVal e As GridViewUpdateEventArgs)
Using db As New ThedatabaseconnectionDataContext()
Try
'getting table
Dim tbltest As Table(Of testtable) = db.GetTable(Of testtable)()
'getting an exiting row
Dim objtest As testtable = tbltest.SingleOrDefault(Function(p) p.test_id = Integer.Parse(gdview.DataKeys(e.RowIndex).Value.ToString()))
If objtest IsNot Nothing Then
'modifying the row
objtest.test_cat = DirectCast(gdview.Rows(e.RowIndex).Cells(0).Controls(0), TextBox).Text
objtest.test_info = DirectCast(gdview.Rows(e.RowIndex).Cells(1).Controls(0), TextBox).Text
objtest.test_number = DirectCast(gdview.Rows(e.RowIndex).Cells(2).Controls(0), TextBox).Text
objtest.test_datetime = DirectCast(gdview.Rows(e.RowIndex).Cells(3).Controls(0), TextBox).Text
'saving rows added
db.SubmitChanges()
gdview.EditIndex = -1
bindgrid()
' Me.lblMsg.Text = "Updated Successfully"
Else
'Me.lblMsg.Text = "Employee not found"
End If
Catch ex As Exception
'Me.lblMsg.Text = ex.Message
End Try
End Using
You can try with
<columns>
<asp:templatefield headertext="Test" >
<itemtemplate>
....
</itemtemplate>
<footertemplate>
<asp:TextBox id="TestTbx" runat="server"/>
</footertemplate>
</asp:templatefield>
</columns>
Link : http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.templatefield.footertemplate.aspx
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.