Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

ASP RegisterClientScriptInclude problem

Options
  • 11-05-2009 8:47pm
    #1
    Registered Users Posts: 4,037 ✭✭✭


    I want to pass a C# variable form the code-behind of my ASP page to a javascript function. I know how to do it when the variable remains the same but when it changes according to what the user is doing then I have a problem (which is the way I want to do it)

    This is what I have so far:

    Under the Page_Load event of the page I have the following:
    
    
    StringBuilder sb = new StringBuilder();
    
    
    sb.Append("<script type=\"text/javascript\" language=\"javascript\">");
    sb.Append("var myVar = '");
    sb.Append(valid);
    sb.Append("'");
    sb.Append("</script>");
    
     this.Page.ClientScript.RegisterClientScriptInclude("AjaxJScript.js", "C:/Documents and Settings/Fonzi/My Documents/Visual Studio 2005/WebSites/Central Reservations System/JavaScript/AjaxJScript.js");
             
            this.Page.ClientScript.RegisterClientScriptBlock(GetType(),"doSomething()", sb.ToString());
    
    

    I set a variable "valid" to true in the page

    
     protected Boolean valid=true;
    
    


    In the javascript file "AjaxJScript.js" I have the following javascript function:


    
    function doSomething()
    {
    alert(myVar);
    }
    

    It displays the value from the C# page ("valid") but I cannot pass it back when this variable changes as it only passes it to the javascript under the page_load event but in my page the variable changes according to what the user clicks on the page.

    Can anyone help me?

    Thank you


Comments

  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Being honest you have confused me on what you want to do

    Are you doing postbacks when the user clicks??


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Ginger wrote: »
    Being honest you have confused me on what you want to do

    Are you doing postbacks when the user clicks??

    No, that's just the point;I don't want to do it using a post back.
    I want to pass the C# variable "valid" to the doSomething function in the jscript file AjaxJScript.js but valid is not set until after the page is loaded (the user sets it).
    This value gets set when the text in a text box is changed so it is set under the TextChanged event of that text box.
    But as it stands I can only send the value if that RegisterClientScriptBlock code is in the Page Load event. That is no good as that variable is sent under page Load


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Right right right.. i think i got it

    You have server variables that you need to update from client script without a postback (round trip to the server)

    You could use a Ajax enabled web service to do this or do some partial postbacks to get this info...


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Ginger wrote: »
    Right right right.. i think i got it

    You have server variables that you need to update from client script without a postback (round trip to the server)

    You could use a Ajax enabled web service to do this or do some partial postbacks to get this info...

    I could do it with Ajax but Ive done that elsewhere in the site and want to mix it up a bit. I can do it with a partial postback but it would mess up the rest of my site.
    I have an idea of how to do it, I'm not sure if it will work, I'll post it if it does.


  • Registered Users Posts: 4,037 ✭✭✭lukin


    I sorted it by putting a hidden asp text box on the page and placing it within an Update Panel.
    <cc1:UpdatePanel ID="UpdatePanel1" runat="server">
              <ContentTemplate>
               <asp:TextBox ID="readme" CssClass="Setinvisible" runat="server" Enabled="false" Width="30px"></asp:TextBox>
              </ContentTemplate></cc1:UpdatePanel>  
            </td>
    


    The text field that is being changed also had to be within an Update Panel.
     <cc1:UpdatePanel ID="UpdatePanel3" runat="server">
              <ContentTemplate>
              <asp:TextBox ID="txtHotelCounty" runat="server" AutoPostBack="true" Width="163px" OnTextChanged="txtHotelCounty_TextChanged"></asp:TextBox>&nbsp;
               </ContentTemplate></cc1:UpdatePanel>
    
    

    The "TextChanged" event of the txtHotelCounty text box set the hidden text field to display some text (doesn't matter what) if the text that was entered by the user was not correct (it has to be one of the 32 counties).

    Then in my javascript I checked the value of this hidden text box with document.getElementById and if there was text there it prompts the user to enter a correct county.
    The update panels only allow whatever is within them (and not the entire page) being sent back to the server. There has to be a ScriptManager on the page and thsi has to have EnablePartialRendering set to true:


    <cc1:ScriptManager ID="ScriptManager1" runat="server" EnablePartialRendering="true" />


  • Advertisement
  • Registered Users Posts: 2,931 ✭✭✭Ginger


    partial updates still do a postback mate


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Ginger wrote: »
    partial updates still do a postback mate

    Yeah but it doesn't refresh the page, which is what I want. I actually would have preferred to do it by passing the C# variable to the javascript but I'm under a bit of pressure so I had to do it this way.


  • Registered Users Posts: 2,931 ✭✭✭Ginger


    Because you said originally no postbacks, I took that you couldnt use update panels or such devices.. :rolleyes:


  • Registered Users Posts: 4,037 ✭✭✭lukin


    Ginger wrote: »
    Because you said originally no postbacks, I took that you couldnt use update panels or such devices.. :rolleyes:

    Aye,probably should have made myself clearer, a "page refresh" and a "postback" are not the same thing.


  • Moderators, Science, Health & Environment Moderators Posts: 8,954 Mod ✭✭✭✭mewso


    lukin wrote: »
    Aye,probably should have made myself clearer, a "page refresh" and a "postback" are not the same thing.

    In terms of an update panel they pretty much are. This seems like an extreme solution to a simple problem.

    For starters consider one of the top tips for javascript - global variables are evil - and it'll get you on the right path. (Don't even get me started on how evil I think MS AJax is)

    If I want to indicate to both the user and my client script the status of an input I could use the class attribute, setting it to class="valid" or class="invalid" depending on it's value. I can do this both on the client or on the server (mytextbox.cssClass="valid"). Client script can check to see if the class attribute contains invalid or not:-
    if (element.className.indexOf("invalid") > -1)   {
       //contents of textbox are invalid
    } else {
       //contents of textbox are valid
    }
    

    Use the class attribute also means I can style the input depending on it's validity and let the user know. If I don't want to post back to the server then these days I'll use jQuery to do an ajax call to the same page to check what the class should be set as. Doing this kind of things these days is pretty trivial and avoids the Update Panel and all it brings with it.


  • Advertisement
Advertisement