简体   繁体   中英

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. I should mention I am not working in 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. 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. Doing it any other way breaks the model.

ASP.NET MVC pretty much mandates that you must do it yourself, without using controls.

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.

(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)

You could try the code below.

in your aspx markup add a Label control

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

In your codebehind make use of your datareader at the point where you need to .

 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
               }
       }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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