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

Persist Datagrid bindable usercontrol properties

4 Answers 68 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Basel Nimer
Top achievements
Rank 2
Basel Nimer asked on 17 May 2010, 04:28 PM
Hello All,

i honestly tried to find if this have been asked before but to no avail.

i have a user control that is bind-able to inside a GridTemplateColumn of a datagrid, it works fine and it displays the data, the problem is when the grid is sorted for example, all the data is lost.

i think i can solve this using a hidden control inside the usercontrol, but i am not sure yet, and i would like to know if there is something i am missing


to reproduce:
create user control:
<%@ Control Language="VB" AutoEventWireup="false" CodeFile="X3.ascx.vb" Inherits="X3" %> 
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label> 
User control code behind:
 
Partial Class X3 
    Inherits System.Web.UI.UserControl 
    Private _X3 As String 
    Public Property X3() As String 
        Get 
            Return _X3 
        End Get 
        Set(ByVal value As String
            _X3 = value 
        End Set 
    End Property 
 
    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load 
        Label1.Text = _X3 
    End Sub 
End Class 
 


Page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="ControlState.aspx.vb" Inherits="ControlState" %> 
 
<%@ Register Assembly="Telerik.Web.UI" Namespace="Telerik.Web.UI" TagPrefix="telerik" %> 
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<%@ Register Src="X3.ascx" TagName="X3" TagPrefix="uc1" %> 
<html xmlns="http://www.w3.org/1999/xhtml" > 
<head runat="server"
    <title>Untitled Page</title> 
</head> 
<body> 
    <form id="form1" runat="server"
        <telerik:RadScriptManager ID="RadScriptManager1" runat="server"
        </telerik:RadScriptManager> 
    <div> 
        <telerik:RadGrid ID="RadGrid1" runat="server" GridLines="None" AllowSorting="True" AutoGenerateColumns="False" EnableViewState ="true" > 
            <MasterTableView> 
                <RowIndicatorColumn> 
                    <HeaderStyle Width="20px" /> 
                </RowIndicatorColumn> 
                <ExpandCollapseColumn> 
                    <HeaderStyle Width="20px" /> 
                </ExpandCollapseColumn> 
                <Columns> 
                    <telerik:GridBoundColumn UniqueName="X1" DataField="X1" HeaderText="X1"
                    </telerik:GridBoundColumn> 
                    <telerik:GridBoundColumn UniqueName="X2" DataField="X2" HeaderText="X2"
                    </telerik:GridBoundColumn> 
                    <telerik:GridTemplateColumn UniqueName="X3" DataField="X3" HeaderText="X3" SortExpression="X3"
                        <ItemTemplate> 
                            <uc1:X3 ID="X3_1" runat="server" X3='<%# Eval("X3") %>' /> 
                        </ItemTemplate> 
                    </telerik:GridTemplateColumn> 
                    <telerik:GridBoundColumn UniqueName="X4" DataField="X4" HeaderText="X4"
                    </telerik:GridBoundColumn> 
                </Columns> 
            </MasterTableView> 
         
        </telerik:RadGrid> 
    </div> 
    </form> 
</body> 
</html> 
 

Page code behind:
Imports Telerik.Web.UI 
Partial Class ControlState 
    Inherits System.Web.UI.Page 
 
    Private Sub RadGrid1_NeedDataSource(ByVal [source] As Object, _ 
             ByVal e As GridNeedDataSourceEventArgs) _ 
             Handles RadGrid1.NeedDataSource 
        Dim list As New ArrayList 
        For i As Integer = 0 To 4 
            Dim v As New D 
            v.X1 = "V" & i.ToString & "_X1" 
            v.X2 = "V" & i.ToString & "_X2" 
            v.X3 = "V" & i.ToString & "_X3" 
            v.X4 = "V" & i.ToString & "_X4" 
            list.Add(v) 
        Next 
 
 
        RadGrid1.DataSource = list 
    End Sub 
    Private Class D 
        Private _X1 As String 
        Public Property X1() As String 
            Get 
                Return _X1 
            End Get 
            Set(ByVal value As String
                _X1 = value 
            End Set 
        End Property 
        Private _X2 As String 
        Public Property X2() As String 
            Get 
                Return _X2 
            End Get 
            Set(ByVal value As String
                _X2 = value 
            End Set 
        End Property 
        Private _X3 As String 
        Public Property X3() As String 
            Get 
                Return _X3 
            End Get 
            Set(ByVal value As String
                _X3 = value 
            End Set 
        End Property 
        Private _X4 As String 
        Public Property X4() As String 
            Get 
                Return _X4 
            End Get 
            Set(ByVal value As String
                _X4 = value 
            End Set 
        End Property 
 
    End Class 
End Class 
 



4 Answers, 1 is accepted

Sort by
0
Nikolay Rusev
Telerik team
answered on 20 May 2010, 08:29 AM
Hello Basel,

One possible solution could be to change the property of the user control to look as follow:
01.Partial Class X3
02.    Inherits System.Web.UI.UserControl
03.  
04.    Public Property X3() As String
05.        Get
06.            Return Label1.Text
07.        End Get
08.        Set(ByVal value As String)
09.            Label1.Text = value
10.        End Set
11.    End Property
12.  
13.    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load        
14.        'Label1.Text = X3
15.    End Sub
16.  
17.End Class


Regards,
Nikolay
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
Basel Nimer
Top achievements
Rank 2
answered on 20 May 2010, 09:49 AM
hello Nikolay
well, eventually that's what i used.

but i was wondering why this happened?

0
Nikolay Rusev
Telerik team
answered on 21 May 2010, 03:17 PM
Hello Basel,

The behavior is due to lifecycle of the controls. The property in the user control is set only on Databinding. However on postback RadGrid won't bind before Page.Load and the value will remain empty.

Event more elegant solution could be if you handle DataBinding event and then set the value for the Label control.
01.Partial Class X3
02.    Inherits System.Web.UI.UserControl
03.  
04.    Private _X3 As String
05.    Public Property X3() As String
06.        Get
07.            Return _X3
08.        End Get
09.        Set(ByVal value As String)
10.            _X3 = value
11.        End Set
12.    End Property
13.  
14.    Protected Sub LabelBinding(ByVal sender As Object, ByVal e As System.EventArgs) Handles Label1.DataBinding
15.        Label1.Text = X3
16.    End Sub
17.End Class


Greetings,
Nikolay
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
Basel Nimer
Top achievements
Rank 2
answered on 21 May 2010, 11:44 PM
Thank you!

Tags
Grid
Asked by
Basel Nimer
Top achievements
Rank 2
Answers by
Nikolay Rusev
Telerik team
Basel Nimer
Top achievements
Rank 2
Share this question
or