2014-06-21 33 views
0

的行爲首先我檢查了這裏的每一個字:奇怪asplistbox

點擊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在se cond listbox它會起作用,但其他事件不會觸發!

好吧, 它很奇怪! 我做了新的一頁 簡單: 設計是:

<%@ 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是在索引改變的時候仍然沒有開火! 這裏有任何提示嗎?

這裏的任何幫助將作爲禮物,因爲我花了很長時間嘗試每一件事情沒有結果! 謝謝

+0

我的意思是事件:OnDataBinding和OnDataBound不燒詭計OnSelectedIndexChanged被解僱! –

回答

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_DataBound隨後就會觸發。

乾杯

+0

非常感謝你:) –