1

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

我已经阅读了很多关于 Web 用户控件的论坛和页面,并且我了解 Web 用户控件在此类回发后会重新创建。

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

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

但是,在主页面中,当我单击一个按钮保存列表数据时,我的所有页面和我的 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

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

网页用户控件代码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);
    }
}
4

1 回答 1

0

Your problem is that you are adding the new items in the DOM, but these changes will not necessarily be reflected in your ListBox after a page refresh or PostBack. I could suggest two solutions:

1) Add the elements in the ListBox through .net instead of using Javascript.

but if you need a client side solution so that your page won't refresh everytime you add an item:

2) Add a .net HiddenField control. These controls have the advantage of being accessible easily from both Client Side and Server Side. You can add each item in both, the ListItem and the HiddenField (maybe comma separated) and on the server side you will use this HiddenField instead of your ListBox.

于 2013-08-29T08:44:29.970 回答