How do I add FooterTemplate to GridView with BoundFields - asp.net

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

Related

unable to fetch id value of a row from a gridview to delete the particular row

I am trying to delete a particular row from a gridview based on the value of a particular column(column Id) for that row.
the gridview looks like:
<asp:GridView ID="grd_issuesDetails" runat="server" AllowPaging="True" AutoGenerateColumns="False" OnRowDataBound="grd_issuesDetails_RowDataBound"
GridLines="None" Width="600px" CellPadding="4" ForeColor="#333333" OnPageIndexChanging="grd_issuesDetails_PageIndexChanging" DataKeyNames="id" OnSelectedIndexChanged="grd_issuesDetails_SelectedIndexChanged" onrowdeleting="grd_issuesDetails_RowDeleting">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="id" HeaderText="Id" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
<asp:BoundField DataField="img_name" HeaderText="Image name" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
<asp:BoundField DataField="teacher_code" HeaderText="Teacher code" ><ItemStyle HorizontalAlign="Center" /></asp:BoundField>
<asp:BoundField DataField="issue_date" HeaderText="Reported date" ></asp:BoundField>
<asp:TemplateField HeaderText="Resolve">
<ItemTemplate>
<asp:Button ID="Buttonid" runat="server" CommandName="fetch" Text="Fetch" OnClick="Button_fetch"></asp:Button>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="status" HeaderText="Status" />
<asp:TemplateField>
<ItemTemplate>
<asp:ImageButton ID="img_user" runat="server" CommandName="Select" ImageUrl='<%# Eval("Status") %>' Width="20px" Height="20px" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Action">
<ItemTemplate>
<asp:ImageButton ID="imgbtnDelete" runat="server" CommandName="Delete" Width="20px" Height="20px" ImageUrl="~/Images/Delete.png"/>
</ItemTemplate>
<EditItemTemplate>
<asp:ImageButton ID="imgbtnUpdate" runat="server" CommandName="Update" ImageUrl="~/Images/icon-update.png"/>
<asp:ImageButton ID="imgbtnCancel" runat="server" CommandName="Cancel" ImageUrl="~/Images/icon-Cancel.png"/>
</EditItemTemplate>
<FooterTemplate>
<asp:LinkButton ID="lbtnAdd" runat="server" CommandName="ADD" Text="Add" ></asp:LinkButton>
</FooterTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="false" ForeColor="White" />
<HeaderStyle BackColor="#0000CD" Font-Bold="false" ForeColor="white" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="false" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
<PagerStyle HorizontalAlign = "Right" CssClass = "GridPager" />
</asp:GridView>
the codebehind:
protected void grd_issuesDetails_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
string id = grd_issuesDetails.DataKeys[grd_issuesDetails.SelectedIndex].Value.ToString();
//int row = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Value);
SqlCommand cmd = new SqlCommand("Delete from [Issuereport] where id= '"+id+"' ", con);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
I get the error :
Index was out of range. Must be non-negative and less than the size of
the collection. Parameter name: index Description: An unhandled
exception occurred during the execution of the current web request.
Please review the stack trace for more information about the error and
where it originated in the code.
Exception Details: System.ArgumentOutOfRangeException: Index was out
of range. Must be non-negative and less than the size of the
collection. Parameter name: index
Source Error:
Line 123: {
Line 124:
Line 125: string id = grd_issuesDetails.DataKeys[grd_issuesDetails.SelectedIndex].Value.ToString();
Line 126:
Line 127: //int row = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Value);
try this
string id = grd_issuesDetails.Rows[e.RowIndex].Cells[your cell vaule index like 1,2... whatever].Text.Trim();
just pass id to your query
I managed to resolve it by using:
int id = Convert.ToInt32(grd_issuesDetails.DataKeys[e.RowIndex].Values[0]);
SqlCommand cmd = new SqlCommand("Delete from [Issuereport] where id= '"+id +"' ", con);

GridView Checked Rows Values VB asp.net

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

Update control from .NET DataGrid on row select

I have what seems on the surface a really simple requirement. I want to update a label with a value from the data source when a user clicks a field in a datagrid control.
My page has an SqlDataSource control on it, which returns (for example) 6 columns. I display 5 of them in the datagrid, and want the sixth column shown in a label when the user selects a row.
I have tried various things, with limited success. One way I was convinced would work was to include the column in the datagrid, but set it to Visible="false". However, it turns out that if you do this, the value of row(5).text is ""... Not what I expected.
Any quick way of achieving this?
-- EDIT - Added in code samples --
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />
</Columns>
<FooterStyle BackColor="#C6C3C6" ForeColor="Black" />
<HeaderStyle BackColor="#4A3C8C" Font-Bold="True" ForeColor="#E7E7FF" />
<PagerStyle BackColor="#C6C3C6" ForeColor="Black" HorizontalAlign="Right" />
<RowStyle BackColor="#DEDFDE" ForeColor="Black" />
<SelectedRowStyle BackColor="#9471DE" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#594B9C" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#33276A" />
</asp:GridView>
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
VB Code:
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Label1.Text = row.Cells(9).Text
End Sub
You could use a hidden <asp:TemplateField> with a label instead. I tested that out and it seems to work.
<asp:GridView ID="gridL250Tickets" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="Ridge" BorderWidth="2px" Caption="Last 250 Tickets" CellPadding="3" CellSpacing="1" DataKeyNames="TICKETID" DataSourceID="sqlSlxL250Tickets" AllowPaging="True" AllowSorting="True" HorizontalAlign="Center" Width="75%" PageSize="6">
<Columns>
<asp:CommandField ShowSelectButton="True" />
<asp:BoundField DataField="SUBJECT" HeaderText="Ticket" SortExpression="SUBJECT" />
<asp:BoundField DataField="RECEIVEDDATE" DataFormatString="{0:d}" HeaderText="Received" SortExpression="RECEIVEDDATE" />
<asp:BoundField DataField="COMPLETEDDATE" DataFormatString="{0:d}" HeaderText="Completed" SortExpression="COMPLETEDDATE" />
<asp:BoundField DataField="AREA" HeaderText="Area" SortExpression="AREA" />
<asp:BoundField DataField="CATEGORY" HeaderText="Category" SortExpression="CATEGORY" />
<asp:BoundField DataField="ISSUE" HeaderText="Issue" SortExpression="ISSUE" />
<asp:BoundField DataField="notes1" HeaderText="Notes" SortExpression="notes1" Visible="False" />
<asp:BoundField DataField="USERNAME" HeaderText="Ass. To" SortExpression="USERNAME" />
<%--<asp:BoundField DataField="notes" HeaderText="Notes - l" SortExpression="notes" Visible="false" />--%>
<asp:TemplateField Visible="false">
<ItemTemplate>
<asp:Label ID="lblNotes" runat="server" Text='<%# Eval("notes") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
//...
Then just find the label in your event and use its text.
Private Sub gridL250Tickets_SelectedIndexChanged(sender As Object, e As EventArgs) Handles gridL250Tickets.SelectedIndexChanged
Dim row As GridViewRow = gridL250Tickets.SelectedRow
Dim lblNotes As Label = row.FindControl("lblNotes")
Label1.Text = lblNotes.Text
End Sub
Instead of setting Visible = "False", try setting the actual CSS of the given column. Display = "none", and switch it back when the grid control is clicked.
Something along the lines of lblNotes.Style.Item("display") = "none". You could also do that in the Javascript if you didn't want to have a pageback.

Send email using gridview checked employees to them in asp.net vb

Good day
I have grid view with employee data displaying in it.
<asp:TabContainer ID="tcmanagers" runat="server" ActiveTabIndex="0"
Width="100%">
<asp:TabPanel ID="TabAdmins" runat="server" HeaderText="Admins" TabIndex="3">
<ContentTemplate>
<asp:Label ID="Label4" runat="server" Font-Bold="False" Font-Italic="False" CssClass="style3"
ForeColor="Blue" Width="290px">Select Admins to send:</asp:Label>
<asp:Button ID="Sendadmins" runat="server" Text="Send Email" Width="201px"/>
<br />
<br />
<asp:GridView ID="GridView3" runat="server" AutoGenerateColumns="False" DataKeyNames="Employee_ID"
DataSourceID="SqlDataSource3" Width="633px" CssClass="grd">
<Columns>
<asp:BoundField DataField="Employee_ID" HeaderText="Employee_ID" ReadOnly="True"
SortExpression="Employee_ID" Visible="False" />
<asp:BoundField DataField="Employee_Number" HeaderText="Employee Number" SortExpression="Employee_Number">
<ItemStyle Width="70px" Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Surname" HeaderText="Surname" SortExpression="Employee_Surname">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Name" HeaderText="Name" SortExpression="Employee_Name">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Job_Title" HeaderText="Job Title" SortExpression="Employee_Job_Title">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_Dept" HeaderText="Department" SortExpression="Employee_Dept">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:BoundField DataField="Employee_EMail" HeaderText="Email Address" SortExpression="Employee_EMail">
<ItemStyle Wrap="False" />
</asp:BoundField>
<asp:TemplateField HeaderText="Check All"><HeaderTemplate>
<asp:CheckBox ID="chkAlladmin" runat="server" AutoPostBack="True" OnCheckedChanged="chkAllAdmin_CheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="chkAdmin" runat="server" />
</ItemTemplate>
</asp:TemplateField>
</Columns><RowStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt" />
<EditRowStyle Font-Names="Arial" Font-Size="10pt" /><HeaderStyle Font-Bold="False" Font-Italic="False" Font-Names="Arial" Font-Size="10pt"
Font-Underline="False" CssClass="grdhead" HorizontalAlign="Center" />
<AlternatingRowStyle CssClass="grdalt" />
<SelectedRowStyle BackColor="Fuchsia" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:ApplicationServices %>"
ProviderName="<%$ ConnectionStrings:ApplicationServices.ProviderName %>" SelectCommand="SELECT dbo.lms_Employee.Employee_ID,dbo.lms_Employee.Employee_EMail,dbo.lms_Employee.Employee_Number, dbo.lms_Employee.Employee_Surname, dbo.lms_Employee.Employee_Name, dbo.lms_Employee.Employee_Job_Title,dbo.lms_Employee.Employee_Dept FROM dbo.aspnet_Membership INNER JOIN dbo.aspnet_Users ON dbo.aspnet_Membership.UserId = dbo.aspnet_Users.UserId INNER JOIN dbo.aspnet_UsersInRoles ON dbo.aspnet_Users.UserId = dbo.aspnet_UsersInRoles.UserId INNER JOIN dbo.lms_Employee ON dbo.aspnet_Users.UserName = dbo.lms_Employee.Employee_UserName WHERE (dbo.lms_Employee.Employee_Active = 'True') AND (dbo.aspnet_UsersInRoles.RoleId = '9ec9b26d-64a1-4736-8e82-fe6de7c7caac') and Employee_Number <> '100'">
</asp:SqlDataSource>
</ContentTemplate>
</asp:TabPanel>
</asp:TabContainer>
Now I can select all data in it or one at a time with code below.
Protected Sub chkAllAdmin_CheckedChanged(sender As Object, e As EventArgs)
For Each gr As GridViewRow In GridView3.Rows
Dim cb As CheckBox = DirectCast(gr.FindControl("chkAdmin"), CheckBox)
If DirectCast(sender, CheckBox).Checked Then
cb.Checked = True
Else
cb.Checked = False
End If
Next
End Sub
What I want to do is send a email to all the selected employees. I have an iis server and sql server. So my database is on sql server and my web application is on iis server.
I have no idea how to approached this any help will be appreciated.
Without coding it for you, here's the strategy I use for this type of task:
Convert the BoundField items to TemplateFields. When you do this, you'll likely have controls with meaningful names inside the ItemTemplate for each field, like "lblEmployeeEmail"
On your button-click, loop through the items in the GridView and for each row, use the FindControl() method to find the CheckBox (and then check if it's checked) and use the FindControl() also to get any other fields you might need. (The email label, etc.)
Here's the generic code pattern to follow (lifted from learneveryday.net):
foreach (GridViewRow gvrow in GridView1.Rows)
{
CheckBox CheckBox1 = (CheckBox)gvrow.FindControl("CheckBox1");
if (CheckBox1.Checked)
{
// we write this code for find a label value
Label lblLookFor = (Label)gvrow.FindControl("lblLookFor");
string lookfor = lblLookFor.Text;
}
}
Of course, this only covers how to get the employees that are checked and how to retrieve their email address from the GridViewRows. I'm assuming you already know how to send emails, but if not, see the MSDN documentation.

OnCheckedChange Event does not execute my event

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.

Resources