?
Solved

setting a div with C# in aspx file

Posted on 2011-03-11
9
Medium Priority
?
903 Views
Last Modified: 2012-05-11
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 ");
  }
%>
0
Comment
Question by:Kalvyn
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 2
  • 2
9 Comments
 

Author Comment

by:Kalvyn
ID: 35113576
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
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 35113706
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
 

Author Comment

by:Kalvyn
ID: 35113976
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Kalvyn
ID: 35124195
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
 

Author Comment

by:Kalvyn
ID: 35124317
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
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 1000 total points
ID: 35125945
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
 
LVL 2

Expert Comment

by:KentMarsh
ID: 35129442

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
 
LVL 2

Expert Comment

by:KentMarsh
ID: 35129571
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
 

Author Closing Comment

by:Kalvyn
ID: 35166426
I think this is right.  I can't do what I want to do while the page is rendering.
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question