获取内容itemTemplate

时间:2011-08-12 09:44:31

标签: c# asp.net linq-to-sql gridview

<asp:GridView ID="gridInboxMessage" runat="server" 
            AutoGenerateColumns="False" 
            DataSourceID="LinqDataSource1">
    <Columns>
        <asp:BoundField DataField="Title" HeaderText="title" ReadOnly="True" SortExpression="Title" />

        <asp:TemplateField HeaderText="Body" SortExpression="Body">
            <ItemTemplate>
                <asp:Label ID="MyBody" runat="server"
                        Text='<%# TruncateText(Eval("Body"))%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Sender">
            <ItemTemplate>
                <asp:Label ID="MySender" runat="server"
                        Text='<%# GetSenderNameFromID(Eval("Sender"))%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>

        <asp:TemplateField HeaderText="Date" SortExpression="Date1">
            <ItemTemplate>
                <asp:Label ID="MyDate" runat="server"
                        Text='<%# DataBinder.Eval(Container.DataItem, "Date1", "{0:MMMM d yyy}")%>'>
                </asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
<div id="FullBody"></div>

背后的代码

protected string TruncateText(object objBody)
{
    string truncated = "";
    if (objBody != null)
    {
        truncated = objBody.ToString().Length > 50 ? 
            objBody.ToString().Substring(0, 47) + "..." : objBody.ToString();
    }
    return truncated;
}

1.我想要

protected void gridInboxMessage_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = gridInboxMessage.SelectedRow;
    //i want show full body in div `fullBody`
}

如何访问body以在div fullBody

中显示完整的正文

2.我希望连背景颜色的行都是黑色的吗?

1 个答案:

答案 0 :(得分:0)

要从您的代码中访问div,最简单的方法是将div的runat属性设置为true,即<div id="fullbody" runat="server"/>

然后在您的代码中,您可以像访问任何其他控件一样访问它:fullbody.some_property = "";

对于替代行背景颜色,您可以使用:<alternatingrowstyle backcolor="Black" />