|
-
Aug 12th, 2012, 02:08 AM
#1
Thread Starter
New Member
[RESOLVED] Help with check boxes in a repeater
Hi
I am trying to use a check box within a repeater in an asp.net vb application. Basically, i am have an application that allows users to create blog posts and then other users can comment on them. I am using a check box to provide the user with functionality to like the posts comments.
When the user clicks on like, it is supposed to fire the event which will insert or update the users like record. However, this even does not seem to be firing.
i set up the check box within a table within the repeater as follows:
Code:
<td class = "LikeCellRepeat">
Like: <asp:CheckBox ID="chkLikeComment"
runat="server"
ToolTip="Like"
AutoPostBack="True"
OnCheckedChanged="chkLikeComment_CheckedChanged"/>
<asp:ToggleButtonExtender ID="Toggle_chkLikeComment" runat="server"
Enabled="True"
TargetControlID="chkLikeComment"
CheckedImageAlternateText="Liked"
CheckedImageUrl="~/Images/Liked.png"
UncheckedImageUrl="~/Images/Unselected.png"
ImageHeight="19"
ImageWidth="19">
</asp:ToggleButtonExtender>
</td>
and here is the check changed event:
Code:
Protected Sub chkLikeComment_CheckedChanged(ByVal sender As Object, ByVal e As System.EventArgs)
MsgBox("like clicked")
Dim CommentItem As RepeaterItem = sender.NamingContainer
Dim HiddenID As HiddenField = CType(CommentItem.FindControl("HiddenCommentID"), HiddenField)
Dim CommentID As Integer = Convert.ToInt32(HiddenID.Value)
Dim isCommentLiked As Boolean = ExistingCommentLike(CommentID)
Dim isCommentDisliked As Boolean = ExistingCommentDislike(CommentID)
If (isCommentLiked = False And isCommentDisliked = False) Then
'user did not previously like or dislike comment, so insert record'
AddUserCommentLike(DirectCast(sender, CheckBox).Checked, CommentID)
Else
'user previously liked or disliked comment, so update record'
UpdateUserCommentLike(DirectCast(sender, CheckBox).Checked, CommentID)
End If
'Display how many likes and dislikes there are on this comment'
DisplayCommentLikesAndDislikes()
End Sub
i included a Msgbox in the event to see if i would at least get that and then the problem might be something else, but i don't even get the msgbox to pop up which is why i'm thinking the check changed event is not even firing.
I've been searching the net for solutions on how to get it to fire, but what i've found so far either does not work or i am not using it properly. I saw something about re-subscribing to the event in repeater's on item data bound, and tried to set up that the way they had, but i am not sure if i did it right, as i get an error that says the checkedchanged cannot be called directly and to use raise event instead...
Code:
Protected Sub rptComments_ItemDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.RepeaterItemEventArgs) Handles rptComments.ItemDataBound
Dim CheckLike As CheckBox = CType(e.Item.FindControl("chkLikeComment"), CheckBox)
CheckLike.AutoPostBack = True
CheckLike.CheckedChanged += New EventHandler(chkLikeComment_CheckedChanged)
End Sub
I have been trying to get this to work for about a week now with no luck. Would appreciate any help i can get...
Thanks!
Last edited by gep13; Aug 12th, 2012 at 08:54 AM.
Reason: Added Code Tags
-
Aug 12th, 2012, 08:48 AM
#2
Re: Help with check boxes in a repeater
Hello,
Since this question is about ASP.NET, I am going to move it to the ASP.NET Forum.
Gary
-
Aug 12th, 2012, 08:54 AM
#3
Re: Help with check boxes in a repeater
Hello Ameera,
Welcome to the forums!!
When you are posting code into the forum, can you please remember to surround it in [code][/code] or [highlight][/highlight] tags? It makes it a lot easier to read. I have done this for you in your above post.
Gary
-
Aug 12th, 2012, 08:58 AM
#4
Re: Help with check boxes in a repeater
Hello Ameera,
A couple of questions..
Are you testing this application by deploying it onto a Web Server, or are you running directly out of Visual Studio?
The reason that I ask is two fold...
If you are running directly out of Visual Studio, setting a breakpoint on the line of code that you are interested in, is a much easier and simpler way to test the code execution path, rather than using a MsgBox.
Secondly, if you are running the site deployed to a Web Server, then you are never going to see the MsgBox. It runs in the same process as the application is running, so you are never going to see it on the client machine.
Gary
-
Aug 12th, 2012, 12:49 PM
#5
Thread Starter
New Member
Re: Help with check boxes in a repeater
Hi Gary
Thanks...
I am running the app out of visual studio.
I tried using the break point by placing it within the chek change event but nothing happens. I am assuming maybe because the check event is not even firing within the repeater?
Should i try placing the break point elsewhere? Not sure where though..
Regards
Ameera
-
Aug 12th, 2012, 03:04 PM
#6
Re: Help with check boxes in a repeater
You need to re-subscribe to the events in every pageload for every dynamic created control
-
Aug 12th, 2012, 10:47 PM
#7
Thread Starter
New Member
Re: Help with check boxes in a repeater
 Originally Posted by Lightning
