Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Going nuts over calling a JScript function from code behind

Status
Not open for further replies.

Khold

MIS
Jun 22, 2008
25
GB
Hi,

This issue has really bugged me despite looking over forums and trying on some of the methods discussed at various places on the net.

I have a JScript file with some functions which I want to call upon from my code behind page in ASP.NET (C#).

I have tried many methods (RegisterClientScriptBlock, RegisterClientScriptInclude) both inside my function as well as on the Page_Load function, but it doesn't work (returns an "Object expected" error).

I have included the JScript file in the <head> tag of the aspx page.

How do I call a function present in a JScript file from the code behind (it uses some parameters from the code behind)?

I am using .NET 3.0. Thanks all!
 
you cannot execute js from code behind. you can register js from the code behind to the browser. if you reference controls by name, you need to use the ClientID property for server controls (not the ID property). if you want to script to start when the page loads, use the RegisterStartupClientScript method (or something like that).

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I got it working. Thanks for your input.
 
jason said:
you cannot execute js from code behind.

sure you can!

1. Reference your jscript in the head tag as usual
2. create an asp:label at the bottom of your page just above the </form>..
<asp:Label id="lblJSMsg" runat="server" EnableViewState="false" />

3. Write your code. Object errors are usually due to dynamic control names (especially when using Master/Content pages) as rendered. You'll have to handle that in your javascript file, mostly if you used alot of document.getElementById functions

this is just an example, but i use js extensively and perform many js actions after the postback
Code:
    public void Save_LabelingOptions(object sender, EventArgs e)
    {
        try
        {
            //do your deeds
            data.updatePackageShip(Convert.ToInt32(ddlShipOptions.SelectedItem.Value), txtPackagingInstructions.Text);
            alert.jsWrite(lblJSMsg, "showSection(" + divBladeLabelPack.ClientID + ",'divLabelingPackaging','formulations','divOpportunites');");
        }
        catch (Exception ex)
        {
            audits.Audit(ex.Message, "Exception Error", Request.Url.AbsolutePath, Master.UserName, "Save_LabelingOptions()");
        }
    }

Code:
    public void jsWrite(Control writeTo, string msg)
    {
        ((Label)(writeTo)).Text = "<script language='javascript'>" + msg + "</script>";
    }

my js aint pretty but it works for me!
Code:
// JScript File
function showSection(clicked,divToShow,divToHide,divToHide2)
{
    var dynCtrl = clicked.id.substring(0,clicked.id.lastIndexOf("_")+1);
    
    document.getElementById(dynCtrl + 'divBladeFormTab').className = "divTab";
    document.getElementById(dynCtrl + 'divBladeLabelPack').className = "divTab";
    if (document.getElementById(dynCtrl + 'divOpportunitesTab'))
        document.getElementById(dynCtrl + 'divOpportunitesTab').className = "divTab";
    clicked.className = "divTabSelected";
    
    document.getElementById(dynCtrl + divToShow).className = "divTabBodySelected";
    if (document.getElementById(dynCtrl + divToHide))
        document.getElementById(dynCtrl + divToHide).className = "divTabBody";
    if (document.getElementById(dynCtrl + divToHide2))
        document.getElementById(dynCtrl + divToHide2).className = "divTabBody";
}
 
It ain't pretty indeed. I got it working, thanks folks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top