C#.NET question ... Load server code inside DIV tag?

dalearyous

Senior member
Jan 8, 2006
836
0
0
i am extremely new to this so go easy ...

i want to load in a bunch of server code inside a DIV tag. right now my example works but the result loads at the top of the page (because i have it running at Page_Load). what i would like is to load it inside a DIV.

my code that is located in a code behind file: (i have removed some things but what i have works)

Code:
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection con1;
        SqlCommand cmdSelectEmployees;
        SqlDataReader dataReaderEmployees;

        con1 = new SqlConnection("the omitted connection string");
        con1.Open();

        cmdSelectEmployees = new SqlCommand("SELECT LastName,Lab FROM Employee WHERE JobType=\'Something\' ORDER BY LastName", con1);

        dataReaderEmployees = cmdSelectEmployees.ExecuteReader();

        while (dataReaderEmployees.Read())
        {
            Response.Write(dataReaderEmployees.GetValue(1) + "<br />");
        }

        dataReaderEmployees.Close();
    }

so if i load that page it spits out the result at the top of the page. i would like the result spit out inside a DIV tag on page load. HELP
 

PhatoseAlpha

Platinum Member
Apr 10, 2005
2,131
21
81
Don't use document.write. Instead, put a literal control inside the div, and set the text value of that literal in your onLoad.
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
should i use a repeater? if so, do you have an example?

No, why would you use a repeater? You just want to get a block of text into the InnertHtml property of a div node in the DOM. Easiest way to do that is to either use a literal HTML control as mentioned above...

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.literal.aspx
http://www.techrepublic.com/blog/pr...ets-literal-control-to-its-full-potential/618

In this case you would include the enclosing DIV, I believe, in the text you assign to the literal's Text property.

You can also add the "runat=server" and "ID=somename" to the DIV in your markup, which will cause it to be available in your codebehind as a HtmlGenericControl...

http://msdn.microsoft.com/en-us/library/7512d0d0(VS.71).aspx
 

dalearyous

Senior member
Jan 8, 2006
836
0
0
Code:
            Repeater1.DataSource = dataReaderEmployees;
            Repeater1.DataBind();

and ...

Code:
<asp:Repeater ID="Repeater1" runat="server">
        <ItemTemplate>
        <li><%# Eval("LastName") %></a></li>
        </ItemTemplate>
        </asp:Repeater>

worked!
 

Markbnj

Elite Member <br>Moderator Emeritus
Moderator
Sep 16, 2005
15,682
14
81
www.markbetz.net
Ah, I don't think I understood your description of your problem. Anyway, glad you got it working.
 

jsedlak

Senior member
Mar 2, 2008
278
0
71
I like the "make it a server control" method better - in pre-asp.net4 the built-in controls have a tendency to output garbage that is hard/painful/etc to use with jQuery/js.
 

BigDH01

Golden Member
Jul 8, 2005
1,631
88
91
One thing I would say is that you should learn to use and love LINQ to SQL. Your quality of life will improve 1000 fold. Imagine going from what you wrote to...

Code:
Repeater1.DataSource = dc.Employees.Where(d => d.JobType == "something").OrderBy(d => d.LastName);
Repeater1.DataBind();

Might have to call .ToList() on the query to actually execute, I haven't used webforms in a long time (you should try MVC ;). Turns your big block of code into two lines.

Also, don't forget to dispose your connection.
 
Last edited: