setting a div with C# in aspx file

I cannot solve the error I get with this code...

Compiler Error Message: CS1061: 'ASP.atest_aspx.setText' does not contain a definition for 'myDiv' and no extension method 'myDiv' accepting a first argument of type 'ASP.atest_aspx.setText' could be found (are you missing a using directive or an assembly reference?)

Source Error:

Line 8:      public void test(string s)
Line 9:      {
Line 10:       this.myDiv.innerHTML = s;
Line 11:       return;
Line 12:     }
 
----- this is my aspx file ----

<%@ Page Language="C#" Title="test" aspcompat="true" %>
<script runat="server">
  class setText
  {
    public setText()
    {
    }
    public void test(string s)
    {
      this.myDiv.innerHTML = s;
      return;
    }
  }
</script>
  <div id="myDiv" runat="server"><p>test text 1</p></div>
<%
  {
    setText foo = new setText();
    foo.test((string)"test text 2 ");
  }
%>
KalvynAsked:
Who is Participating?
 
Carl TawnConnect With a Mentor Systems and Integration DeveloperCommented:
You'd have to manually attach and event handler to the PreRender event using:
this.PreRender += new EventHandler(this.Page_PreRender);

Open in new window

And name the method:
 void Page_PreRender(object sender, EventArgs e)
{
  
}

Open in new window

But i don't think that is going to achieve what you want anyway. All of these events fire as part of the page lifecycle before anything is sent to the browser. I suspect you are thinking that Page_Load will write out "start" for the client to see, and then that the text will change to "done" in PreRender and the change be updated on the client.

That isn't what will happen. All those events will happen in sequence, so you are altering the text before it gets sent to the browser, so the only thing the client will ever see is "done".
0
 
KalvynAuthor Commented:
I would rather use this code but it doesn't change the div.

<%@ Page Language="C#" Title="test"%>
<script runat="server">
void SetText(string s)
{
  this.myDiv.InnerHtml = s;// +" : " + DateTime.Now.ToString();
}
</script>

<html>
<body>
<div id="myDiv" runat="server"><p>Running query...</p></div>
--- start ---
<%
  Response.Write(myDiv.InnerHtml);
  SetText("Updated text.");
  Response.Write(myDiv.InnerHtml);
  Response.Flush();
%>
--- end ---
</body>
</html>

I have fuddled with the code for hours, any Ideas?
0
 
Carl TawnSystems and Integration DeveloperCommented:
Even thought you are using inline code rather than code-behind the Page is still an object so you have to put your code into an event. In your case Page_Load would probably suit:
<script runat="server">
void SetText(string s)
{
  this.myDiv.InnerHtml = s;// +" : " + DateTime.Now.ToString();
}

void Page_Load(object sender, EventArgs e)
{
      SetText("Updated text.");
}
</script>

Open in new window

0
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

 
KalvynAuthor Commented:
Yes, i've done that before, but the problem I am trying to solve is to set the div to "running  query...", then when the these statements finish
ADODB.Recordset data = new ADODB.Recordset();
data.Open(sql,MyConnection...)
SetText("open finished");
I want to be able to cause the event or call the function to update text so the user doens't think the query started.  I would be willing to do in in code behind.

0
 
KalvynAuthor Commented:
Going on your [carl_tawn] advice, is the an event that is called like Page_Unload that I can call to change the text of a label or InnerHTML of a DIV before the page is rendered?  I can't google to find where the list of page evens are documented.  I can find the .NET methods, but they don't work for my code.
0
 
KalvynAuthor Commented:
I tried this, the Page_Load gets called, but not the PreRender, I expect the "start" to be replace with "done".  What am I missing?

<%@ Page Language="C#" Title="test"%>

<script runat="server">
void Page_Load(object sender, EventArgs e)
{
  SetText("start.");
}
  void PreRender(object sender, EventArgs e)
{
  SetText("done.");
}
void SetText(string s)
{
  this.myDiv.InnerHtml = s;// +" : " + DateTime.Now.ToString();
//window.defaultStatus = "This is the default status message.";
}
</script>

<html>
<body>

<div id="myDiv" runat="server"><p>-- div --</p></div>
<h5>Some Text</h5>

</body>
</html>
0
 
KentMarshCommented:

Create a "div" Programmatically

You can programmatically create "div" objects and put controls inside them by overriding the OnPreInit method in your ASP.NET Page code behind.

A Panel control on an ASP.NET page is really a  "div". I have provided some code that will create a "myDiv" area and populated it with a textbox and a button. The  "div" needs to be inside a page Form element; so, I first find "form1" and then add the  "div" to it.

    protected override void OnPreInit(EventArgs e)
    {
        base.OnPreInit(e);

        Control form1 = null;

        // Find your form
        if (this.Controls != null)
        {

            foreach (Control c in this.Controls)
            {
                try
                {
                    if (c != null && c.ID.Equals("form1"))
                    {
                        form1 = c;
                        break;
                    }
                }
                catch(Exception ex)
                {}
            }

            if (form1 != null)
            {
                // Load form1 with a new Panel
                // Note a Panel is a "div"
                Panel MyPanel = new Panel();
                form1.Controls.Add(MyPanel);

                // Add Controls to the Panel
                Button MyButton = new Button();
                MyButton.Text = "This is My Button";
                TextBox MyTextBox = new TextBox();
                MyTextBox.Text = "Hello";
                MyPanel.Controls.Add(MyTextBox);
                MyPanel.Controls.Add(MyButton);
            }
        }
    }

Open in new window


When you run the page, the resulting HTML is shown below.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>
 
</title></head>
<body>
    <form name="form1" method="post" action="MySample.aspx" id="form1">
<div>
</div>
<div id="myDiv">
	<input name="ctl01" type="text" value="Hello" /><input type="submit" name="ctl02" value="This is My Button" />
</div>
</form>
</body>
</html>

Open in new window

0
 
KentMarshCommented:
If all you are trying to do is reference controls with your C# code in order to set attribute values, then you need to be sure to reference them correctly.

The easiest way is to use the Control's ID. For example, I have a Label with the ID of "lblStatus" as defined in the HTML below.
<asp:Label ID="lblException" runat="server" Text=""></asp:Label>

Open in new window


I can set it's value by using the ID reference in my C# code.
            try
            {
                // Some logic that might throw an exception
            }
            catch (Exception ex)
            {
                this.lblException.Text = ex.Message;
            }

Open in new window

0
 
KalvynAuthor Commented:
I think this is right.  I can't do what I want to do while the page is rendering.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.