Literal控件怎么不会执行换行操作呢?应该怎么让动态生成的每个控件占用一行呢?

Sanlence 2016-07-12 09:49:17
页面展示情况和疑问在图片中,请看:


我的cs代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

public partial class ReadAllLines : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (null == Request.QueryString["fname"])
{
Response.Write("ReadAllText.aspx.cs:" + "QueryString没有值" + "<br />");
Response.Redirect("https://www.baidu.com/");
}
else
{
String filePath = Path.Combine(Server.MapPath("读取内容文件夹"), Request.QueryString["fname"]);
if (!File.Exists(filePath))
{
Response.Write("ReadAllText.aspx.cs:" + "没有这个文件。");
Response.Redirect("https://www.baidu.com/");
}

String[] content = File.ReadAllLines(filePath);
if (content.Length < 1)
{
Response.Write("ReadAllText.aspx.cs:" + "文件没有内容");
}

TextBox tb;
Literal ltl = new Literal();
ltl.Text = "<br />";
foreach (String line in content)
{
tb = new TextBox();
tb.Text = line;
line_panel.Controls.Add(tb);
line_panel.Controls.Add(ltl);
}
}
}
}
protected void save_btn_Click(object sender, EventArgs e)
{
if (null == Request.QueryString["fname"])
{
Response.Write("ReadAllText.aspx.cs:" + "QueryString没有值" + "<br />");
Response.Redirect("https://www.baidu.com/");
}
else
{
String filePath = Path.Combine(Server.MapPath("读取内容文件夹"), Request.QueryString["fname"]);
if (!File.Exists(filePath))
{
Response.Write("ReadAllText.aspx.cs:" + "没有这个文件。");
Response.Redirect("https://www.baidu.com/");
}

String content = "";
foreach (Control ctl in line_panel.Controls)
{
if (ctl is TextBox)
{
content += ((TextBox)ctl).Text + '\u0081';
}
}

content = content.Substring(0, content.Length - 1);
String[] lines = content.Split('\u0081');

File.WriteAllLines(filePath, lines);
}
}
}


我的样式如下:

<%@ Page Title="" Language="C#" MasterPageFile="~/Editor.master" AutoEventWireup="true" CodeFile="ReadAllLines.aspx.cs" Inherits="ReadAllLines" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
<style type="text/css">
#line_panel {
display:block;
width:400px;
}
</style>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="holder_main" Runat="Server">
<asp:Panel ID="line_panel" runat="server" clientidmode="Static"></asp:Panel>
<br />
<br />
<asp:Button ID="save_btn" runat="server" Text="保存文本" OnClick="save_btn_Click" />
</asp:Content>


我是菜鸟,请问各位前辈,为什么这么不行呢?如果不行,有什么替代方式吗?谢谢了!
...全文
210 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
Sanlence 2016-07-15
  • 打赏
  • 举报
回复
引用 2 楼 insus 的回复:
Literal控件只是显示你的html代码。 看见你有new TextBox, 那建议你还是使用Placeholder容器来加载你的new TextBox吧。
谢啦,您的建议非常不错,我试了一下可行!
insus 2016-07-12
  • 打赏
  • 举报
回复
Literal控件只是显示你的html代码。 看见你有new TextBox, 那建议你还是使用Placeholder容器来加载你的new TextBox吧。
  • 打赏
  • 举报
回复
简单程序写成花哨的代码,这其实是不好的。这方面你自己慢慢体会吧。 在 for 循环里插入n个 ltl,你应该每一次插入之前创建一个 ltl 实例,这才是插入 n 个 ltl 的意思。你用一个 ltl 反复“插入”,这是不论是 wpf、winform、silverlight、asp.net 都应该告诉过你的知识,一个控件插入控件树的时候首先会从控件树上移除之前插入的位置。也就是说n 次插入,至于最后一次是有效的,之前 n-1 次都是开玩笑找快感、插一下又取出的。

62,074

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