动态Web用户中的列表框控制回发后丢失的数据

时间:2013-08-28 16:02:04

标签: vb.net user-controls postback

我创建了动态Web用户控件,它有一个列表框和一个文本框。但是,回发后列表框中的数据会丢失,但文本框中的数据不会丢失。

我已经阅读了很多关于网络用户控件的论坛和网页,我了解网络用户控件在这样的回发后重新创建。

你能否告诉我在回复之前获取列表框数据的方法?

非常感谢 “-------------------------------------- 请注意:我的Web用户控件有一个列表和文本框,用户可以通过在文本框中输入值来为列表框添加值(我在javascripts中编写函数以将数据从文本框添加到列表框)。

但是,在主页面中,当我单击按钮保存列表数据时,我的所有页面和我的Web用户控件都被重新加载,因此列表框中的数据也会消失: - (

用于添加Web用户控件的代码VB:

Private Sub WebFormTestValidation_Init(sender As Object, e As EventArgs) Handles Me.Init

        Dim ctrList As wucListPerson
        ctrList = LoadControl("wucListPerson.ascx")
        Me.Panel1.Controls.Add(ctrList)
        ctrList.ID = "wucDynamic"     
        ctrList.wucName = "DYNAMIC TEST"
        ctrList.wucInfo = "DYNAMIC TEST"
    End Sub

'----------------------

Web用户控件的代码VB

Public Class wucListPerson
    Inherits System.Web.UI.UserControl
    Public Property lstPersons As List(Of String)
        Get
            Dim lst As New List(Of String)
            If lstFullName.Items.Count > 0 Then
                For i = 0 To lstFullName.Items.Count - 1
                    lst.Add(CStr(lstFullName.Items(i).Value))
                Next
            End If
            Return lst
        End Get

        Set(ByVal lstValues As List(Of String))
            If lstValues.Count > 0 Then
                For i = 0 To lstValues.Count - 1
                    Dim strArr As String() = Split(CStr(lstValues.Item(i)), "*")
                    Ordre.Text = CStr(strArr(0))
                    Nom.Text = strArr(1)
                    Prenom.Text = strArr(2)
                    hdfIdPersonne.Value = strArr(3)
                    Dim item As New System.Web.UI.WebControls.ListItem
                    item.Text = strArr(0) + "." + strArr(1) + " " + strArr(2)
                    item.Value = CStr(lstValues.Item(i))
                    lstFullName.Items.Add(item)
                Next
            End If
        End Set
    End Property
    Public Property wucName As String
        Get
            Return lblName.Text
        End Get

        Set(ByVal value As String)
            lblName.Text = value

        End Set
    End Property
    Public Property wucInfo As String
        Get
            Return Info.Text

        End Get
        Set(ByVal value As String)
            Info.Text = value
        End Set
    End Property

    Public Sub addPersonne(ByVal ordre As Integer, ByVal nom As String, ByVal prenom As String, idpersonne As Integer)
        'lstName.Items.Add(CStr(ordre) + "." + nom + " " + prenom, CStr(ordre) + "*" + nom + "*" + prenom + "*" + CStr(idpersonne))
        Dim item As New System.Web.UI.WebControls.ListItem
        item.Text = CStr(ordre) + "." + nom + " " + prenom
        item.Value = CStr(ordre) + "*" + nom + "*" + prenom + "*" + CStr(idpersonne)
        lstFullName.Items.Add(item)

    End Sub

    Public Function getList() As List(Of String)
        Dim lst As New List(Of String)
        If lstFullName.Items.Count > 0 Then
            For i = 0 To lstFullName.Items.Count - 1
                lst.Add(CStr(lstFullName.Items(i).Value))
            Next
        End If
        Return lst
    End Function
    Public Sub SetList(ByVal lst As List(Of String))
        If lst.Count > 0 Then
            For i = 0 To lst.Count - 1
                Dim strArr As String() = Split(CStr(lst.Item(i)), "*")
                Ordre.Text = CStr(strArr(0))
                Nom.Text = strArr(1)
                Prenom.Text = strArr(2)
                hdfIdPersonne.Value = strArr(3)
                Dim item As New ListItem
                item.Text = strArr(0) + "." + strArr(1) + " " + strArr(2)
                item.Value = CStr(lst.Item(i))
                lstFullName.Items.Add(item)
            Next
        End If

    End Sub
    Protected uniqueKey As String
    Private Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
        'If Not Page.IsPostBack Then
        '    lblName.Text = wucName
        'End If
        Me.uniqueKey = Guid.NewGuid.ToString("N")
        Me.cmdRight.Attributes("onclick") = ("RemoveItem_" + (uniqueKey + "(); return false;"))
        Me.cmdLeft.Attributes("onclick") = ("AddItem_" + (uniqueKey + "(); return false;"))
        Me.cmdInfor.Attributes("onclick") = ("showHideInfor_" + (uniqueKey + "(); return false;"))
        If Trim(lblName.Text) = "" Then cmdInfor.Visible = False
    End Sub


End Class

'---------------------- 代码Javascript从文本框中获取/删除数据到列表

// ===================

function getMaxOrdre_<%=uniqueKey%>() {

    var listName = document.getElementById("<%=lstFullName.ClientID()%>");
    var lst = listName.options;

    var count = lst.length;
    if (lst.length == 0) {
        return 0;
    }
    var max = 0;
    for (var i = 0; i < count; i++) {
        var item = lst[i].value;
        var FName = getFullName_<%=uniqueKey%>(item);           
        if (parseInt(FName.Ordre) > max) {
            max = FName.Ordre;
        }
    }
    return max;
}
//===================
function getFullName_<%=uniqueKey%>(FullName) {
    if (FullName.length != "0") {
        var temp = new Array();
        temp = FullName.split('*');
        return { Ordre: temp[0], Nom: temp[1], PreNom: temp[2], IdPersonne: temp[3] };
    }
    return { Ordre: 0, Nom: '', PreNom: '', IdPersonne: '0' };
}
//===================
function AddItem_<%=uniqueKey %>() {
    //lstName.BeginUpdate();
    var txtOrdre = document.getElementById("<%=Ordre.ClientID()%>");
    var txtNom = document.getElementById("<%=Nom.ClientID()%>");
    var txtPrenom = document.getElementById("<%=Prenom.ClientID()%>");
    var hdfIdPerson = document.getElementById("<%=hdfIdPersonne.ClientID%>");

    var listName = document.getElementById("<%=lstFullName.ClientID()%>");
    var index = listName.selectedIndex;
    var lst = listName.options;

    var ordre = txtOrdre.value;
    var nom = txtNom.value;
    var prenom = txtPrenom.value;
    var idPerson = hdfIdPerson.value;
    if ((nom.length > 0) || (prenom.length > 0)) {

        var selectBoxOption = document.createElement("option");//create new option
        selectBoxOption.value = ordre + '*' + nom + '*' + prenom + '*' + idPerson;//set option value
        selectBoxOption.text = ordre + '.' + nom + ' ' + prenom;//set option display text
        listName.add(selectBoxOption, null);//add created option to select box. 
    };

   // lstName.EndUpdate();
    //get deafault data
    var ordreMax = getMaxOrdre_<%=uniqueKey%>();

    txtOrdre.value = parseInt(ordreMax) + 1;
    txtNom.value = '';
    txtPrenom.value = '';
    hdfIdPerson.value = '0';


}
//===================
function RemoveItem_<%=uniqueKey %>() {

    //lstName.BeginUpdate();
    var txtOrdre = document.getElementById("<%=Ordre.ClientID()%>");
    var txtNom = document.getElementById("<%=Nom.ClientID()%>");
    var txtPrenom = document.getElementById("<%=Prenom.ClientID()%>");
    var hdfIdPerson = document.getElementById("<%=hdfIdPersonne.ClientID%>");
    var listName = document.getElementById("<%=lstFullName.ClientID()%>");
    var index = listName.selectedIndex;      
    var lst = listName.options;
    if (index >= 0) {
        var FName = getFullName_<%=uniqueKey %>(lst[index].value);
        txtOrdre.value = FName.Ordre;
        txtNom.value = FName.Nom;
        txtPrenom.value = FName.PreNom;
        hdfIdPerson.value = FName.IdPersonne;

        listName.remove(index);
    }
}

1 个答案:

答案 0 :(得分:0)

您的问题是,您要在DOM中添加新项目,但这些更改不一定会在页面刷新后ListBoxPostBack反映出来。我可以提出两个解决方案:

1)添加ListBox到.net中的元素,而不是使用Javascript。

但如果您需要客户端解决方案,那么每次添加项目时您的页面都不会刷新:

2)添加.net HiddenField控件。这些控件的优点是可以从客户端和服务器端轻松访问。您可以在ListItemHiddenField(可能以逗号分隔)中添加每个项目,在服务器端,您将使用此HiddenField而不是ListBox。< / p>