Wrox Programmer Forums

Need to download code?

View our list of code downloads.

Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
Password Reminder
Register
Register | FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of tens of thousands of software programmers and website developers including Wrox book authors and readers. As a guest, you can read any forum posting. By joining today you can post your own programming questions, respond to other developers’ questions, and eliminate the ads that are displayed to guests. Registration is fast, simple and absolutely free .
DRM-free e-books 300x50
Reply
 
Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old November 17th, 2007, 03:55 PM
SV SV is offline
Authorized User
 
Join Date: Oct 2007
Location: , , .
Posts: 14
Thanks: 0
Thanked 0 Times in 0 Posts
Default Problem getting value out of a custom webcontrol

I have the following web control. When I tab out of the TextBox of this control, I have an alert to display the value of the control. However, when the alert in the ClientCallback() is executed, it displays undefined. This means I some how do not get the control. And suggestions? Looks like there is some problem with the way I am gettting the value out of the control.

Here is the javascript function which gets generated:

function ClientCallback()
{
 alert(document.getElementById('WebCustomControl1_1 ').value);
 args = document.getElementById('WebCustomControl1_1').val ue;
 WebForm_DoCallback('WebCustomControl1_1',args,Call backHandler,null,ErrorHandler,true)

}


Here is the code for this control:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace WebControlLibrary1
{

    [DefaultProperty("Text")]
    [ToolboxData("<{0}:WebCustomControl1 runat=server></{0}:WebCustomControl1>")]
    public class WebCustomControl1 : WebControl, ICallbackEventHandler
    {
        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        [Themeable(false)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? String.Empty : s);
            }

            set
            {
                ViewState["Text"] = value;
            }

        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.RenderBeginTag(HtmlTextWriterTag.Div);

            output.AddAttribute(HtmlTextWriterAttribute.Type, "text");
            output.AddAttribute(HtmlTextWriterAttribute.Id, this.ClientID);
            output.AddAttribute(HtmlTextWriterAttribute.Name, String.Concat(this.ClientID, "zz"));
            output.AddAttribute(HtmlTextWriterAttribute.Value, this.Text);
            output.AddAttribute("OnBlur", "ClientCallback()");
            output.RenderBeginTag(HtmlTextWriterTag.Input);
            AddAttributesToRender(output);

            output.RenderEndTag();
            output.RenderEndTag();

        }

        protected override void OnPreRender(EventArgs e)
        {
            Page.ClientScript.RegisterClientScriptResource(typ eof(WebControlLibrary1.WebCustomControl1), "WebControlLibrary1.Utility.js");



            Page.ClientScript.RegisterClientScriptBlock(
                typeof(Page),
                "ClientCallback",
                "function ClientCallback() { alert(document.getElementById('" + this.ClientID + "').value); " +
                "args = document.getElementById('" + this.ClientID + "').value;" +
                Page.ClientScript.GetCallbackEventReference(this, "args", "CallbackHandler", null, "ErrorHandler", true) + "}",
                true);
        }

        public void RaiseCallbackEvent(string eventArguments)
        {
            int result;
            if (!Int32.TryParse(eventArguments, out result))
            {
                throw new Exception("The method or operation not implemented");
            }

        }

        public string GetCallbackResult()
        {
            return "Valid Data";
        }
    }
}


Javascript file:

function ValidateText(ctl)
{
    if (ctl.value=='') {
        alert('Please enter a value.');
        ctl.focus();
    }
}

function CallbackHandler(args,ctx)
{
    alert("The data is valid");
}

function ErrorHandler(args,ctx)
{
    alert("Please enter a number");
}


:(:(
Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
deploying custom webcontrol pratik28 General .NET 0 June 10th, 2006 01:08 PM
problem applying a style to a webcontrol ACE2084 VS.NET 2002/2003 3 March 10th, 2005 02:10 PM
problem with UI Webcontrol-treeview hawing General .NET 1 December 30th, 2004 11:19 AM
Treeview Webcontrol problem Victor ADO.NET 2 September 9th, 2004 10:55 AM
help in TreeView Webcontrol Victor .NET Web Services 1 September 25th, 2003 06:06 PM



All times are GMT -4. The time now is 11:37 AM.


Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.