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

Eval DateTime.Now in design time for TemplateColumn

3 Answers 407 Views
Grid
This is a migrated thread and some comments may be shown as answers.
alsi
Top achievements
Rank 1
alsi asked on 03 Nov 2008, 09:25 AM
Hi All!

I have a newbie question:
I have a radgrid and inside it I have templatecolumns.
One of it look like:
<telerik:GridTemplateColumn UniqueName="DateNow">
    <ItemTemplate>
        <asp:Literal runat="server" ID="litDateNow" />
    </ItemTemplate>
</telerik:GridTemplateColumn>

and want to evaluate the datetim.now somehow in the following way:
Text='<%# DateTime.Now.ToString() %>' but not working

and what should I do if I want to do the same with a Session variable, for example Session["userName"] ?

thx, waiting Ur answers
alsi

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 03 Nov 2008, 10:42 AM
Hello,

Check out the following code to access set the session variable and access the value in the code behind.
aspx:
 <telerik:GridTemplateColumn UniqueName="UserName"
      <ItemTemplate> 
        <asp:Literal runat="server" Text='<%#Session["username"] %>' ID="litUserName" />                   
      </ItemTemplate> 
 </telerik:GridTemplateColumn> 

cs:
    protected void RadGrid1_PreRender(object sender, EventArgs e) 
        { 
            foreach (GridDataItem item in RadGrid1.Items) 
            { 
                string str = ((Literal)item["UserName"].FindControl("litUserName")).Text; 
            }    
        } 
0
alsi
Top achievements
Rank 1
answered on 03 Nov 2008, 10:45 AM
Thanks!

this is what I exactly need! and what about DateTime.Now? pls give me a code snippet.

thx
0
Accepted
Daniel
Telerik team
answered on 03 Nov 2008, 11:29 AM
Hello Alsi,

This code should be working as expected:
<telerik:GridTemplateColumn UniqueName="DateNow"
    <ItemTemplate> 
        <asp:Literal runat="server" ID="litDateNow" Text='<%# DateTime.Now.ToString() %>'></asp:Literal> 
    </ItemTemplate> 
</telerik:GridTemplateColumn> 

Otherwise you can access the literal control in code-behind:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) 
    GridDataItem item = e.Item as GridDataItem; 
    if (item != null
    { 
        (item["DateNow"].Controls[1] as Literal).Text = DateTime.Now.ToString(); 
    } 

Regards,
Daniel
the Telerik team

Check out Telerik Trainer, the state of the art learning tool for Telerik products.
Tags
Grid
Asked by
alsi
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
alsi
Top achievements
Rank 1
Daniel
Telerik team
Share this question
or