简体   繁体   English

在不使用转发器或其他控件的情况下向ASPX页面显示数据

[英]Display data to ASPX page without using repeater or other controls

I have a sample code snippet below which works just fine (I trimmed the query a bit for sample purposes). 我在下面有一个示例代码片段,它可以很好地工作(为了示例目的,我稍微调整了一下查询)。 I want to display the data in an ASPX file but I do not want to use a repeater, datagrid or anything like that. 我想在ASPX文件中显示数据,但我不想使用转发器,数据网格或类似的东西。 I should mention I am not working in MVC3. 我应该提到我不在MVC3工作。

In ASP 3.0 it was possible to use a DO WHILE loop to do this, I can't seem to get it to work in ASPX. 在ASP 3.0中,可以使用DO WHILE循环来执行此操作,我似乎无法在ASPX中使用它。 Can someone post a code snippet that will do the job? 有人可以发布一个可以完成这项工作的代码片段吗? Many thanks in advance. 提前谢谢了。

    public void sqlconn2(object sender, EventArgs e)
    {
        SqlConnection cn = null;
        cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
        cn.Open();
        SqlCommand cmd = new SqlCommand("SELECT DISTINCT submajor.subid FROM subdetails", cn);
        myreader = cmd.ExecuteReader();
    }

    public static SqlDataReader myreader { get; set; }

Edit: If I put the code directly in the markup, it works. 编辑:如果我将代码直接放在标记中,它就可以了。 Obviously I do not want to do this. 显然我不想这样做。 Here is the working sample of the markup code: 以下是标记代码的工作示例:

            <%
        System.Data.SqlClient.SqlConnection cn = null;
        cn = new System.Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString());
        cn.Open();
        System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand("SELECT DISTINCT submajor.subid FROM subdetails", cn);
        myreader = cmd.ExecuteReader();
        %>
        <% 
        while (myreader.Read())
           {
               if (myreader["subid"] != null)
               {%>
                   <div><%Response.Write(myreader["subid"]);%></div>
               <%}
           }
       %>

There are unfortunately a few problems here. 不幸的是,这里有一些问题。 ASPX (ASP.NET WebForms) is all about using controls. ASPX(ASP.NET WebForms)就是使用控件。 Doing it any other way breaks the model. 以任何其他方式执行它会破坏模型。

ASP.NET MVC pretty much mandates that you must do it yourself, without using controls. ASP.NET MVC几乎要求您必须自己完成,而不使用控件。

It cannot be summed up quickly in an answer here, but http://www.asp.net/mvc is great, and the Music Store Tutorial App - http://www.asp.net/mvc/tutorials/mvc-music-store/mvc-music-store-part-1 - is a really good introduction / explanation. 这里的答案无法快速总结,但http://www.asp.net/mvc很棒,音乐商店教程应用程序 - http://www.asp.net/mvc/tutorials/mvc-music -store / mvc-music-store-part-1 - 是一个非常好的介绍/解释。

(As per your recent edit, in MVC you write the code that presents the data in the markup, which you don't want to do, but that's really where that belongs. Regular ASPX WebForms abstracts views into controls using properties etc and MVC lets you present in the view markup; those are the two best options) (根据您最近的编辑,在MVC中,您编写了代码,该代码在标记中显示数据,您不想这样做,但这确实属于那里。常规ASPX WebForms使用属性等将视图抽象为控件,MVC让你出现在视图标记中;这是两个最佳选择)

You could try the code below. 您可以尝试下面的代码。

in your aspx markup add a Label control 在您的aspx标记中添加一个Label控件

<asp:Label id="container" runat="server"/>

In your codebehind make use of your datareader at the point where you need to . 在您的代码隐藏中,在您需要的地方使用您的datareader。

 while (myreader.Read())
           {
               if (myreader["subid"] != null)
               {
                   string str = string.Format("<div>{0}</div>",myreader["subid"]);
                   container.Controls.Add(new LiteralControl(str)); //label we added in markup
               }
       }

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

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