Results 1 to 13 of 13

Thread: [RESOLVED] Populating a client text area from server code

  1. #1

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Resolved [RESOLVED] Populating a client text area from server code

    Hello,

    I have a .net user control which gets some data from the database in the controls load event, once it has this data I want it to populate a html text area on the same control.

    I have looked at many examples of how to populate client controls from server side code but not ones on a user control.

    So far I have the following in script blocks at the start of my markup.
    Code:
     function AddText(elementid, txtval) {     var txtArea = document.getElementByID ( elementid );     if ( txtArea )     {       txtArea.value = txtval;     }  }
    and after I have retreived the data base in my controls load method I have the following

    Code:
    if (!Page.ClientScript.IsStartupScriptRegistered("AddText"))
                    {
                        Page.ClientScript.RegisterStartupScript
                            (this.GetType(), "AddText", "AddText('txtArea', '" + aOutline1.Overview + "');", true);
                    }
    When I run the code the function gets called and appears to pass the correct values in, however it throws this error
    Microsoft JScript runtime error: Object doesn't support this property or method
    when it hits the code document.getElementByID ( elementid );

    I guess this may have something to do with it being on a user control?

    Anyone able to help at all.

  2. #2
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: Populating a client text area from server code

    you have syntax error here
    Code:
    getElementByID 
    The "D" should be lower case:
    Code:
    getElementById
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  3. #3

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: Populating a client text area from server code

    Thanks, I dont work much with Javascript and mainy copy things off the web to suit my needs, I had completely forgotten it was case sensitive and just trusted the code I found.

  4. #4
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    Ok no problem, I just have to say about something you wrote:
    Code:
    I guess this may have something to do with it being on a user control?
    I see this types of mistakes coming a lot from people, you must to understand that ASP.NET controls (whatever it be) ending up rendering as native HTML, there is NO such thing ASP.NET controls or elements after your code is being rendered and handed back to the client browser, so what ever ASP.NET controls you use, javascript will be able to understand and use it if you get them properly.

    just keep that in mind
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  5. #5

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: [RESOLVED] Populating a client text area from server code

    Thanks, the reason for confusion is all our page content has to sit in the user controls, this is so we can port our controls on to other web pages quickly i.e. into a CMS page without having to alter the page content.

    It is causing me problems though, i.e. control communication.

    I have a control on one page which has some public properties, on the click of a button(on the control) it takes you to another page also with a user control. I want the second user control to be able to get the value from the user control on the previous page.

    I am trying to do this via a crosspagepostback. However I cannot seem to get the value because when I try and cast the control as the control type required it doesnt recognise it as a type so wont build.

    Code:
    Outline ctrla = (Outline)Page.PreviousPage.FindControl("Outlines1");
                
                //if (ctrla != null)
                //{
                 
                //    OutlineID = ctrla.outlineID;
                //    this.lblCourseCode.Text =  OutlineID;
    }
    even though I have referenced the first control in my markup

    Code:
    <%@ Register src="../../usercontrols/Outlines.ascx" tagname="Outlines" tagprefix="uc2" %>

  6. #6
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    are you using master page ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  7. #7

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: [RESOLVED] Populating a client text area from server code

    Yes we are, could this be affecting it?

  8. #8
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    of course it, you think you can handle it or should I'll scrap example for you ?
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  9. #9

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: [RESOLVED] Populating a client text area from server code

    I have no idea what the difference would be if you could show me an example please.

  10. #10
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    ok, I'll write one down soon
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  11. #11
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    Ok let me explain how to retrieve you control property value when using master page:

    first i created a new website and add this very basic control that only has label:

    MyControls.ascx
    Code:
    <&#37;@ Control Language="C#" AutoEventWireup="true" CodeFile="MyControl.ascx.cs" Inherits="MyControl" %>
    <asp:Label ID="lblPropertyValue" runat="server" Text="Label"></asp:Label>
    in the code behind I added property to fill that label:

    MyControls.cs
    Code:
     public partial class MyControl : System.Web.UI.UserControl
        {
            private string _MyProperty;
    
            public string MyProperty
            {
                set
                {
                    this._MyProperty = value;
                }
    
                get
                {
                    return this._MyProperty;
                }
            }
            protected void Page_Load(object sender, EventArgs e)
            {
                lblPropertyValue.Text = this.MyProperty;
            }
        }
    So now I have control that called "MyControl" and a string property with the name of "MyProperty", I added to the control to the source page along with a button that will do the post back

    Default.aspx
    Code:
        <uc1:MyControl ID="MyControl1" runat="server" MyProperty="My Property" />
        <asp:Button ID="Button1" runat="server" PostBackUrl="~/Default2.aspx" Text="Button" />
    so the button is set to post back to Default2.aspx:

    Default2.aspx
    Code:
     if (Page.PreviousPage != null)
            {
    
                // now here all the dirty work is been done
    
                // first you need to get your master page ContentPlaceHolder control which holds your Custom Control
                // and here is how you do that.
                // Note my Master page ContentPlaceHolder Control ID is "MainContent" change it if it's different
                // in your master page
                ContentPlaceHolder BodyContent = (ContentPlaceHolder)PreviousPage.Master.FindControl("MainContent");
    
                // once we have the ContentPlaceHolderControl we can get our custom control
                // change this "controls_mycontrol_ascx" to your custom control class name 
                ASP.controls_mycontrol_ascx test = (ASP.controls_mycontrol_ascx)BodyContent.FindControl("MyControl1");
                Trace.Warn(test.MyProperty);
            
    
            }
    of course I didn't made more checks for null values which you should do, if you have anymore question we are here to help.

    Note:There are more ways to get the controls like drilling down the controls tree but I Decided to show you the most basic method since this methods helps to understand
    how things built and why you need to do it all of this.
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

  12. #12

    Thread Starter
    Frenzied Member FishGuy's Avatar
    Join Date
    Mar 2005
    Location
    Bradford UK
    Posts
    1,708

    Re: [RESOLVED] Populating a client text area from server code

    That is excellent thankyou so much, it worked exactly as you said with code below.
    Code:
      ContentPlaceHolder BodyContent = (ContentPlaceHolder)Page.PreviousPage.Master.FindControl("maincontent");
                ASP.usercontrols_courses_ascx test = (ASP.usercontrols_courses_ascx)BodyContent.FindControl("Courses1");
              
                            
               
                 
                    courseCode = test.courseCode;
                    this.lblCourseCode.Text = courseCode;
    Thanks again.

  13. #13
    PowerPoster motil's Avatar
    Join Date
    Apr 2009
    Location
    Tel Aviv, Israel
    Posts
    2,143

    Re: [RESOLVED] Populating a client text area from server code

    No problem at all, Glad to hear everything working now
    * Rate It If you Like it

    __________________________________________________________________________________________

    "Programming is like sex: one mistake and you’re providing support for a lifetime."

    Get last SQL insert ID

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width