The outer listview is LVPaths, and the inner listview is LVSkills. I have a checkbox in LVSkills that is enabled and checkable without having to go into the edit mode. I need to write code for the OnCheckChanged event.
Sub Check_Clicked(sender As Object, e As EventArgs)
Dim test As String = ""
End Sub
Checking the checkbox does not run the Check_Clicked code. I've tried this with and without the OnCheckedChanged="Check_Clicked" property. What am I doing wrong?
Thank you SohailShaikh, but that isn't working. The code in LVSkills_ItemCommand doesn't run. I tried adding a handler (AddHandler LVSkills.ItemCommand, AddressOf LVSkills_ItemCommand) to the outer listview, but that had no effect.
please remove your code behind event AddHandler chkSkill.CheckedChanged, AddressOf Check_Clicked
Then use my first reply code or if its not working again then
put your check box control out of your all listview then create event OnCheckedChanged and then reput you button into your lvskills and debug and please remove your itemdatabound its work for me
I get an error. Handles clause requires a WithEvents variable defined in the containing type or one of its base types. is triggered by Protected Sub ChkSkill_CheckedChanged(sender As Object, e As System.EventArgs) Handles ChkSkill.CheckedChanged
I don't understand how moving the code to the ItemCreated event will help. What I need is to write code for the checkchanged event of the checkboxes in the inner listview. I'm not allowing insert in the inner listview, and I'm not allowing edits either.
The chkbox can be checked at any time, and I need to write code for when the check box is checked.
For events to fire properly, the Controls need to be in the Page at the right time.
On PostBack, ItemDataBound doesn't fire. So the CheckBoxes that need to process the CheckChanged events won't be there.
RowCreated always fires. So using that instead of RowDataBound will at least make the CheckChanged events fire.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
LVPaths is the outer listview, and LVSkills is the inner listview. In my code behind, I set LVPaths.DataSource in the load event. The rest of the listview code is below. I need to know how to write code against the check-changed event of the checkboxes
in the inner listview. Note the checkboxes are enabled, there is no editing the inner listview records.
Protected Sub LVPaths_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles LVPaths.ItemDataBound
Dim LVSkills As ListView = CType(e.Item.FindControl("LVSkills"), ListView)
Dim pathIDLabel As Label = CType(e.Item.FindControl("pathIDLabel"), Label)
Dim skillLevelLabel As Label = CType(e.Item.FindControl("skillLevelLabel"), Label)
Dim context As New CRO6Entities
AddHandler LVSkills.ItemDataBound, AddressOf LVSkills_ItemDataBound
Dim skill = From s In context.Skills
Where s.pathID = pathIDLabel.Text And s.skillLevel = skillLevelLabel.Text
Order By s.name
Select s
LVSkills.DataSource = skill
LVSkills.DataBind()
End Sub
Private Sub LVSkills_ItemDataBound(sender As Object, e As ListViewItemEventArgs)
Dim context As New CRO6Entities
Dim skillIDLabel As Label = CType(e.Item.FindControl("skillIDLabel"), Label)
Dim nameLabel As Label = CType(e.Item.FindControl("nameLabel"), Label)
Dim chkSkill As CheckBox = CType(e.Item.FindControl("chkSkill"), CheckBox)
Dim bAllowed As Boolean = True
If chkSkill.Checked = True Then
Dim SkillID As Int16 = chkSkill.ID
End If
Dim s = (From skill In context.Skills
Where skill.skillID = skillIDLabel.Text
Select skill).FirstOrDefault
'see if the character already has this skill
Dim ck = (From k In Context.CharacterSkills
Where k.skillID = s.skillID
Select k).FirstOrDefault
If ck IsNot Nothing Then
chkSkill.Checked = True
chkSkill.Enabled = False
Else
'see if this character has the prereqs to select this skill
If s.reqLevel > 0 Then
Dim chkLevel = (From k In context.CharacterSkills
Where k.Skill.skillLevel = s.reqLevel
Select k).FirstOrDefault
If chkLevel Is Nothing Then
bAllowed = False
End If
End If
If s.minLevels > 0 Then
Dim chkMinLevel = From k In context.CharacterSkills
Where k.Skill.skillLevel <= s.skillLevel
Select k
If chkMinLevel.Count < s.minLevels Then
bAllowed = False
End If
End If
If s.reqSkill <> "None" Then
Dim chkRequ = (From k In context.CharacterSkills
Where k.Skill.name = s.reqSkill
Select k).FirstOrDefault
If chkRequ Is Nothing Then
bAllowed = False
End If
End If
Dim chkCost = (From k In context.Characters
Where k.characterID = cID
Select k).FirstOrDefault
If chkCost.UXP < s.cost Then
bAllowed = False
End If
If bAllowed = True Then
chkSkill.Enabled = True
chkSkill.ID = ""
nameLabel.Text = "<a href='character.aspx?cid=" & Request.QueryString("cid") & "&sid=" & s.skillID & "'>" & nameLabel.Text & " </a>"
Else
nameLabel.ForeColor = Drawing.Color.Gray
chkSkill.Checked = False
chkSkill.Enabled = False
End If
End If
End Sub
Setting the CheckChanged handler in .aspx like you did in an earlier post should work.
I see you say you set DataSource in Load. Do you also call DataBind there? If so, do you check IsPostBack?
A DataBind on PostBack can muck up event processing.
Superguppie.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Mainship
Participant
864 Points
2037 Posts
Problem with checkbox in nested listview control
Aug 07, 2012 05:51 PM|LINK
The outer listview is LVPaths, and the inner listview is LVSkills. I have a checkbox in LVSkills that is enabled and checkable without having to go into the edit mode. I need to write code for the OnCheckChanged event.
In LVSkills_ItemDataBound I added a handler:
AddHandler chkSkill.CheckedChanged, AddressOf Check_Clicked
and the code that creates the textbox in LVSkills is:
<asp:CheckBox ID="chkSkill" runat="server" AutoPostBack="true" OnCheckedChanged="Check_Clicked" />
I also have:
Sub Check_Clicked(sender As Object, e As EventArgs)
Dim test As String = ""
End Sub
Checking the checkbox does not run the Check_Clicked code. I've tried this with and without the OnCheckedChanged="Check_Clicked" property. What am I doing wrong?
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: Problem with checkbox in nested listview control
Aug 07, 2012 06:10 PM|LINK
i think you have to use item command event
<asp:CheckBox ID="chkSkill" runat="server" CommandName="Select" />if(chk.Checked ){do your code here}Make as your Answer if you think its helped youSohail ShaikhSohail Shaikh
Mainship
Participant
864 Points
2037 Posts
Re: Problem with checkbox in nested listview control
Aug 07, 2012 06:37 PM|LINK
Thank you SohailShaikh, but that isn't working. The code in LVSkills_ItemCommand doesn't run. I tried adding a handler (AddHandler LVSkills.ItemCommand, AddressOf LVSkills_ItemCommand) to the outer listview, but that had no effect.
SohailShaikh
Contributor
6119 Points
1167 Posts
Re: Problem with checkbox in nested listview control
Aug 07, 2012 06:45 PM|LINK
please remove your code behind event AddHandler chkSkill.CheckedChanged, AddressOf Check_Clicked
Then use my first reply code or if its not working again then
put your check box control out of your all listview then create event OnCheckedChanged and then reput you button into your lvskills and debug and please remove your itemdatabound its work for me
Sohail Shaikh
Mainship
Participant
864 Points
2037 Posts
Re: Problem with checkbox in nested listview control
Aug 07, 2012 07:39 PM|LINK
I get an error. Handles clause requires a WithEvents variable defined in the containing type or one of its base types. is triggered by Protected Sub ChkSkill_CheckedChanged(sender As Object, e As System.EventArgs) Handles ChkSkill.CheckedChanged
Qin Dian Tan...
All-Star
113532 Points
12480 Posts
Microsoft
Re: Problem with checkbox in nested listview control
Aug 09, 2012 06:18 AM|LINK
Hi,
Try to remove the code: AddHandler chkSkill.CheckedChanged, AddressOf Check_Clicked in ItemDataBound event. Then see if it works.
If not, try to move this code to ItemCreated event of ListView instead.
Thanks,
If you have any feedback about my replies, please contactmsdnmg@microsoft.com.
Microsoft One Code Framework
Mainship
Participant
864 Points
2037 Posts
Re: Problem with checkbox in nested listview control
Aug 09, 2012 06:21 PM|LINK
I did remove it.
I don't understand how moving the code to the ItemCreated event will help. What I need is to write code for the checkchanged event of the checkboxes in the inner listview. I'm not allowing insert in the inner listview, and I'm not allowing edits either. The chkbox can be checked at any time, and I need to write code for when the check box is checked.
superguppie
All-Star
48225 Points
8679 Posts
Re: Problem with checkbox in nested listview control
Aug 10, 2012 02:55 PM|LINK
For events to fire properly, the Controls need to be in the Page at the right time.
On PostBack, ItemDataBound doesn't fire. So the CheckBoxes that need to process the CheckChanged events won't be there.
RowCreated always fires. So using that instead of RowDataBound will at least make the CheckChanged events fire.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.
Mainship
Participant
864 Points
2037 Posts
Re: Problem with checkbox in nested listview control
Aug 10, 2012 08:37 PM|LINK
What you're saying makes sense, but I still don't know how to put it together. Here's my listview code:
<div id="skills">
<asp:ListView ID="LVPaths" runat="server" DataKeyNames="PathID">
<EmptyDataTemplate>
<span>No paths were found. Please contact a GM.</span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="">
<asp:Label ID="pathIDLabel" runat="server" Text='<%# Eval("pathID") %>' visible="false" />
<h2><asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /></h2>
<h3>Level: <asp:Label ID="skillLevelLabel" runat="server" Text='<%# Eval("skillLevel") %>' /></h3>
<asp:Label ID="lblSkills" runat="server" />
<asp:ListView ID="LVSkills" runat="server" DataKeyNames="skillID">
<EmptyDataTemplate>
<span>No data was returned.</span>
</EmptyDataTemplate>
<ItemTemplate>
<span style="float:left; padding:0 10px;">
<asp:CheckBox ID="chkSkill" runat="server" AutoPostBack="true" CommandName="Select" CommandArgument='<%# Eval("skillID") %>' visible="false" />
<asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' />
<asp:Label ID="skillIDLabel" runat="server" Text='<%# Eval("skillID") %>' Visible="false" />
<br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div ID="itemPlaceholderContainer" runat="server" style="">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
<br /><br />
</span>
</ItemTemplate>
<LayoutTemplate>
<div style="" ID="itemPlaceholderContainer" runat="server">
<span runat="server" id="itemPlaceholder" />
</div>
<div style="">
</div>
</LayoutTemplate>
</asp:ListView>
</div>
LVPaths is the outer listview, and LVSkills is the inner listview. In my code behind, I set LVPaths.DataSource in the load event. The rest of the listview code is below. I need to know how to write code against the check-changed event of the checkboxes in the inner listview. Note the checkboxes are enabled, there is no editing the inner listview records.
Protected Sub LVPaths_ItemDataBound(sender As Object, e As System.Web.UI.WebControls.ListViewItemEventArgs) Handles LVPaths.ItemDataBound
Dim LVSkills As ListView = CType(e.Item.FindControl("LVSkills"), ListView)
Dim pathIDLabel As Label = CType(e.Item.FindControl("pathIDLabel"), Label)
Dim skillLevelLabel As Label = CType(e.Item.FindControl("skillLevelLabel"), Label)
Dim context As New CRO6Entities
AddHandler LVSkills.ItemDataBound, AddressOf LVSkills_ItemDataBound
lblSavePath.Text = pathIDLabel.Text
lblSaveLevel.Text = skillLevelLabel.Text
Dim skill = From s In context.Skills
Where s.pathID = pathIDLabel.Text And s.skillLevel = skillLevelLabel.Text
Order By s.name
Select s
LVSkills.DataSource = skill
LVSkills.DataBind()
End Sub
Private Sub LVSkills_ItemDataBound(sender As Object, e As ListViewItemEventArgs)
Dim context As New CRO6Entities
Dim skillIDLabel As Label = CType(e.Item.FindControl("skillIDLabel"), Label)
Dim nameLabel As Label = CType(e.Item.FindControl("nameLabel"), Label)
Dim chkSkill As CheckBox = CType(e.Item.FindControl("chkSkill"), CheckBox)
Dim bAllowed As Boolean = True
If chkSkill.Checked = True Then
Dim SkillID As Int16 = chkSkill.ID
End If
Dim s = (From skill In context.Skills
Where skill.skillID = skillIDLabel.Text
Select skill).FirstOrDefault
'see if the character already has this skill
Dim ck = (From k In Context.CharacterSkills
Where k.skillID = s.skillID
Select k).FirstOrDefault
If ck IsNot Nothing Then
chkSkill.Checked = True
chkSkill.Enabled = False
Else
'see if this character has the prereqs to select this skill
If s.reqLevel > 0 Then
Dim chkLevel = (From k In context.CharacterSkills
Where k.Skill.skillLevel = s.reqLevel
Select k).FirstOrDefault
If chkLevel Is Nothing Then
bAllowed = False
End If
End If
If s.minLevels > 0 Then
Dim chkMinLevel = From k In context.CharacterSkills
Where k.Skill.skillLevel <= s.skillLevel
Select k
If chkMinLevel.Count < s.minLevels Then
bAllowed = False
End If
End If
If s.reqSkill <> "None" Then
Dim chkRequ = (From k In context.CharacterSkills
Where k.Skill.name = s.reqSkill
Select k).FirstOrDefault
If chkRequ Is Nothing Then
bAllowed = False
End If
End If
Dim chkCost = (From k In context.Characters
Where k.characterID = cID
Select k).FirstOrDefault
If chkCost.UXP < s.cost Then
bAllowed = False
End If
If bAllowed = True Then
chkSkill.Enabled = True
chkSkill.ID = ""
nameLabel.Text = "<a href='character.aspx?cid=" & Request.QueryString("cid") & "&sid=" & s.skillID & "'>" & nameLabel.Text & " </a>"
Else
nameLabel.ForeColor = Drawing.Color.Gray
chkSkill.Checked = False
chkSkill.Enabled = False
End If
End If
End Sub
superguppie
All-Star
48225 Points
8679 Posts
Re: Problem with checkbox in nested listview control
Aug 13, 2012 02:49 PM|LINK
Setting the CheckChanged handler in .aspx like you did in an earlier post should work.
I see you say you set DataSource in Load. Do you also call DataBind there? If so, do you check IsPostBack?
A DataBind on PostBack can muck up event processing.
Please remember to click “Mark as Answer” on the post that helps you. This can be beneficial to other community members reading the thread.
When all you've got is a Hammer, Every Problem looks like a Nail. Michael Swain.