?
Solved

How to update a dropdown when a text field is updated automatically

Posted on 2008-10-31
4
Medium Priority
?
163 Views
Last Modified: 2013-12-17
I have a file selector that when a file is selected a text box is populated.  When that text box is updated through code I need a dropdown populated.  My question is how can I get the code to know when a text field is updated?  I have tried TextBox1_TextChanged but that only triggers if I manually update the text field.
0
Comment
Question by:tmctogo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
4 Comments
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22851696
You can set the "AutoPostBack" property of the text box control to "True" and it will automatically generate a post back when the text is changed.

If you need a custom JavaScript postback, then you get an event reference via the Page.ClientScript object.
0
 

Author Comment

by:tmctogo
ID: 22851728
I already have AutoPostBack set to true.  The only time that the process for populating the dropdown is started is when I change the text in the field manually.  Since I am using a fileupload control, it does not have postbacks so I populate a text box with the value from the fileupload control.
0
 
LVL 13

Expert Comment

by:numberkruncher
ID: 22851749
Okay, so it sounds like you need a custom post back. Give me a few minutes and I will post you an example which allows you to do this.
0
 
LVL 13

Accepted Solution

by:
numberkruncher earned 500 total points
ID: 22851910
First an explanation of how the code below works:

A custom post back event has been registered via the ClientScript object. The post back is registered for the page itself. But in-order to be able to respond to this post back you must implement the 'IPostBackEventHandler' interface. Your special server-side post back code goes in the method provided there.

In the example I have demonstrated how to access the text box value.
In the HTML section of the website:
 
    <script type="text/javascript">
        function myTestFunction() {
            // Simply call the following function to do postback.
            firePostback('MyTextChanged');
        }
    </script>
 
I used the following to test this:
 
    <input type="button" value="Test" onclick="myTestFunction()" />
 
In your code file:
 
 
public partial class Default5 : System.Web.UI.Page, IPostBackEventHandler
{
    protected void Page_Load(object sender, EventArgs e)
    {
        // Register the event reference.
        // "@XXX@" gets replaced with the name attribute of firePostback.
        //      i.e. firePosback('boo')   replaces "@XXX@" with "boo".
        string customEventReference = ClientScript.GetPostBackEventReference(this, "@XXX@");
        // Register the post back function.
        ClientScript.RegisterClientScriptBlock(GetType(), "DoTextChanged",
            string.Format("function firePostback(name) {{ eval(\"{0}\".replace('@XXX@', name)); }}", customEventReference), true);
    }
 
 
    #region IPostBackEventHandler Members
 
    public void RaisePostBackEvent(string eventArgument)
    {
        // Check for the argument which was provided to our post back function.
        if (eventArgument == "MyTextChanged")
        {
            string textBoxValue = TextBox1.Text;
        }
    }
 
    #endregion
}

Open in new window

0

Featured Post

Secure Your WordPress Site: 5 Essential Approaches

WordPress is the web's most popular CMS, but its dominance also makes it a target for attackers. Our eBook will show you how to:

Prevent costly exploits of core and plugin vulnerabilities
Repel automated attacks
Lock down your dashboard, secure your code, and protect your users

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Although a lot of people devote their energy toward marketing for specific industries, there are some basic principles that can be applied to any sector imaginable. We’ll look at four steps to take and examine how those steps were put into action fo…
Australian government abolished Visa 457 earlier this April and this article describes how this decision might affect Australian IT scene and IT experts.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
Suggested Courses

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question