This is a migrated thread and some comments may be shown as answers.

Button click not recognized(?)

3 Answers 164 Views
ListView
This is a migrated thread and some comments may be shown as answers.
TSCGlobal
Top achievements
Rank 1
TSCGlobal asked on 06 Jul 2010, 08:18 PM
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 

3 Answers, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 07 Jul 2010, 07:35 AM
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
0
TSCGlobal
Top achievements
Rank 1
answered on 07 Jul 2010, 05:16 PM
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....
0
Accepted
Rosen
Telerik team
answered on 09 Jul 2010, 04:49 PM
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
Tags
ListView
Asked by
TSCGlobal
Top achievements
Rank 1
Answers by
Rosen
Telerik team
TSCGlobal
Top achievements
Rank 1
Share this question
or