Connecting dropdown to database - asp.net

I asked a question a week ago on how to connect my checkbox to a database but i realised my database would not function the way i wanted it to due to a many to many problem.
So now the user will select a 'question' then select from the dropdown what 'paperID' the new test will have.
So i need to be able to retrieve the paperID from the dropdown and the QuestionID from the selected rows and save them under in a new table.
Here is my current code behind for the 'TestCreation' page which will compile both the PaperID and the 'QuestionID' into the 'QuestionInPaper' Table.
Public Class testcreation1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Session.Item("User Type") <> "Teacher" Then
Response.Redirect("/")
End If
End Sub
Protected Sub QuestionCompiler_Click(sender As Object, e As EventArgs) Handles QuestionCompiler.Click
Dim allCheckedRows = From row In CreateTest.Rows.Cast(Of GridViewRow)()
Let chkQuestion = DirectCast(row.FindControl("QuestionSelector"), CheckBox)
Where chkQuestion.Checked
Select row
For Each checkedRow As GridViewRow In allCheckedRows
' implement the save function for this row '
Dim QuestionID As Int32 = Int32.Parse(checkedRow.Cells(0).Text)
' ... '
Next
End Sub
End Class
Source Code:
<h1>Compile Your Tests</h1>
<asp:GridView ID="CreateTest" runat="server"
AllowSorting="True" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="QuestionID" DataSourceID="SqlDataSource1" ForeColor="#333333"
GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:TemplateField HeaderText="Select">
<ItemTemplate>
<asp:CheckBox ID="QuestionSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID"
InsertVisible="False" ReadOnly="True" SortExpression="QuestionID" />
<asp:BoundField DataField="Answer" HeaderText="Answer"
SortExpression="Answer" />
<asp:BoundField DataField="Question" HeaderText="Question"
SortExpression="Question" />
<asp:BoundField DataField="SubjectID" HeaderText="SubjectID"
SortExpression="SubjectID" />
<asp:TemplateField></asp:TemplateField>
</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>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [QuestionID], [Answer], [Question], [SubjectID] FROM [Question]">
</asp:SqlDataSource>
<h5>Choose A Paper ID to compile your questions into</h5>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource3" DataTextField="PaperID" DataValueField="PaperID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource3" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>"
SelectCommand="SELECT [PaperID] FROM [ExamPaper]"></asp:SqlDataSource>
<asp:Button ID="QuestionCompiler" runat="server" Text="Compile Selected Questions" />
<h1>Preview Previous Tests</h1>
</asp:Content>
So as you can see from the code behind i've already had help with finding the rows of data, but how do I do the same with the checkbox in order to insert both of the QuestionID and PaperID into the QuestionInPaper table in my database. (The main problem is retrieving the value inside the checkbox.
Any help is appreciated!

Related

VB.NET checkbox problems

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

GridView will not display when button is clicked