You need to re-subscribe to the events in every pageload for every dynamic created control
Thanks. i saw that while searching for help online but i am not sure how to do that.
This is the code for my page load
Code:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
If Not Page.IsPostBack Then
GetPostDetails()
DisplayPostRatings()
DisplayPostLikesAndDislikes()
End If
GetPostComments()
DisplayCommentRatings()
DisplayCommentLikesAndDislikes()
End Sub
the repeater data bind takes place in GetPostComments().
Thanks
Ameera
-
Aug 13th, 2012, 01:43 AM
#8
Re: Help with check boxes in a repeater
 Originally Posted by Ameera
Hi Gary
Thanks...
I am running the app out of visual studio.
I tried using the break point by placing it within the chek change event but nothing happens. I am assuming maybe because the check event is not even firing within the repeater?
Should i try placing the break point elsewhere? Not sure where though..
Regards
Ameera
Ah, ok, now we are getting somewhere...
In this scenario, you would need to attach Visual Studio "remotely" to the IIS instance in order to have your code stop on a particular breakpoint. This is known as remote debugging.
Here are the setup instructions for Visual Studio 2010:
http://msdn.microsoft.com/en-us/library/bt727f1t.aspx
Gary
-
Aug 13th, 2012, 01:44 AM
#9
Re: Help with check boxes in a repeater
 Originally Posted by Lightning
You need to re-subscribe to the events in every pageload for every dynamic created control
Unless I am missing something, I don't see a direct need to create the CheckBox dynamically, rather simply set the Checked property of the CheckBox that is already contained within the ItemTemplate.
Gary
-
Aug 13th, 2012, 06:34 AM
#10
Re: Help with check boxes in a repeater
Controls in a repeater are always dynamically created
-
Aug 13th, 2012, 12:09 PM
#11
Thread Starter
New Member
Re: Help with check boxes in a repeater
Hi
Thanks.. would anyone be able to guide me on resubscribe to the event in the page load? i am seeing this a lot online as the solution to the problem, but i don't know how to do that.
would it be something like:
Code:
ChkLikeComment.CheckedChanged += New EventHandler(chkLikeComment_CheckedChanged)
i tried doing that in the repeater item databound but it was underlined with an error stating that it is an event and cannot be called directly and to use raise event instead..
i also tried doing that in the page load but it does not recognise ChklikeComment becuase it is in the repeater and also underlined (chkLikeComment_CheckedChanged) wiht an error that says
"Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to it's constructor"
i am really lost as to how to get this check box to work within the repeater.. i need to get this done for a school project as soon as possible.... would appreciate any help i can get.
-
Aug 13th, 2012, 01:17 PM
#12
Re: Help with check boxes in a repeater
If you rebind the data in the pre-init event, you can foreach thru the items in the repeater. Using item.FindControl("chkLikeComment") you can get all the "likecommennt checkboxes" and add the eventhandlers
-
Aug 13th, 2012, 02:17 PM
#13
Re: Help with check boxes in a repeater
Depending on exactly what you are trying to achieve, simplifying this slightly might be the right approach. Here is one technique which doesn't require an individual event handler, but rather uses a common one:
http://www.codeproject.com/Articles/...ew-or-Repeater
Gary
-
Aug 13th, 2012, 02:19 PM
#14
Re: Help with check boxes in a repeater
 Originally Posted by Lightning
Controls in a repeater are always dynamically created
Doh! Good point well made
-
Aug 15th, 2012, 03:25 PM
#15
Thread Starter
New Member
Re: Help with check boxes in a repeater
 Originally Posted by gep13
