简体   繁体   English

解析中继器的值

[英]Parse Value from repeater

<asp:Repeater ID="repeater" runat="server" OnItemDataBound="repeater_ItemDataBound">

<HeaderTemplate>
    <ul class="sf_newsList">
</HeaderTemplate>

<ItemTemplate>  
<asp:Literal ID="Publication_Date" runat="server" Text="{0}" />
...

Ok so i have a date coming back from the source, i need to strip this date into day and month as it currently comes back like "march 12 2012". 好的,所以我有一个从源头返回的日期,我需要将此日期剥离为日和月,因为它现在像“ 2012年3月12日”一样返回。

once parsed into a day and a month string i want to then pass it back into 一旦解析成一天和一个月的字符串,我想然后将其传递回

<asp:Literal ID="Publication_Day" runat="server" Text="{0}" />

now ive tried some things such as 现在我尝试了一些东西,例如

protected void repeater_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
    string newsdate = "";
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType ==  ListItemType.AlternatingItem)
    {
        Literal ltDate = (Literal)e.Item.FindControl("Publication_Date");
        newsdate = ltDate.Text;
        //do parse code
        Literal ltDay = (Literal)e.Item.FindControl("Publication_Day");
        ltDay.Text = newsdate;
    }       
}

but when i step through the publication_date is only returning {0} as a string not the date. 但是当我单步执行publication_date时,仅返回{0}作为字符串,而不返回日期。

i know the source is fine, as if i just put the date on the template is shows up on the front end. 我知道来源很好,就好像我只是将日期放在模板上一样。

Any ideas what im doing wrong? 任何想法我做错了什么?

It is returnin {0} because it is on the Text property of Literal. 返回值{0}因为它位于Literal的Text属性上。

You could use the DataBinder.Eval to read the column of your DataSource from e.Item.DateItem . 您可以使用DataBinder.Evale.Item.DateItem读取DataSource的列。 Since you know what is the column you want to read you could get the DateTime object direct from datasource, for sample: 由于您知道要读取的列是什么,因此可以直接从数据源获取DateTime对象,例如:

// get the dateTime from datasource
DateTime datePublication = DateTime.Parse(DataBinder.Eval(e.Item.DataItem, "DatePublicationColumn").ToString());

// show the day name
ltDay.Text = datePublication.ToString("dddd");

Where I add DatePublicationColumn , you specify the column. 在我添加DatePublicationColumn ,您可以指定列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM