Viewstate not working for listboxes...
Messages   Related Types
This message was discovered on ASPFriends.com 'aspngfreeforall' list.


Greg Quinn
I have two listbox controls on a page that are databound when the page loads
the first time, if the page is posted back, the controls are not rebound
again because the viewstate should keep the listbox information.

Well this is not happening, when I call a server-side function to read the
items sitting in the listbox, the listboxes reset to their original values,
even though I have checked for a postback.

What is happening here? this doesn't make sense...

Greg

Reply to this message...
 
    
Greg Quinn
-- Moved from [aspngwebcontrols] to [aspngfreeforall] by Yannick Smits <Click here to reveal e-mail address> --

I have two listbox controls on a page that are databound when the page loads
the first time, if the page is posted back, the controls are not rebound
again because the viewstate should keep the listbox information.

Well this is not happening, when I call a server-side function to read the
items sitting in the listbox, the listboxes reset to their original values,
even though I have checked for a postback.

What is happening here? this doesn't make sense...

Greg

Reply to this message...
 
    
Alex Lowe
Need to see some or all of the code.

Alex - AspFriends.com Moderation Team
Microsoft MVP - ASP.NET

***********************************************************
Translate C# code to VB.NET code at
http://aspalliance.com/aldotnet/examples/translate.aspx
***********************************************************

[Original message clipped]

Reply to this message...
 
    
Greg Quinn
I'll give it all to you :)

----------- company_share.aspx

<%@ Import Namespace = "System.Data" %>
<%@ Import Namespace = "System.Data.SqlClient" %>

<script language = "vb" runat = "server">
Sub Page_Load()
    If Not Page.IsPostBack Then
        PopulateLists()
    End If
End Sub

Sub PopulateLists()
    ' Bind Left List
    If Not Page.IsPostBack Then
    Dim objConnection = New
SQLConnection(ConfigurationSettings.AppSettings("connString"))

    Dim SqlBindLeftList as String
    SqlBindLeftList = "Select Users_UserID, (Users_Fname + char(32) +
Users_LName) as ExprJoinedName From Officium_Users WHERE Users_UserID <> 1
ORDER BY Users_Fname"
    Dim objCommand as New SQLCommand(SqlBindLeftList, objConnection)
    objConnection.Open

    Dim objDataReaderBindLeftList as SqlDataReader
    objDataReaderBindLeftList = objCommand.ExecuteReader()

    list1.DataSource = objDataReaderBindLeftList
    list1.DataBind()

    objDataReaderBindLeftList.Close()
    objConnection.Close()
    End If
End Sub

Sub UpdateShare(Sender as Object, E as EventArgs)
        ' Update share list for contact

        Dim objConnection = New
SQLConnection(ConfigurationSettings.AppSettings("connString"))

        ' First delete existing record

        Dim strSQLDeleteExistingShare as String
        strSQLDeleteExistingShare = "Delete From Officium_ContactsShareLookup
WHERE ContactID = 194 AND UserID = 1"
        Dim myCommandShareDelete as New SQLCommand(strSQLDeleteExistingShare,
objConnection)

        myCommandShareDelete.Connection.Open()
        myCommandShareDelete.ExecuteNonQuery()
        myCommandShareDelete.Connection.Close()

        ' Add new share information to Officium_ContactsShareLookup

        Dim newIDConstructor as new NextIDClass
        Dim newShareID as Integer

        newShareID = newIDConstructor.Get_Next_ID("Officium_ContactsShareLookup",
"ContactsShareID")

        Dim shareString as String
        Dim validItems as Integer
For validItems = 0 to list2.Items.Count() - 1
    shareString += "," & list2.items(validItems).Value
Next

        Dim strSQLInsertShare as String
        strSQLInsertShare = "INSERT INTO Officium_ContactsShareLookup
(ContactsShareID, UserID, ContactID, ShareList) VALUES "
        strSQLInsertShare += "(" & newShareID & ","
        strSQLInsertShare += "1,"
        strSQLInsertShare += "194,"
        strSQLInsertShare += "'" & shareString & "')"

        Dim myCommandShareInsert as New SQLCommand(strSQLInsertShare,
objConnection)

        myCommandShareInsert.Connection.Open()
        myCommandShareInsert.ExecuteNonQuery()
        myCommandShareInsert.Connection.Close()

        lblMessage.Text = strSQLInsertShare
