Button click not recognized(?)

4 posts, 1 answers
  1. TSCGlobal
    TSCGlobal avatar
    43 posts
    Member since:
    Jan 2008

    Posted 06 Jul 2010 Link to this post

    Some context to this particular task;

    I have a news system I am updating with the ability to see comments and to post comment in a RadWindow.  So far, I have been able to get the "show comments" to work by using a combination of the "select" command and session variables, but when I try to wire up the other LinkButton to open another RadWindow to insert a new comment into the database, it seems to default to the code I have done for the selectedItem instead.  I'm hoping someone can help me figure this one out.

    the *.aspx:
    <!-- Windows Begin --> 
    <telerik:RadWindowManager ID="TrackWindowManager" runat="server" ShowContentDuringLoad="False">  
        <Windows> 
            <telerik:RadWindow runat="server" ID="CommentsWindow" 
                VisibleOnPageLoad="false" 
                Title="Comments Window" 
                Width="600" Height="500" 
                NavigateUrl="~/NewsComments.aspx" /> 
            <telerik:RadWindow runat="server" ID="PostCommentWindow" 
                VisibleOnPageLoad="false" 
                Title="Post Comments" 
                Width="600" Height="500" 
                NavigateUrl="~/PostNewsComment.aspx" /> 
        </Windows> 
    </telerik:RadWindowManager> 
    <!-- Windows End --> 
     
    <!-- RadOpen Javascript begins --> 
    <script type="text/javascript">  
        function openRadWin()   
            {  
                radopen(null, "CommentsWindow");  
            }                                                                        
    </script> 
    <!-- RadOpen Javascript ends --> 
     
    <telerik:RadListView ID="RadListView1" runat="server"   
        DataSourceID="dsNews" 
        DataKeyNames="NewsID">  
        <ItemTemplate> 
            <table cellpadding="0" cellspacing="0" class="NewsTable">  
                <tr> 
                    <td class="NewsHeader">  
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>' />&nbsp;-  
                        <asp:Label ID="lblDate" runat="server" Text='<%# Eval("ActDate") %>' /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="NewsContent">  
                        <asp:Label ID="lblStory" runat="server" Text='<%# Eval("NewsContent") %>' /> 
                        <asp:Label ID="lblNewsID" runat="server" Text='<%# Eval("NewsID") %>' Visible="false" /> 
                    </td> 
                </tr> 
                <tr> 
                    <td> 
                        <asp:LinkButton ID="lbShowComments" runat="server"   
                            Text="Show Comments" 
                            CommandName="Select" />&nbsp;  
                        <asp:LinkButton ID="lbPostComment" runat="server" 
                            CommandName="PostComment" 
                            Text="Post Comment" /> 
                    </td> 
                </tr> 
            </table>          
        </ItemTemplate> 
        <SelectedItemTemplate> 
            <table cellpadding="0" cellspacing="0" class="NewsTable">  
                <tr> 
                    <td class="NewsHeader">  
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>' />&nbsp;-  
                        <asp:Label ID="lblDate" runat="server" Text='<%# Eval("ActDate") %>' /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="NewsContent">  
                        <asp:Label ID="lblStory" runat="server" Text='<%# Eval("NewsContent") %>' /> 
                        <asp:Label ID="lblNewsID" runat="server" Text='<%# Eval("NewsID") %>' Visible="false" /> 
                    </td> 
                </tr> 
                <tr> 
                    <td> 
                        <asp:LinkButton ID="lbShowComments" runat="server"   
                            Text="Show Comments" 
                            CommandName="Select" />&nbsp;  
                        <asp:LinkButton ID="lbPostComment" runat="server" 
                            CommandName="PostComment" 
                            Text="Post Comment" /> 
                    </td> 
                </tr> 
            </table>     
        </SelectedItemTemplate> 
    </telerik:RadListView> 

    and here's the VB.NET code:
        Private Sub RadListView1_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadListView1.SelectedIndexChanged  
     
            Dim NewsIdent As Label  
     
            NewsIdent = RadListView1.SelectedItems(0).FindControl("lblNewsID")  
            Session.Add("NewsID", NewsIdent.Text)  
            Me.CommentsWindow.VisibleOnPageLoad = True 
     
        End Sub 
     
        Sub PostComment()  
     
            Dim NewsIdent As Label  
     
            NewsIdent = RadListView1.SelectedItems(0).FindControl("lblNewsID")  
            Session.Add("NewsID", NewsIdent.Text)  
            PostCommentWindow.VisibleOnPageLoad = True 
     
        End Sub 
  2. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 07 Jul 2010 Link to this post

    Hi,

    In order to handle custom commands you should hook to ItemCommand event of RadListView and add the appropriate logic there.

    Regards,
    Rosen
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. TSCGlobal
    TSCGlobal avatar
    43 posts
    Member since:
    Jan 2008

    Posted 07 Jul 2010 Link to this post

    Okay, so I changed the Listview as follows;

    <telerik:RadListView ID="RadListView1" runat="server"   
        DataSourceID="dsNews" 
        DataKeyNames="NewsID">  
        <ItemTemplate> 
            <table cellpadding="0" cellspacing="0" class="NewsTable">  
                <tr> 
                    <td class="NewsHeader">  
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>' />&nbsp;-  
                        <asp:Label ID="lblDate" runat="server" Text='<%# Eval("ActDate") %>' /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="NewsContent">  
                        <asp:Label ID="lblStory" runat="server" Text='<%# Eval("NewsContent") %>' /> 
                        <asp:Label ID="lblNewsID" runat="server" Text='<%# Eval("NewsID") %>' Visible="false" /> 
                    </td> 
                </tr> 
                <tr> 
                    <td> 
                        <asp:LinkButton ID="lbShowComments" runat="server" 
                            Text="Show Comments" 
                            CommandName="Select" />&nbsp;  
                        <asp:LinkButton ID="lbPostComment" runat="server" 
                            OnClick="PostComment" 
                            Text="Post Comment" /> 
                    </td> 
                </tr> 
            </table>          
        </ItemTemplate> 
        <SelectedItemTemplate> 
            <table cellpadding="0" cellspacing="0" class="NewsTable">  
                <tr> 
                    <td class="NewsHeader">  
                        <asp:Label ID="lblTitle" runat="server" Text='<%# Eval("Title") %>' />&nbsp;-  
                        <asp:Label ID="lblDate" runat="server" Text='<%# Eval("ActDate") %>' /> 
                    </td> 
                </tr> 
                <tr> 
                    <td class="NewsContent">  
                        <asp:Label ID="lblStory" runat="server" Text='<%# Eval("NewsContent") %>' /> 
                        <asp:Label ID="lblNewsID" runat="server" Text='<%# Eval("NewsID") %>' Visible="false" /> 
                    </td> 
                </tr> 
                <tr> 
                    <td> 
                        <asp:LinkButton ID="lbShowComments" runat="server" 
                            Text="Show Comments" 
                            CommandName="Select" />&nbsp;  
                        <asp:LinkButton ID="lbPostComment" runat="server" 
                            CommandName="PostComment" 
                            Text="Post Comment" /> 
                    </td> 
                </tr> 
            </table>     
        </SelectedItemTemplate> 
    </telerik:RadListView> 

    and then I changed the VB.NET codebehind to this;
        Private Sub RadListView1_ItemCommand(ByVal sender As ObjectByVal e As Telerik.Web.UI.RadListViewCommandEventArgs) Handles RadListView1.ItemCommand  
     
            PostComment()  
     
        End Sub 
     
        Private Sub RadListView1_SelectedIndexChanged(ByVal sender As ObjectByVal e As System.EventArgs) Handles RadListView1.SelectedIndexChanged  
     
            PostComment()  
     
        End Sub 
     
        Sub PostComment()  
     
            Dim NewsIdent As Label  
     
            NewsIdent = RadListView1.SelectedItems(0).FindControl("lblNewsID")  
            Session.Add("NewsID", NewsIdent.Text)  
            PostCommentWindow.VisibleOnPageLoad = True 
     
        End Sub 

    BUT, when I click the "post comment" button, I get an "out of index" error.  I'm not certain why, but if I had to guess, I would guess that when I click the "Post Comment" button, that the item is not being selected.  Would this be a correct assumption?

    If so, how can I make sure that when I click the "Post Comment" button that item is selected?  Also, I wasn't sure if the ItemCommand event fired, or if it goes directly to PostComment sub, so I handled the ItemCommand event just to be on the safe side....
  5. Answer
    Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 09 Jul 2010 Link to this post

    Hi,

    You should modify the ItemCommand event handler in the following way:

    Protected Sub RadListView1_ItemCommand(ByVal sender As Object, ByVal e As Telerik.Web.UI.RadListViewCommandEventArgs) Handles RadListView1.ItemCommand
        If e.CommandName = "PostComment" Then
            DirectCast(e.ListViewItem, RadListViewDataItem).Selected = True
            PostComment()
            e.ListViewItem.OwnerListView.Rebind() ' needed in order the Item to enter Select mode
        End If
    End Sub

    And setting both the SelectedItemTemplate and ItemTemplate button's CommandName to PostComment.

    <asp:LinkButton ID="lbPostComment" runat="server"  
         CommandName="PostComment"  
         Text="Post Comment" />  

    Sincerely yours,
    Rosen
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
Back to Top