PDA

View Full Version : Listbox WebControl Challenge


aCcodeMonkey
01-03-2003, 02:39 AM
Hi All It's me again.

Gotta problem with a mail distribution list management web form that I'm migrating to ASP.Net. The original form used ASP and the RDC.DataControl to populate the form objects via an XML Stream.

Originallythis was no problem as the client was using only IE. Now they require that the form function in both IE and NS7. Thus ASP.Net.

I have a Listbox webcontrol that can be populated either by Server-side Databinding:

cmbMailList.DataSource = oDS.Tables("DistGroup2Member").DefaultView
cmbMailList.DataTextField = "LongName"
cmbMailList.DataValueField = "ListValue"
cmbMailList.DataBind()

or

Client-Side DOM Script:


// add all selected list items from both selection objects
oSelectTarget=document.getElementById('cmbMailList');
var aFields = new Array('cmbMembers','cmbProspects');
for(i=0;i<2;i++){
var oSelectSource=document.getElementById(aFields[i]);
for(nSelectedIndex=(oSelectSource.options.length-1);nSelectedIndex>=0;nSelectedIndex--){
if(oSelectSource.options[nSelectedIndex].selected){
sSourceValue=(i==0?oSelectSource.options[nSelectedIndex].value+'|0': oSelectSource.options[nSelectedIndex].value+'|1')
sSourceText=(i==0?oSelectSource.options[nSelectedIndex].text+' (M)': oSelectSource.options[nSelectedIndex].text+' (P)');
oSelectTarget.options[oSelectTarget.length]=new Option(sSourceText,sSourceValue)
oSelectSource.remove(nSelectedIndex);
}
}
}



When I run the following server-side code:

'******************************************
'********** Problem Code ***************/
Dim sItem as Object
For Each sItem In cmbMailList.Items
' *** Debug Print ****
Response.write(sItem.Value.ToString() & "<BR>")

sIndexID=Left(sItem.Value.ToString(),(Len(sItem.Value.ToString())-2))
nSourceID=right(sItem.Value.ToString(),1)
sSQL="INSERT INTO DistGroup2Member " & _
"(MailGroupID,MemberID,Source) VALUES(" & _
sGrpIndexID & "," & sIndexID & "," & nSourceID & ")"
'bUpdateStatus = Execute_Sql(sSQL,1)
Next
'******************************************
'******************************************


Only the originally bound data is returned. No client-side changes are reflected


If I use server-side HTTPRequest code:

'******************************************
'********** Problem Code ***************/
Dim sItem as Object
For Each sItem In Request.Form("cmbMailList")
' *** Debug Print ****
Response.write(sItem.Value.ToString() & "<BR>")

sIndexID=Left(sItem.Value.ToString(),(Len(sItem.Value.ToString())-2))
nSourceID=right(sItem.Value.ToString(),1)
sSQL="INSERT INTO DistGroup2Member " & _
"(MailGroupID,MemberID,Source) VALUES(" & _
sGrpIndexID & "," & sIndexID & "," & nSourceID & ")"
'bUpdateStatus = Execute_Sql(sSQL,1)
Next
'******************************************
'******************************************


I get a comma separated String instead of a collection e.g. 207|0,231|0,238|0,237|0,246|0

I can parse the string but for large mailing lists this will really slow down the process.

I've attached the working file to this so you can really see what's going on with the form.

Basically I am trying to avoid round-tripping to populate the cmbMailList Object.

Gotta Love a Challenge.....

aCcodeMonkey
01-04-2003, 03:32 AM
I guess yesterday was a day of brain death.

I forgot that VB.NET now has a String.Split() function that can parse a string and create an array. :eek:

Syntax: Dim myArray As String() = Split(String, Delimiter)

In the form's code, the posed ListBox control 's ListItem value string that is looks like: "999|0,123|1,789|0"

Now I can loop thru the values and post each selection to the database as shown below.


Dim aNewList as String() = Split(Request.Form("cmbMailList"), ",")
Dim i As Integer
For i = 0 To aNewList.Length - 1
sSQL="INSERT INTO DistGroup2Member " & _
"(MailGroupID,MemberID,Source) VALUES(" & _
sGrpIndexID & "," &+
CInt(Left(aNewList(i),(Len(aNewList(i))-2))) & "," & _
CInt(right(aNewList(i),1)) & ")"
bUpdateStatus = Execute_Sql(sSQL,1)
Next



Now if I can just get the DataGrid to format the way I want it..... :p