[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

setting a div with C# in aspx file

Posted on 2011-03-11
9
Medium Priority
?
909 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 

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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

656 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