Using a Dataset to populate javascript array

Eduardo Lorenzo
12-18-2006, 09:59 PM
does anyone know how to do this?

function namelist() {
this.names= [];
}

how can I put the contents of a datatable (populated using serverside) inside the names array declared in javascript?

all suggestions are appreciated.

wayneph
12-19-2006, 07:29 AM
I guess you could use a Repeater, and just put asp:Literal controls in it. Or build it in the code behind and loop through it manually.

Eduardo Lorenzo
12-19-2006, 04:51 PM
I think I should.

What I am trying to build now is an invisible listbox and point the contents to the java array. problem is, the java function is called during window.onload and I am populating the listbox on the page_load. This is wrong.. right?

wayneph
12-20-2006, 07:53 AM
you're going to have to be a lot more specific. i can't see your code to understand your question.

how are you calling the function? Have you tried using Page.RegisterStartUpScript() to add the JS code to your page?

Eduardo Lorenzo
12-26-2006, 04:33 PM
well this is how I call the function

<script type="text/javascript">
window.onload = function () {
var oTextbox = new AutoSuggestControl(document.getElementById("txtremlname"), new NameList());
}
</script>

but this is during the .onload event.
and hte problem is, the dataset is poputaed during the Page_Load event.

Eduardo Lorenzo
12-26-2006, 10:13 PM
ok I have updated a little and this is what I have right now:

Private Sub suggestions()
Dim ws As New obpwebservice.remitter
Dim myDS As New DataSet
Dim strSuggest As New StringBuilder
Dim lnCtr As Int16
Dim stringko As String
Try
ws.getRemitterByName(myDS, " ", "")
For lnCtr = 0 To myDS.Tables(0).Rows.Count - 1
stringko = stringko & "''" & myDS.Tables(0).Rows(lnCtr)("lastname") & "'',"
Next
txtSuggestions.Text = Mid(stringko, 3, Len(stringko) - 4)
Catch ex As Exception
Me.lbl_Error.Text = ws.errormsg
End Try
ws = Nothing
End Sub

this is how I create stringko and place it inside a textbox (txtSuggestions)

and then

I have this:

<script type="text/javascript">
window.onload = function () {
var oTextbox = new AutoSuggestControl(document.getElementById("txtremlname"), new NameList());
}
</script>

in the HTML code..
and this:

function NameList()
{
this.Names = [document.getElementById("txtSuggestions.value")];
}

is what Namelist now looks like.

I am in total darkness as to which these functions/procedures are fired first. :confused:

wayneph
12-27-2006, 09:03 AM
you're missing one key point. your dataset is on the serverside, but the rest of your code is running on the client side.

also your NameList function isn't using a valid element. I assume you're binding your DataSet to the txtSuggestions control? but to get anything, you'll need to use document.getElementById("txtSuggestions"), not txtSuggestions.value. That's invalid JavaScript no matter what you're trying to do.

Eduardo Lorenzo
01-09-2007, 12:18 AM
this is where this task got to its final version.
I'm posting this for those who are trying to do this.

This is the server-side code to get all the data.

Private Sub suggestions()
Dim ws As New obpwebservice.remitter
Dim myDS As New DataSet
Dim strSuggest As New StringBuilder
Dim lnCtr As Int16
Dim stringko As String
Try
ws.getRemitterByName(myDS, " ", "")
For lnCtr = 0 To myDS.Tables(0).Rows.Count - 1
stringko = myDS.Tables(0).Rows(lnCtr)("lastname") & ", " & stringko
Next
txtSuggestions.Value = stringko
Hidden1.Value = myDS.Tables(0).Rows.Count
Catch ex As Exception
Me.lbl_Error.Text = ws.errormsg
End Try
ws = Nothing
End Sub

I placed all the values in a Hidden Textbox and added a comma as separator.
then I added this to the HTML code:

<script type="text/javascript">
window.onload = function ()
{
var oTextbox = new AutoSuggestControl(document.getElementById("txtremlname"), new NameList());
}
</script>

this assigns txtremlname as a textbox with "autosuggest feature"
and this is how the data is 'transferred' from the hidden textbox

var Names = new Array();

function NameList()
{

Names.length = parseInt(document.getElementById('Hidden1').value);
var stringer = document.getElementById('txtSuggestions').value;
for (var x = 0; x < Names.length; x++)
{
Names[x] = stringer.substring(0, stringer.indexOf(','));
stringer = stringer.substring(stringer.indexOf(', ') +2, stringer.length);
}
}


NameList.prototype.requestSuggestions = function (oAutoSuggestControl) {
var aSuggestions = [];
var sTextboxValue = oAutoSuggestControl.textbox.value;
if (sTextboxValue.length > 0){
for (var i=0; i < Names.length; i++) {
if (Names[i].indexOf(sTextboxValue) == 0) {
aSuggestions.push(Names[i]);
}
}
}
oAutoSuggestControl.autosuggest(aSuggestions);
};

basically stringer gets the string and adds it to NameList.

I could not have done this without WaynePH's help in using the getelementbyid thing..

thanks wayne.. you da man!

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum