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!

Selecting a Drop Down List Value Based on a Text Box Value

Status
Not open for further replies.

aradia926

MIS
Jul 11, 2005
29
US

I have a formview control (.NET 2.0/VS2005) in which users enter company contact information. When a user enters a postal code in the appropriate text box, the territory for that company needs to be calculated and displayed in a territory drop down list.

I know how to calculate and retrieve the territory from my SQL Server database, but I can't seem to nail down how to trigger the code and don't want the user to have to click a button.

I tried using the OnTextChanged event, but that doesn't seem to be what I want because I need this calculation to be performed prior to saving the record.

Can anyone point me in the right direction?

Thanks in advance!

Lori
 
If I'm understanding you, the only way to do what you want is to either trigger a complete postback with some client side javascript that listens for changes to the textbox, or do some AJAX to retrieve the list as they are typing.
 
why not put all the postal codes in a DB table, then when the user selects the postal code you would do a post back to fill the other ddl.

just a thought

 
Thanks, dvannoy, I guess I could do that but would it slow my application down at all? Also, I'd rather avoid a postback, knowing what I know now, which brings me to...

Ok, so I've been doing some research and determined that since I'm using asp.net 2.0, a client callback should work. (Thanks, Oddball, I found this out by following my nose throught the link you provided.)

That's about all I've figured out.

I've been trying to piece something together from various tutorials, examples etc. from all over the web, but I'm not having much luck. This is mostly because I don't know much about javascript.

I'm desperately trying to learn, and trust me I've been googling furiously and reading everything I can get my hands on (I've always thought that people who don't try to help themselves first really don't deserve the help of others) but I just don't know where to go from here.

This is what I have (mostly taken from
Dim _callBackResult As String

In Page_Load:

Dim callBack As String = Page.ClientScript.GetCallbackEventReference(Me, "arg", "ClientCallback", "context", "ClientCallbackError", False)
Dim clientFunction As String = "function GetChildren(arg, context){ " & callBack & "; }"
Page.ClientScript.RegisterClientScriptBlock(Me.GetType(), "GetChildren", clientFunction, True)


If Page.IsPostBack AndAlso Not Page.IsCallback Then
Dim territory As DropDownList
territory = CType(Me.DetailGrid.FindControl("ddlterritory"), DropDownList)
If _callBackResult <> "" Then
territory.SelectedValue = _callBackResult
end if
End If

Public Function GetCallbackResult() As String Implements System.Web.UI.ICallbackEventHandler.GetCallbackResult
Return _callBackResult
End Function


Public Sub RaiseCallbackEvent(ByVal eventArgument As String) Implements System.Web.UI.ICallbackEventHandler.RaiseCallbackEvent


'Here I get the territoryID for the Company and store it in a variable of the same name

_callBackResult = territoryID

End Sub

In <script> portion of my contentplaceholder:

function ClientCallback(result, context){

'Not really sure what goes here, obviously that is a BIG problem!

}

function ClientCallbackError(result, context){
alert(result);
}

Can anyone explain to me what I need to do to achieve the results I need??
 
as long as there's not thousands of postal codes a post back should not hurt you in anyway.

 
I would have to store every postal code for the US and Canada. I'm not sure what the exact number would be, but it seems like it would be a large one...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top