asplistbox的奇怪行为

时间:2014-06-21 22:35:35

标签: c# asp.net listbox

首先我在这里检查了每一个字:

点击questions/6621510

我的问题描述: 我有下一个设计:

<asp:UpdatePanel ID="updPnltmpTermCats" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<div class="alt">
<div class="msgnoteborder">

<div class="panelistbox">
<asp:ListBox ID="lsttmpTermCats" runat="server" DataValueField="ProposalTermCatID" DataTextField="TermCatName"  
Rows="10" SelectionMode="Single"  AutoPostBack="true"    OnSelectedIndexChanged="lsttmpTermCats_SelectedIndexChanged" />
</div>

<div class="panelistbox">
<asp:ListBox ID="lstTermSubCat" runat="server" DataValueField="ProposalTermSubCatID" DataTextField="TermVirtualName"  
Rows="10"  SelectionMode="Single"  OnDataBinding="lstTermSubCat_DataBinding" OnDataBound="lstTermSubCat_DataBound" ViewStateMode="Enabled"  />
<div class="layoutbuttons">
<asp:ImageButton ID="TermSubCatUpBtn" runat="server" ImageUrl="~/Data/SiteImages/up.gif"
CommandName="up" 
CommandArgument="TermSubCat"
SkinID="pageLayoutMoveUp" CssClass="btnup" />

<asp:ImageButton ID="TermSubCatDownBtn" runat="server" ImageUrl="~/Data/SiteImages/dn.gif"
CommandName="down" 
CommandArgument="TermSubCat"
SkinID="pageLayoutMoveDown"
CssClass="btndown" />
</div>
</div>

</div>
</div> 
</ContentTemplate>
</asp:UpdatePanel> 

代码背后:

    protected void lstTermSubCat_DataBound(object sender, EventArgs e)
    {
            int indx = -1;
            indx = lstTermSubCat.SelectedIndex;
            string txt = string.Empty;
            txt = lstTermSubCat.Items[indx].Text;
            lstTermSubCat.Items[indx].Text = Regex.Replace((txt).ToString(), @"<[^>]*>", String.Empty).Replace("\r\n", String.Empty).Trim();

    }

    protected void lstTermSubCat_DataBinding(object sender, EventArgs e)
    {

        // ListItem item = lstTermSubCat.Items. .Item;
        int indx = -1;
        indx = lstTermSubCat.SelectedIndex;
        string txt = string.Empty;
        txt = lstTermSubCat.Items[indx].Text;
        lstTermSubCat.Items[indx].Text = Regex.Replace((txt).ToString(), @"<[^>]*>", String.Empty).Replace("\r\n", String.Empty).Trim();

    }

如果我在第二个列表框中放置OnSelectedIndexChanged它将起作用,但其他事件没有触发!

确定, 这很奇怪! 我做了新的一页 简单的一个: 设计是:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="testlist.aspx.cs" Inherits="UHSRFP.Features.UI.Admin.UHSRFP.Proposal.testlist" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="updPnltmpTermCats" runat="server" UpdateMode="Conditional" >
<ContentTemplate>
<asp:ListBox ID="lsttmpTermCats" runat="server" 
DataValueField="ProposalTermCatID" DataTextField="TermCatName"  
Rows="10" SelectionMode="Single"  AutoPostBack="true"   
OnSelectedIndexChanged="lsttmpTermCats_SelectedIndexChanged"
OnDataBinding="lsttmpTermCats_DataBinding"
OnDataBound="lsttmpTermCats_DataBound" />

<asp:ListBox ID="lstTermSubCat" runat="server" 
DataValueField="ProposalTermSubCatID" DataTextField="TermVirtualName"   
Rows="10" SelectionMode="Single"  AutoPostBack="true"    
OnSelectedIndexChanged="lsttmpTermCats_SelectedIndexChanged"
OnDataBinding="lstTermSubCat_DataBinding" 
OnDataBound="lstTermSubCat_DataBound"
/>

</ContentTemplate>
</asp:UpdatePanel>     
</form>
</body>
</html>

后面的代码是:

using System;
using System.IO;
using System.Web;
using System.Data;
using System.Text;
using System.Text.RegularExpressions;
using System.Web.UI;
using System.Globalization;
using System.Web.UI.WebControls;
using System.Collections;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using System.Web.UI.HtmlControls;
using UHSRFP.Business;
using UHSRFP.Features.UI.Helper;
using mojoPortal.Business;
using mojoPortal.Business.WebHelpers;
using mojoPortal.Web.Framework;
using mojoPortal.Web;
using mojoPortal.Web.AdminUI;
using mojoPortal.Web.Editor;
using mojoPortal.Web.UI;
using Resources;

