如何格式化树视图的字符串

时间:2013-11-11 13:02:15

标签: asp.net

我有一个树视图。我想形成像网格视图一样的附加字符串:

<ItemTemplate>
<asp:Label ID="lbut_subject" runat="server" 
Text='<%#Eval("Inquiry_subject").ToString().Length > 10 ? Eval("Inquiry_subject").ToString().Substring(0,10)+"..." :Eval("Inquiry_subject") %>'
ToolTip='<%# Bind("Inquiry_subject") %>'></asp:Label>
</ItemTemplate>

我多么希望这与我的树视图有关:

<asp:TreeView ID="TV_Question_Summary" runat="server" ImageSet="Simple" ShowLines="True"  OnTreeNodeDataBound="TV_Question_Summary_DataBound" SkipLinkText=""> 
      <DataBindings>
        <asp:TreeNodeBinding DataMember="Root" FormatString=" {0}" TextField="" ValueField=""/>
        <asp:TreeNodeBinding DataMember="ParentNode" FormatString=" {0}" TextField="Inquiry_id" ValueField="Inquiry_id"/><asp:TreeNodeBinding DataMember="Node" FormatString=" {0}" TextField="body" ValueField="Inquiry_id" ToolTip="body"/>
     </DataBindings>
</asp:TreeView>

我希望将字符串表示为...如果Node字体字符串溢出15个字符中没有...

用于绑定文本我使用此代码:

 void fill_Tree()
    {
        using (SqlConnection conn = Util.GetConnection())
        {
            conn.Open();
            SqlCommand SqlCmd = new SqlCommand("Select * from tbl_Inquiry_History Where (IsDisplay='True')", conn);
            SqlDataReader Sdr = SqlCmd.ExecuteReader();
            SqlCmd.Dispose();
            string[,] ParentNode = new string[100, 2];
            int count = 0;
            while (Sdr.Read())
            {

                ParentNode[count, 0] = Sdr.GetValue(Sdr.GetOrdinal("Inquiry_id")).ToString();
                ParentNode[count++, 1] = Sdr.GetValue(Sdr.GetOrdinal("Inquiry_id")).ToString();

            }
            Sdr.Close();
            for (int loop = 0; loop < count; loop++)
            {
                TreeNode root = new TreeNode();
                root.Text = ParentNode[loop, 1];
                root.NavigateUrl = "~/Admin/OWM_Inquiry.aspx?inquiryid="+ ParentNode[loop, 1];
                SqlCommand Module_SqlCmd = new SqlCommand("Select * from tbl_Question where (Inquiry_id = @parent_id AND IsCount='False')", conn);
                Module_SqlCmd.Parameters.AddWithValue("@parent_id", ParentNode[loop, 0]);
                SqlDataReader Module_Sdr = Module_SqlCmd.ExecuteReader();
                while (Module_Sdr.Read())
                {
                    TreeNode child = new TreeNode();
                    child.Text = Module_Sdr.GetValue(Module_Sdr.GetOrdinal("body")).ToString();
                    child.NavigateUrl = "~/Admin/OWM_Inquiry.aspx?inquiryid="+ParentNode[loop, 0];
                    root.ChildNodes.Add(child);
                }

                Module_Sdr.Close();
                TV_Question_Summary.Nodes.Add(root);

            }
            TV_Question_Summary.CollapseAll();
        }

    }

请帮帮我......

1 个答案:

答案 0 :(得分:1)

尝试使用代码隐藏方法而不是嵌入式代码块,如下所示:

protected string DisplayInquirySubject(string inquirySubject)
{
    return inquirySubject.Length > 10 
        ? inquirySubject.Substring(0, 10) 
        : inquirySubject;
}

现在在你的标记中,你可以调用这样的方法:

<ItemTemplate>
    <asp:Label ID="lbut_subject" runat="server" 
               Text='<%# DisplayInquirySubject(Eval("Inquiry_subject").ToString()) %>'
               ToolTip='<%# Bind("Inquiry_subject") %>'>
    </asp:Label>
</ItemTemplate>

这有两个好处,它简化了标记逻辑,它允许您利用Visual Studio代码编辑器的强大功能(即Intellisense和调试)。