Depending on exactly what you are trying to achieve, simplifying this slightly might be the right approach. Here is one technique which doesn't require an individual event handler, but rather uses a common one:
http://www.codeproject.com/Articles/...ew-or-Repeater
Gary
Hi
I don't think that will work for me as my problem is not getting the id of the item, but rather getting the change event to fire. I'm actually using the checkbox to indicate if a person likes a blog comment. The blog comments are stored in one table, the likes and dislikes in another. Upon page load, the repeater will display the comments, and will display if the logged in user already likes or dislikes each comment.
Hence some of the checkboxes will already, upon loading, have a status of checked if you've already liked or dislike that comment.
If he has not yet liked or disliked it, it will display as unchecked. i need to track each like by each user.... so everytime the like or dislike checkbox is clicked, it's supposed to check if you've already liked or disliked the comment and if not, insert a new record in the like table with the comment id, your user id and the store the like / dislike accordingly. If you have, it will find your record and update it, based on what you clicked.
So the key thing is that i need it to respond when the users click on it, wheter or not they are now adding a like / dislike or updating thier existing like / dislike. I went in the db and manually created a like entry for a user and it displays correctly when the page is loaded. The problem is just upon clicking on the checkbox... *sigh*
If you can think of a way i can do this, using the methods described in the link you provided, i will be gratefull as it does indeed seem a lot simpler.
-
Aug 15th, 2012, 03:32 PM
#16
Thread Starter
New Member
Re: Help with check boxes in a repeater
 Originally Posted by Lightning
If you rebind the data in the pre-init event, you can foreach thru the items in the repeater. Using item.FindControl("chkLikeComment") you can get all the "likecommennt checkboxes" and add the eventhandlers
Ok, so i tried to set up the pre-init as follows:
Code:
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.PreInit
rptComments.DataSource = GetPostComments()
rptComments.DataBind()
For Each CommentItem As RepeaterItem In rptComments.Items
Dim CheckLike As CheckBox = CType(CommentItem.FindControl("chkLikeComment"), CheckBox)
CheckLike.AutoPostBack = True
CheckLike.CheckedChanged += New EventHandler(chkLikeComment_CheckedChanged)
Next
End Sub
However, the statement
Code:
CheckLike.CheckedChanged += New EventHandler(chkLikeComment_CheckedChanged)
get blue underlined with an error in 2 places
CheckLike.CheckedChanged gets the error stating that it is an event and cannot be called directly and to use raise event instead..
chkLikeComment_CheckedChanged gets the error stating that "Delegate 'System.EventHandler' requires an 'AddressOf' expression or lambda expression as the only argument to it's constructor"
Am i adding the event handlers wrong? Or have i set up the preinit wrong in some way?
-
Aug 16th, 2012, 02:00 AM
#17
Re: Help with check boxes in a repeater
Hello,
Did you try the sample that I directed you to in that article?
From a very brief read, it would seem to do exactly what you want.
Gary
-
Aug 16th, 2012, 02:10 AM
#18
Re: Help with check boxes in a repeater
 Originally Posted by Ameera
Am i adding the event handlers wrong? Or have i set up the preinit wrong in some way?
Hey,
Looks like you are using the C# syntax, rather than the VB.NET Syntax.
Have a look at the documentation here:
http://msdn.microsoft.com/en-us/libr...edchanged.aspx
Gary
-
Aug 16th, 2012, 10:54 PM
#19
Thread Starter
New Member
-
Aug 17th, 2012, 06:40 AM
#20
Re: Help with check boxes in a repeater
Hello,
Glad to hear that you got it working!
For the benefit of other people who might have a similar issue, would it be possible for you to share your final working code?
Also, since your question is now answered, can you go back and mark your thread as resolved?
If you are not sure how to do this, you will find a link in my signature for help with this.
Gary
-
Aug 29th, 2012, 01:33 AM
#21
Thread Starter
New Member
Re: [RESOLVED] Help with check boxes in a repeater
Sure, I've marked it as resolved.
i don't mind sharing the final code, but it's school project that i have to pass through a plagiarism detector that searches online. I wouldn't want it to return a positive find on the code, so after i hand up the project on Sep. 10, i'll post the code then.
-
Aug 30th, 2012, 08:25 AM
#22
Re: [RESOLVED] Help with check boxes in a repeater
Not a problem at all.
Thanks for sharing. Hopefully you get a good mark!
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|