Postback actual state on Load_Page event

3 posts, 0 answers
  1. zDog
    zDog avatar
    2 posts
    Member since:
    May 2014

    Posted 29 May 2014 Link to this post

    Good day!

    In the code snippet below I'm always getting RadListView1.SelectedValue from previous ViewState at Page_Load handler.
    For example, when I press "3" in the listview, I get "" in LabelRadListView1SelectedValue. Next time I press "5" and get "3" and so on.

    Why "RadListView1.SelectedValue" isn't actual at Page_Load event on PostBack?
    For example, It's going well at Page_LoadComplete event. So why RadListView1 restored from ViewState after Page_Load event?
    I'm confused. Please, help!

    RadListVew.aspx:
    <%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="RadListView.aspx.cs" Inherits="RadListV" %>
     
    <%@ Import Namespace="Telerik.Web.UI" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
        <telerik:RadListView runat="server" ID="RadListView1" ItemPlaceholderID="PlaceHolderListContainer" DataKeyNames="data">
            <LayoutTemplate>
                <asp:PlaceHolder ID="PlaceHolderListContainer" runat="server"></asp:PlaceHolder>
            </LayoutTemplate>
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">
                    <asp:Label runat="server" ID="Label1" Text='<%#Eval("data")%>' />
                </asp:LinkButton>
            </ItemTemplate>
            <SelectedItemTemplate>
                <span style="font-weight: bold;">
                    <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select">
                        <asp:Label runat="server" ID="Label1" Text='<%#Eval("data")%>' />
                    </asp:LinkButton>
                </span>
            </SelectedItemTemplate>
        </telerik:RadListView>
     
        <div>
            <asp:Label runat="server" ID="LabelRadListView1SelectedValue" />
        </div>
    </asp:Content>


    RadListVew.aspx.cs:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using Telerik.Web.UI;
     
    public partial class RadListV : System.Web.UI.Page
    {
        class ListItem
        {
            public int data { get; set; }
        }
     
        List<ListItem> ListDataSource
        {
            get
            {
                return (List<ListItem>)Session["ListDataSource"];
            }
     
            set { Session["ListDataSource"] = value; }
        }
     
        protected void Page_Load(object sender, EventArgs e)
        {
            if (RadListView1.SelectedValue != null)
            LabelRadListView1SelectedValue.Text = RadListView1.SelectedValue.ToString();
        }
     
        protected void Page_Init(object sender, EventArgs e)
        {
            ListDataSource = new List<ListItem>();
            for (int i = 1; i < 5; i++)
            {
                ListDataSource.Add(new ListItem { data = i });
            }
            RadListView1.DataSource = ListDataSource;
        }
    }




  2. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 29 May 2014 in reply to zDog Link to this post

    Hi,

    As a suggestion please try to attach the OnSelectedIndexChanged event of RadListView  to access the SelectedValue.

    C#:
    protected void RadListView1_SelectedIndexChanged(object sender, EventArgs e)
    {
        LabelRadListView1SelectedValue.Text = RadListView1.SelectedValue.ToString();
    }

    Thanks,
    Princy.
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. zDog
    zDog avatar
    2 posts
    Member since:
    May 2014

    Posted 29 May 2014 in reply to Princy Link to this post

    It's works well too.
Back to Top