Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/70.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从代码隐藏显示表格_C#_Html_Asp.net - Fatal编程技术网

C# 从代码隐藏显示表格

C# 从代码隐藏显示表格,c#,html,asp.net,C#,Html,Asp.net,我的程序中有一个函数,它将一个由表组成的字符串打印到页面上 getWhileLoopData(){ string htmlStr = ""; SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString); SqlCommand th

我的程序中有一个函数,它将一个由表组成的字符串打印到页面上

getWhileLoopData(){          
        string htmlStr = "";
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);     
        SqlCommand thisCommand = thisConnection.CreateCommand();       
        thisCommand.CommandText = "SELECT * from events";      
        thisConnection.Open();      
        SqlDataReader reader = thisCommand.ExecuteReader();      
        while (reader.Read()){      
            int room_id = reader.GetInt32(0);
            string title = reader.GetString(1);                
            htmlStr += "<tr><td>" + title + "</td></tr>";                        
        }      
        thisConnection.Close();      
        return htmlStr;
}
getWhileLoopData(){
字符串htmlStr=“”;
SqlConnection thisConnection=新的SqlConnection(ConfigurationManager.ConnectionString[“ConnectionString”].ConnectionString);
SqlCommand thisCommand=thisConnection.CreateCommand();
thisCommand.CommandText=“从事件中选择*”;
thisConnection.Open();
SqlDataReader=thisCommand.ExecuteReader();
而(reader.Read()){
int room_id=reader.GetInt32(0);
字符串标题=reader.GetString(1);
htmlStr+=“”+标题+“”;
}      
thisConnection.Close();
返回htmlStr;
}
在.aspx页面中,我通过
调用它,它可以工作。 但问题是,当我在
page\u load(objectsender,EventArgs e)
函数中打开页面时,我有类似的东西,它会将表值附加到字符串中


如何将page_load()函数中的字符串打印到屏幕上?

我认为最好在服务器上运行div,然后使用html代码调用函数集innerhtml Property

<div id="test" runat="server></div>

如果您想根据某些数据在页面上添加特定的HTML,更好的方法是使用现有的.net控件之一,例如链接到数据源的中继器。数据源可以是数据库或列表(基本上是实现IEnumerable的任何内容)

或者,您可以通过继承WebControl来创建自己的控件,并覆盖呈现事件来呈现HTML

如果您选择自定义控件路径,则只需执行以下操作

<%@ Register TagPrefix="XYZ" NameSpace="XYZ.MyControlsNameSpace" Assembly="MyControlsAssembly" %>

然后用它,

<XYZ:MyNewCustomControl runat="server" SomeProperty="SomeValue" ... />


当aspx引擎遇到XYZ:MyNewCustomControl时,它将在该点将控件插入页面,并在页面事件生命周期中运行它,导致它被呈现,导致它的呈现事件触发,其中所有的HTML将被注入页面。

不太清楚您的结果是什么。。。你能显示输出吗?然后从这里指定您希望它说什么?您能澄清一下
将表值附加到字符串的意思吗?这就是你的代码告诉它要做的。您是否在寻求一种方法,从
读取器
中填充HTML表,而无需手动构建字符串?此外,以这种方式连接字符串来构建HTML是一种非常低效、难以维护的方法。这不是PHP。因此上面的代码打印的是数据库中作为表的两列。load_page()的代码与此几乎相似,但查询不同。+1用于使用现成控件。基于这个问题,实现自定义控件可能有点太高级了。
getWhileLoopData(){          
        string htmlStr = "";
        SqlConnection thisConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);     
        SqlCommand thisCommand = thisConnection.CreateCommand();       
        thisCommand.CommandText = "SELECT * from events";      
        thisConnection.Open();      
        SqlDataReader reader = thisCommand.ExecuteReader();      
        while (reader.Read()){      
            int room_id = reader.GetInt32(0);
            string title = reader.GetString(1);                
            htmlStr += "<tr><td>" + title + "</td></tr>";                        
        }      
        thisConnection.Close();      
        return htmlStr;
}