I have an ASP.NET page I am writing in VB, and I am trying to only have the gridView show up when the button is clicked, and that it should display the product id, product name, supplier name, and quantity purchased for all products purchased by the customer (which comes from the drop down list). For some reason that I am unable to figure out, no matter what I do the gridView does not display. Ever. Any help would be greatly appreciated.
<%# Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT [CompanyName] FROM [Customers] ORDER BY [CompanyName]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Customers_1.CustomerID, Customers_1.CompanyName, (SELECT COUNT(Orders.OrderID) AS Expr1 FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID WHERE (Customers.CompanyName = #p1)) AS [Number of Orders], SUM([Order Details].UnitPrice * [Order Details].Quantity - [Order Details].Discount) AS [Total Cost of Order] FROM Customers AS Customers_1 INNER JOIN Orders AS Orders_1 ON Customers_1.CustomerID = Orders_1.CustomerID INNER JOIN [Order Details] ON Orders_1.OrderID = [Order Details].OrderID WHERE (Customers_1.CompanyName = #p1) GROUP BY Customers_1.CustomerID, Customers_1.CompanyName">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource3" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>" SelectCommand="SELECT Products.ProductID, Products.ProductName, Suppliers.CompanyName, [Order Details].Quantity FROM Orders INNER JOIN Customers ON Orders.CustomerID = Customers.CustomerID INNER JOIN [Order Details] ON Orders.OrderID = [Order Details].OrderID INNER JOIN Products ON [Order Details].ProductID = Products.ProductID INNER JOIN Suppliers ON Products.SupplierID = Suppliers.SupplierID WHERE (Customers.CustomerID = #p1)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="p1" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True" DataSourceID="SqlDataSource1" DataTextField="CompanyName" DataValueField="CompanyName">
<asp:ListItem>Select A Customer</asp:ListItem>
</asp:DropDownList>
<br />
<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="CustomerID" DataSourceID="SqlDataSource2" Height="50px" Width="125px">
<EditRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<Fields>
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" ReadOnly="True" SortExpression="CustomerID" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="Number of Orders" HeaderText="Number of Orders" ReadOnly="True" SortExpression="Number of Orders" />
<asp:BoundField DataField="Total Cost of Order" HeaderText="Total Cost of Order" ReadOnly="True" SortExpression="Total Cost of Order" />
</Fields>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
</asp:DetailsView>
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" BackColor="White" BorderColor="#CC9966" BorderStyle="None" BorderWidth="1px" CellPadding="4" DataKeyNames="ProductID" DataSourceID="SqlDataSource3" EmptyDataText="N/A" Visible="False">
<Columns>
<asp:BoundField DataField="ProductID" HeaderText="ProductID" InsertVisible="False" ReadOnly="True" SortExpression="ProductID" />
<asp:BoundField DataField="ProductName" HeaderText="ProductName" SortExpression="ProductName" />
<asp:BoundField DataField="CompanyName" HeaderText="CompanyName" SortExpression="CompanyName" />
<asp:BoundField DataField="Quantity" HeaderText="Quantity" SortExpression="Quantity" />
</Columns>
<FooterStyle BackColor="#FFFFCC" ForeColor="#330099" />
<HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="#FFFFCC" />
<PagerStyle BackColor="#FFFFCC" ForeColor="#330099" HorizontalAlign="Center" />
<RowStyle BackColor="White" ForeColor="#330099" />
<SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="#663399" />
<SortedAscendingCellStyle BackColor="#FEFCEB" />
<SortedAscendingHeaderStyle BackColor="#AF0101" />
<SortedDescendingCellStyle BackColor="#F6F0C0" />
<SortedDescendingHeaderStyle BackColor="#7E0000" />
</asp:GridView>
<br />
<asp:Button ID="Button1" runat="server" Text="Order Summary" />
</div>
</form>
</body>
</html>
And my code-behind page looks like this:
Partial Class _Default
Inherits System.Web.UI.Page
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
GridView1.Visible = True
GridView1.DataSourceID = "SqlDataSource3"
GridView1.
End Sub
End Class
Any help would be greatly appreciated
After setting the DataSource or DataSourceID property of the GridView you need to call DataBind() as a final step to bind your grid to the data.

ASP.NET/VB dropdownlist not properly populating gridview

