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

Dynamically setting LinkButton within Listview

1 Answer 366 Views
ListView
This is a migrated thread and some comments may be shown as answers.
Adam
Top achievements
Rank 1
Adam asked on 04 Nov 2011, 08:01 PM
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"]);
       }
   }

1 Answer, 1 is accepted

Sort by
0
Tsvetoslav
Telerik team
answered on 09 Nov 2011, 12:59 PM
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
Tags
ListView
Asked by
Adam
Top achievements
Rank 1
Answers by
Tsvetoslav
Telerik team
Share this question
or