End Sub
</script>
<HEAD>
<title>Officium</title>
<SCRIPT LANGUAGE="JavaScript" src = "../scripts/company_share2.js"></script>
<link href="../include/forms.css" rel="stylesheet" type="text/css">
</HEAD>
<BODY bgcolor="#FFFFFF">
<asp:label id = "lblMessage" runat = "server" />
<form id="shareForm" runat = "server">
<table>
<tr>
<td colspan="3">Select the users to share this item with...</td>
</tr>
<tr>
<td> </td>
<td align="center" valign="middle"> </td>
<td> </td>
</tr>
<tr>
<td>
<asp:listbox id = "list1" Style = "width:200px;height:350px;"
DataValueField = "Users_UserID" DataTextField = "ExprJoinedName" cssStyle =
"width : 150px" selectionMode = "multiple" runat = "server" />
</td>
<td align="center" valign="middle">
<input type="button" onClick="move(this.form.list2,this.form.list1)"
value="<<">
<input type="button" onClick="move(this.form.list1,this.form.list2)"
value=">>">
</td>
<td>
<asp:listbox id = "list2" Style = "width:200px;height:350px;"
DataValueField = "Users_UserID" DataTextField = "ExprJoinedName" cssStyle =
"width : 150px" selectionMode = "multiple" runat = "server" />
</td>
</tr>
<tr>
<td> </td>
<td align="center" valign="middle">
    <asp:button id = "updateBut" Text = "Save" onClick = "UpdateShare"
runat = "server" />
</td>
<td> </td>
</tr>
</table>
</form>
</body>

company_share2.js --------------------------------

function move(fbox, tbox) {
var arrFbox = new Array();
var arrTbox = new Array();
var arrLookup = new Array();
var i;
for (i = 0; i < tbox.options.length; i++) {
arrLookup[tbox.options[i].text] = tbox.options[i].value;
arrTbox[i] = tbox.options[i].text;
}
var fLength = 0;
var tLength = arrTbox.length;
for(i = 0; i < fbox.options.length; i++) {
arrLookup[fbox.options[i].text] = fbox.options[i].value;
if (fbox.options[i].selected && fbox.options[i].value != "") {
arrTbox[tLength] = fbox.options[i].text;
tLength++;
}
else {
arrFbox[fLength] = fbox.options[i].text;
fLength++;
}
}
arrFbox.sort();
arrTbox.sort();
fbox.length = 0;
tbox.length = 0;
var c;
for(c = 0; c < arrFbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrFbox[c]];
no.text = arrFbox[c];
fbox[c] = no;
}
for(c = 0; c < arrTbox.length; c++) {
var no = new Option();
no.value = arrLookup[arrTbox[c]];
no.text = arrTbox[c];
tbox[c] = no;
}

}

Reply to this message...
 
    
Dumitru Sbenghe
The listbox store in ViewState only the selected item (I guess only the
index), not all the items from list, so he knows only to restore the
selected item, not the whole list. Is a little bit strange but is logic.

To mantain in ViewState all the items you can write a new ListBox derived
from ListBox or HtmlSelect, but you must handle in the same time the
possibility that the items from list box be modified on client using
javascript code.

Dumitru Sbenghe

[Original message clipped]

Reply to this message...
 
    
Greg Quinn
Yip, that makes sense, but then what I don't understand if I've disabled
viewstate on the page, there is a still a VIEWSTATE control with encrypted
data in my source??? Should it be there?

If I take off the viewstate from the page, then why shouldn't my Update
function work? surely it should take the new values inserted via Javascript
into the listbox and send those to the update function?

By the way, I assume (Sender as Object, E as EventArgs) is the correct
arguments to pass to my Update function?

Thanks
Greg

-----Original Message-----
From: Dumitru Sbenghe [mailto:Click here to reveal e-mail address]
Sent: Thursday, August 15, 2002 4:58 AM
To: aspngfreeforall
Subject: [aspngfreeforall] RE: Viewstate not working for listboxes...

The listbox store in ViewState only the selected item (I guess only the
index), not all the items from list, so he knows only to restore the
selected item, not the whole list. Is a little bit strange but is logic.

To mantain in ViewState all the items you can write a new ListBox derived
from ListBox or HtmlSelect, but you must handle in the same time the
possibility that the items from list box be modified on client using
javascript code.

Dumitru Sbenghe

[Original message clipped]

| ASP.net DOCS = http://www.aspng.com/docs
| [aspngfreeforall] member Click here to reveal e-mail address = YOUR ID
| http://www.asplists.com/aspngfreeforall = JOIN/QUIT
| news://ls.asplists.com = NEWSGROUP

Reply to this message...
 
 
System.Array
System.Configuration.ConfigurationSettings
System.Data.SqlClient.SqlDataReader
System.EventArgs
System.Web.UI.HtmlControls.HtmlSelect
System.Web.UI.Page
System.Web.UI.WebControls.ListBox
System.Windows.Forms.ListBox




Ad
MBR BootFX
Best-of-breed application framework for .NET projects, developed by Matthew Baxter-Reynolds and MBR IT
 
 Copyright © Matthew Baxter-Reynolds 2001-2008. '.NET 247 Software Development Services' is a trading style of MBR IT Solutions Ltd.
Contact Us - Terms of Use - Privacy Policy - www.dotnet247.com