namespace UHSRFP.Features.UI.Admin.UHSRFP.Proposal
{
    public partial class testlist : System.Web.UI.Page
    {
        private int proposalID = 11;
        private int siteid = 1;
        int termCatID = -1;

        protected void Page_Load(object sender, EventArgs e)
        {




            PopulateControls();




        }



        private void PopulateControls()
        {

            if ((this.proposalID > 0) & (!Page.IsPostBack))
            {

                Bind_tmpTermCatList();
                //Bind_TermSubCatList();

            }

        }

        private void Bind_tmpTermCatList()
        {
            lsttmpTermCats.Items.Clear();
            ArrayList proposalTermCategories = ProposalTermCategory.GetArrayProposalTermCats(this.siteid, this.proposalID);

            foreach (ProposalTermCategory proposalTermCategory in proposalTermCategories)
            {
                ListItem listItem = new ListItem(proposalTermCategory.TermCat.Coalesce(Resource.ContentNoTitle), proposalTermCategory.ProposalTermCatID.ToInvariantString());
                lsttmpTermCats.Items.Add(listItem);
            }

            if (this.lsttmpTermCats.Items.Count > 0)
                this.lsttmpTermCats.SelectedIndex = 0;

            this.termCatID = int.Parse(lsttmpTermCats.SelectedValue);

            ListItem item = lsttmpTermCats.Items.FindByValue(this.termCatID.ToInvariantString());
            if (item != null)
            {
                lsttmpTermCats.ClearSelection();
                item.Selected = true;
            }

            Bind_TermSubCatList();
        }

        private void Bind_TermSubCatList()
        {
            lstTermSubCat.Items.Clear();
            ArrayList proposalTermSubCategories = ProposalTermSubCategory.GetArrayProposalTermSubCats(this.siteid, this.termCatID);

            foreach (ProposalTermSubCategory proposalTermSubCategory in proposalTermSubCategories)
            {
                ListItem listItem = new ListItem(proposalTermSubCategory.TermSubCatDescr.Coalesce(Resource.ContentNoTitle), proposalTermSubCategory.ProposalTermSubCatID.ToInvariantString());
                lstTermSubCat.Items.Add(listItem);
            }
        }

        protected void lstTermSubCat_DataBound(object sender, EventArgs e)
        {

            // ListItem item = lstTermSubCat.Items. .Item;
            int indx = -1;
            indx = lstTermSubCat.SelectedIndex;
            string txt = string.Empty;
            txt = lstTermSubCat.Items[indx].Text;
            lstTermSubCat.Items[indx].Text = Regex.Replace((txt).ToString(), @"<[^>]*>", String.Empty).Replace("\r\n", String.Empty).Trim();

        }

        protected void lstTermSubCat_DataBinding(object sender, EventArgs e)
        {

            // ListItem item = lstTermSubCat.Items. .Item;
            int indx = -1;
            indx = lstTermSubCat.SelectedIndex;
            string txt = string.Empty;
            txt = lstTermSubCat.Items[indx].Text;
            lstTermSubCat.Items[indx].Text = Regex.Replace((txt).ToString(), @"<[^>]*>", String.Empty).Replace("\r\n", String.Empty).Trim();

        }

        protected void lsttmpTermCats_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.termCatID = int.Parse(lsttmpTermCats.SelectedValue);
            Bind_TermSubCatList();
        }

        protected void lsttmpTermCats_DataBinding(object sender, EventArgs e)
        {
            this.termCatID = int.Parse(lsttmpTermCats.SelectedValue);
            //Bind_TermSubCatList();
        }

        protected void lsttmpTermCats_DataBound(object sender, EventArgs e)
        {
            this.termCatID = int.Parse(lsttmpTermCats.SelectedValue);
        }

    }
}

仍然无法正常工作 问题是只有OnDataBinding和OnDataBound事件在索引更改时仍未正常运行!! 请问这里有什么提示吗?

这里的任何帮助都将作为礼物,因为我花了很长时间尝试所有没有结果的东西! 感谢

1 个答案:

答案 0 :(得分:0)

请记住,如果您的控件在页面完全呈现时没有绑定的数据源,则他们不会为您启动DataBinding和DataBound事件。

因此,在控件触发DataBiniding或DataBound事件之前,必须在其控件属性(.aspx文件)或Page_load事件上绑定数据源。

因此,在将数据设置为控件后,您需要使用DataBind()方法。

    private void Bind_tmpTermCatList()
    {
        //your datasource code here

        lsttmpTermCats.DataBind(); //bind your control here
        Bind_TermSubCatList();
    }

在您的子列表中,您将拥有以下内容:

    private void Bind_TermSubCatList()
    {
        // your datasource code here...
        lstTermSubCat.DataBind();  //bind your sub control here
    }

现在您将看到两个事件lsttmpTermCats_DataBindinglsttmpTermCats_DataBound随后将会触发。

干杯