I'm rather new to ASP.NET, but I have to create a .aspx form with VB that when selecting a customer from the ddl, it will populate gridview with the customer's orders from an Access database.
So far, my problem is that when I try and select any other customer than the first one, the gridview doesn't repopulate with new information. Rather it stays stuck on the first customer's information.
Here's what I have so far:
<%# Page Language="vb" AutoEventWireup="false" CodeBehind="WebForm1.aspx.vb" Inherits="Technical_Challenge.WebForm1" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Technical Challenge</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="Label1" runat="server" Text="Customers"></asp:Label>
<br />
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT [CustomerID], [CustomerName] FROM [Customers] ORDER BY [CustomerName]"></asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:TechChallengeConnectionString %>" ProviderName="<%$ ConnectionStrings:TechChallengeConnectionString.ProviderName %>" SelectCommand="SELECT * FROM [Orders] WHERE ([CustomerID] = ?)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownList1" Name="CustomerID" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<br />
<asp:Label ID="CustomerOrdersLabel" runat="server" Text="Customer Orders"> </asp:Label>
<br />
<asp:GridView ID="GridView1" runat="server" AllowSorting="True" AutoGenerateColumns="False" CellPadding="4" DataKeyNames="OrderID" DataSourceID="SqlDataSource2" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="OrderID" HeaderText="OrderID" InsertVisible="False" ReadOnly="True" SortExpression="OrderID" />
<asp:BoundField DataField="CustomerID" HeaderText="CustomerID" SortExpression="CustomerID" />
<asp:BoundField DataField="OrderPrice" HeaderText="OrderPrice" SortExpression="OrderPrice" />
</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>
</div>
</form>
</body>
</html>
The code-behind:
Public Class WebForm1
Inherits System.Web.UI.Page
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
End Sub
Protected Sub SqlDataSource1_Selecting(sender As Object, e As SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
End Sub
End Class
Any help would be greatly appreciated. Thanks!
I don't see either AutoPostBack=True set in your dropdown list or a submit button that would send the data for processing? or you haven't copied that code?
You have to make changes in your code as per below :-
.aspx changes :-
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1" DataTextField="CustomerName" DataValueField="CustomerID" AutoPostBack="True">
</asp:DropDownList>
The Code-Behind :-
Protected Sub DropDownList1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DropDownList1.SelectedIndexChanged
// write your query to select data from database
// bind with grid view
End Sub

two components with the same id can't be added to the application

Hello I am having problems with 2 popupcontrol extenders. They are used in conjuction with a webservice to display the detail information from a database. Each extender gets information a from 2 different tables. The extenders both work fine except when they have run across a row that has the same primary key number. Here is what the code is (I removed most of the info) to hopefully make it faster to view:
<cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" CssClass="ajax__myTab" Width="100%" ScrollBars="Horizontal">
<cc1:TabPanel ID="TabPanel5" runat="server" HeaderText="" Enabled="true">
<HeaderTemplate>Agent Info </HeaderTemplate>
<ContentTemplate>
<asp:GridView ID="GridViewClosedProducts" runat="server" AutoGenerateColumns="False"
BackColor="White" BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px"
CellPadding="5" CellSpacing="1" DataKeyNames="Pkey"
DataSourceID="SqlDataSourceGRClosedProds" ForeColor="Black" GridLines="Vertical"
AllowSorting="True" PageSize="20">
<FooterStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" BorderStyle="None" ID="Image1" ImageUrl="Images\MagnifyingClass.gif" />
<cc1:PopupControlExtender ID="PopupControlExtender1" runat="server" PopupControlID="Panel1" TargetControlID="Image1" DynamicContextKey='<%# Eval("Pkey") %>' DynamicControlID="Panel1" DynamicServiceMethod="GetClosedComment" Position="right">
</cc1:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ItemTemplate>
<HeaderStyle VerticalAlign="Top" BorderWidth="0px" />
<ItemStyle BorderWidth="0px" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
<AlternatingRowStyle BackColor="#CCCCCC" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceGRClosedProds" runat="server"
ConnectionString="<%$ ConnectionStrings:EUCNET00722 %>"
SelectCommand="SELECT * FROM [tblClosedProducts] WHERE ([AgentMainPkey] = #AgentMainPkey) ORDER BY Product, DateClosed">
<SelectParameters>
<asp:ControlParameter ControlID="hdnPkey" Name="AgentMainPkey" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</cc1:TabPanel>
<cc1:TabPanel ID="TabPanel3" runat="server" HeaderText="" Enabled="true">
<HeaderTemplate>Agent Info </HeaderTemplate>
<ContentTemplate>
<asp:GridView ID="GridViewUpdates" runat="server" AutoGenerateColumns="False" BackColor="White"
BorderColor="#999999" BorderStyle="Solid" BorderWidth="1px" CellPadding="5"
CellSpacing="1" DataKeyNames="Pkey" DataSourceID="SqlDataSourceGRUpdates" ForeColor="Black"
GridLines="Vertical">
<AlternatingRowStyle BackColor="#CCCCCC" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:Image runat="server" BorderStyle="None" ID="Image1" ImageUrl="Images\MagnifyingClass.gif" />
<cc1:PopupControlExtender ID="PopupControlExtenderGVU" runat="server" PopupControlID="Panel1" TargetControlID="Image1" DynamicContextKey='<%# Eval("Pkey") %>' DynamicControlID="Panel1" DynamicServiceMethod="GetUpdateComment" Position="right">
</cc1:PopupControlExtender>
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</ItemTemplate>
<HeaderStyle VerticalAlign="Top" BorderWidth="0px" />
<ItemStyle BorderWidth="0px" HorizontalAlign="Center" />
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Silver" Font-Bold="True" ForeColor="Black" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSourceGRUpdates" runat="server" ConnectionString="<%$ ConnectionStrings:EUCNET00722 %>" SelectCommand="SELECT * FROM [tblComments] WHERE ([AgentMainPkey] = #AgentMainPkey) ORDER BY DateOfComment DESC" DeleteCommand="delete from tblComments where pkey = #pkey">
<SelectParameters>
<asp:ControlParameter ControlID="hdnPkey" Name="AgentMainPkey" PropertyName="Value" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</ContentTemplate>
</cc1:TabPanel>
</cc1:TabContainer>
** Code behind
Protected Sub GridViewClosedProducts_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewClosedProducts.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.DataItem Is Nothing Then
Dim pce As PopupControlExtender = TryCast(e.Row.FindControl("PopupControlExtender1"), PopupControlExtender)
Dim MyPkey As String = e.Row.DataItem("Pkey")
pce.BehaviorID = MyPkey
Dim img As Image = DirectCast(e.Row.FindControl("Image1"), Image)
Dim OnMouseOverScript As String = String.Format("$find('{0}').showPopup();", MyPkey)
Dim OnMouseOutScript As String = String.Format("$find('{0}').hidePopup();", MyPkey)
img.Attributes.Add("onmouseover", OnMouseOverScript)
img.Attributes.Add("onmouseout", OnMouseOutScript)
End If
End If
End Sub
Protected Sub GridViewUpdates_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridViewUpdates.RowCreated
If e.Row.RowType = DataControlRowType.DataRow Then
If Not e.Row.DataItem Is Nothing Then
Dim pceGVU As PopupControlExtender = TryCast(e.Row.FindControl("PopupControlExtenderGVU"), PopupControlExtender)
Dim MyPkey As String = e.Row.DataItem("Pkey")
pceGVU.BehaviorID = MyPkey
Dim img As Image = DirectCast(e.Row.FindControl("Image1"), Image)
Dim OnMouseOverScript As String = String.Format("$find('{0}').showPopup();", MyPkey)
Dim OnMouseOutScript As String = String.Format("$find('{0}').hidePopup();", MyPkey)
img.Attributes.Add("onmouseover", OnMouseOverScript)
img.Attributes.Add("onmouseout", OnMouseOutScript)
End If
End If
End Sub
This is how the code is rendered by IE
<img id="ctl00_ContentPlaceHolder2_TabContainer1_TabPanel5_GridViewClosedProducts_ctl10_Image1" onmouseover="$find('48').showPopup();" onmouseout="$find('48').hidePopup();" src="Images\MagnifyingClass.gif" style="border-style:None;border-width:0px;" />
<img id="ctl00_ContentPlaceHolder2_TabContainer1_TabPanel3_GridViewUpdates_ctl02_Image1" onmouseover="$find('48').showPopup();" onmouseout="$find('48').hidePopup();" src="Images\MagnifyingClass.gif" style="border-style:None;border-width:0px;" />
#Mike: If it's possible to store the previous row's Pkey value in a variable, then I'd do something like this --
Dim MyPkey As Integer = e.Row.DataItem("Pkey")
Dim MyStoredPkey As Integer
If MyPkey <> MyStoredPkey Then
MyStoredPkey = MyPkey
Else
MyPkey = MyPkey + 1
End If
pceGVU.BehaviorID = MyPkey
...to increment the Pkey should it be found in the stored variable.
Note: This is practically pseudo-code since I have very little ASP.NET experience.

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