Solved

setting a div with C# in aspx file

Posted on 2011-03-11
9
886 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
  • 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
 

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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

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…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

758 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now