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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ASP.NET AJAX, UpdatePanel failed to update the ASP Label Control 1

Status
Not open for further replies.

fletchsod

Programmer
Dec 16, 2002
181
I don't understand why the lblJavaScript1 does not work when the script is complete. When I ran the debugger, it showed the new data is updated in the lblJavaScript1 but when it is complete and a web browser appear, the view-source show there is no data in the lblJavaScript1.

Can anyone point out why it is broken? How do you make it work?


Code:
//Web.Config
<pages>
<controls>
		<add tagPrefix="asp" namespace="System.Web.UI" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
		<add tagPrefix="asp" namespace="System.Web.UI.WebControls" assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
		<add namespace="AjaxControlToolkit" assembly="AjaxControlToolkit" tagPrefix="ajaxToolkit"/>
	</controls>
</pages>

Code:
//ASPX file

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ContactUs1.aspx.cs" Inherits="contactus" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "[URL unfurl="true"]http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">[/URL]

<html xmlns="[URL unfurl="true"]http://www.w3.org/1999/xhtml">[/URL]
<head runat="server">
	<title></title>
</head>
<body id="contact-page">
    <form id="form1" runat="server">
    <div>
    
        <!-- ASP.NET AJAX Script Only... -->
        <asp:ScriptManager ID="ScriptManager1" runat="server" />

        <asp:UpdatePanel ID="UpdatePanel1" runat="server">
            <ContentTemplate>
                  						
			        <asp:textbox id="txtName" 
                        runat="server" Width="176px"></asp:textbox>
                        
                    <br /><br />
                                                           
                    <asp:Button ID="btnSubmit" runat="server" 
                        onClick="btnSubmit_Click" 
                        Text="Submit" Width="72px" />
					
					<br /><br />
			
                    <asp:Label ID="lblJavaScript1" runat="server"></asp:Label>    
                    
            </ContentTemplate>
            <Triggers>
                <asp:AsyncPostBackTrigger ControlID="btnSubmit" EventName="Click" />
            </Triggers>
        </asp:UpdatePanel>

    </div>
    </form>
</body>
</html>

Code:
//ASPX Code Behind

//Code Behind (ASPX.cs webpage)...
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

public partial class contactus : System.Web.UI.Page
{
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string sName = this.txtName.Text.ToString().Trim();

        if (sName.Length == 0)
        {
            this.lblJavaScriptValidation1.Text = "<script type='text/javascript'>alert('Test');</script>";
            this.txtName.BackColor = System.Drawing.Color.LemonChiffon;
            return;  //This exit the function...
        }
    }
    protected void Page_Load(object sender, EventArgs e)
    {
        this.lblJavaScriptValidation1.Text = "";  //Clear the validation error msg...

        //Put user code to initialize the page here
        if (!(Page.IsPostBack))
        {
            this.txtName.Text = "";
        }
    }
}

Thanks...
 
why are you putting javascript into a label? webforms is very stringent about injecting scripts. most of the time it's used for malicious purposes. if you want to inject javascript into the page you need to use the ClientScriptManager to add it.

for now just put text into the label, no scripts.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Ah! I didn't know that ClientScriptManager is used for that! I tested it and it works now. Wow!

I'm still learning with AJAX. I have done a few simple AJAX with toolkit and it is nice. Then it doesn't work when I added more stuffs to the ASP.NET webpage, that is when I realized I can't depend on the toolkit and had to create javascript functions/objects and have ASP.NET called it instead. That javascript alert() and Label is my 1st attempt on getting something like this to work until you mentioned ClientScriptManager and I found a sample testcase via Google surfing.

Code:
Guid oGuide = new Guid.NewGuide();
ScriptManager.RegisterStartupScript(this.UpdatePanel1, this.UpdatePanel1.GetType(), oGuide.ToString(), "alert('Hello World!');", true);

Thanks man! ASP.NET AJAX is a bit stressful for me cuz it is new to me.
 
ms ajax is the most complex of the ajax engines. it makes clientside scripting a server side concern with webserver controls.

I would recommend using jquery for the ajax/client script needs.

also as an FYI: the updatepanel does not provide any performance benefits. it still performs a full postback, it's simply wrapped in ajax call. the appearance of the postback is removed but it happens, viewstate and all. there is a wickcode article from MSDN magazine on the topic from 2-3 years ago.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Yea! I have heard of performance issues with UpdatePanel. It is something I will have to figure out down the road.

Yea, I heard of jquery but I'm not too sure if I can do the javascript hacking to make it work better for cross-browser. So, I find it easier with normal javascript. I have done lot of javascript and javascript's AJAX for PHP websites.

Right now the website I ahve been working on for 4 years is getting too complex, so I'm gonna keep it simple while I can. My project right now is to get rid of postback (button, drop-down selection, etc) cuz some webpages now have like 4 buttons, 3 drop-down selection, 2 checkboxes, etc. Image the headache when someone play around with it or use the browser refresh button, back button, etc. Argh!!

Crazy...
 
Yea, I heard of jquery but I'm not too sure if I can do the javascript hacking to make it work better for cross-browser.
then you don't understand what jquery (or prototype, mootools or scriptolicious) do. the core purpose of these js libraries is to abstract the browser idiosyncrasies away from development. there is no hacking with jquery. take a quick spin around and to see examples of what is possible out of the box.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top