+ -
当前位置:首页 → 问答吧 → listBox后台绑定的方法 到底放在那个位置

listBox后台绑定的方法 到底放在那个位置

时间:2011-12-23

来源:互联网

using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data;
using Gethome.Model;
using Gethome.Web.UI.Control;
using Gethome.Common;
using System.Collections.Generic;

namespace Gethome.Web.AddEvent
{
  public partial class AddEvent2 : Gethome.Web.UI.Page.BasePage
  {
  protected void Page_Load(object sender, EventArgs e)
  {
  if (!Page.IsPostBack) 
  {
   
  }
这个方法到底是放在Page.IsPostBack里面还是外面,放在里面。我点击添加按钮列表就没了。下面还是没取出什么值
  FriendBind();
   
  }
listBox绑定方法
  protected void FriendBind() 
  {
  int UserID = this.GetUserID();
  IList<FriendInfo> list = new Gethome.BLL.User.Friend().GerListFriend(UserID);
  foreach (FriendInfo fd in list)
  {
  ListItem li = new ListItem();
  li.Text = fd.userBasic.RealName;
  li.Value = fd.userBasic.UserID.ToString(); ;
  Lb_Friend.Items.Add(li);
  }
  }

这个地方Lb_Friend.Items[i].Selected 一直是False,一直没进去。急死我了。
  protected void SelectFrind_Click(object sender, EventArgs e)
  {
  string Participant = "";
  string ParticipantId = "";
  for (int i = 0; i < Lb_Friend.Items.Count; i++)
  {
  if (Lb_Friend.Items[i].Selected == true)
  {
  int UserID =Convert.ToInt32(Lb_Friend.Items[i].Value);
  UserBasicInfo ub = new Gethome.BLL.User.User().GetUserBasic(UserID);
  Participant += ub.RealName + ",";
  ParticipantId += ub.UserID+",";
  }
  }
  Txt_Participant.Text = Participant;
  Hid_Participant.Value = ParticipantId;
  }
  }
}
大哥大姐们。看的说下了。那个绑定方法是不是有什么不对的。为什么一直是False,取不到值呀,

作者: xcy_wwj   发布时间: 2011-12-23

if (!IsPostBack)  
 {//绑定数据}
可无刷新操作。在外面回传重新绑定

作者: wuyq11   发布时间: 2011-12-23

放在外面,保证由于服务器控件导致的刷新页面后能够重新绑定数据

作者: Return_false   发布时间: 2011-12-23

C# code

protected void Page_Load(object sender, EventArgs e)
  {
  if (IsPostBack)
      return;
  
  //在retrun 后面写你的操作
  
  FriendBind(); 
  }

作者: w290601645   发布时间: 2011-12-23

我放在里面了。可是第一次我的列表有。点击添加后,那个地方还是FALSE,并且列表也没了。空了

作者: xcy_wwj   发布时间: 2011-12-23

放外面。。2楼是正解

作者: jianghui7897   发布时间: 2011-12-23

给控件绑定数据时,请确保放在这代码块里:
if (!Page.IsPostBack)  
  {
   
  }

//如果出现你4楼所说的情况,请检查是否禁止了页面或该控件的状态保存属性
protected void SelectFrind_Click(object sender, EventArgs e)
  {
  Txt_Participant.Text =Lb_Friend.Items.Count.ToString();//你这里尝试显示下拉框的数据项看下,估计是0
}

作者: likevs   发布时间: 2011-12-23

六楼的你说的很对。是个0.那要怎么解决呀,。这个问题我都边工作边想了一个上午了。以前我也没遇到这样的,,求正解

作者: xcy_wwj   发布时间: 2011-12-23