Dynamically setting LinkButton within Listview

2 posts, 0 answers
  1. Adam
    Adam avatar
    2 posts
    Member since:
    Jun 2011

    Posted 04 Nov 2011 Link to this post

    I have a RadListView and within the listview I have an asp.net linkbutton. I want to dynamically set the linkbutton's PostBackUrl. I can achieve this if I find the linkbutton control in the listview's ItemDataBound event. But by doing this when I end up clicking on the linkbutton the listview ItemCommand event is not being fired. I need the itemCommand even to be fired so I can take the datakeyvalue of the item clicked and set it into a session variable to pass to teh postbackurl page of the link button.

    <telerik:RadListView ID="lstVwWebpageList" runat="server" EnableViewState="true" ItemPlaceholderID="webpageContainer" AllowPaging="true" DataKeyNames="id" Skin="WebBlue" OnItemDataBound="lstVwWebpageList_ItemDataBound" OnPageIndexChanged="lstVwWebpageList_PageChange" Width="607px" OnItemCommand="lstVwWebpageList_ItemCommand">
        <LayoutTemplate>
            <table cellpadding="0" cellspacing="0">
                <tr>
                <td>
                    <asp:PlaceHolder ID="webpageContainer" runat="server" />
                </td>
                </tr>
                <tr>
                <td>
                <telerik:RadDataPager ID="Pager" runat="server" PagedControlID="lstVwWebpageList" PageSize="50" Skin="WebBlue">
                    <Fields>
                        <telerik:RadDataPagerButtonField FieldType="FirstPrev" />
                        <telerik:RadDataPagerButtonField FieldType="Numeric" />
                        <telerik:RadDataPagerButtonField FieldType="NextLast" />
                    </Fields>
                </telerik:RadDataPager>
                </td>
                </tr>
            </table>
        </LayoutTemplate>
         
        <ItemTemplate>
            <fieldset style="float: left; width: 300px;">
                <table cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <table cellpadding="0" cellspacing="0">
                                <tr>
                                    <td style="width: 25%;">
                                        <b>Name:</b>
                                    </td>
                                    <td style="width: 75%;">
                                        <asp:Label ID="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
                                    </td>
                                </tr>
                                <tr>
                                    <td style="width: 25%;">
                                        <b>Page:</b>
                                    </td>
                                    <td style="width: 75%;">
                                        <asp:LinkButton ID="pagenameLink" runat="server" CommandName="LinkClicked" Text='<%# Eval("pagename") %>'></asp:LinkButton>                                   
                                    </td>
                                </tr>
                            </table>
                        </td>
                    </tr>
                </table>
            </fieldset>
        </ItemTemplate>
    </telerik:RadListView>


    protected void lstVwWebpageList_ItemDataBound(object sender, RadListViewItemEventArgs e)
       {
           LinkButton link = (LinkButton)e.Item.FindControl("pagenameLink");
           Label lblname = (Label)e.Item.FindControl("lblName");
     
           link.PostBackUrl = "EditWebpage.aspx?name=" + lblname.Text + "&page=" + link.Text;
     
           //RadListViewDataItem item = (RadListViewDataItem)e.Item;
           //Session["linkID"] = item.DataItem;// Convert.ToInt32(item.OwnerListView.DataKeyValues[item.DisplayIndex]["id"]);
       }
     
       protected void lstVwWebpageList_ItemCommand(object sender, RadListViewCommandEventArgs e)
       {
           if (e.CommandName == "LinkClicked")
           {
               RadListViewDataItem item = (RadListViewDataItem)e.ListViewItem;
               Session["linkID"] = item.DataItem;// Convert.ToInt32(item.OwnerListView.DataKeyValues[item.DisplayIndex]["id"]);
           }
       }
  2. Tsvetoslav
    Admin
    Tsvetoslav avatar
    1823 posts

    Posted 09 Nov 2011 Link to this post

    Hi Adam,

    The Item command event is not fired because you are making a redirect to a different page. Just get the data key value in the ItemDataBound event and set it as a query string parameter in the url of the page which the list view is redirecting to.

    Hope it helps.

    All the best, Tsvetoslav
    the Telerik team
    If you want to get updates on new releases, tips and tricks and sneak peeks at our product labs directly from the developers working on the RadControls for ASP.NET AJAX, subscribe to their blog feed now
  3. UI for ASP.NET Ajax is Ready for VS 2017
Back to Top