首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

关于DATALIST控件一个有关问题

2012-08-11 
关于DATALIST控件一个问题using Systemusing System.Configurationusing System.Datausing System.Linq

关于DATALIST控件一个问题
using System;
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;

public partial class _Default : System.Web.UI.Page 
{
  protected void Page_Load(object sender, EventArgs e)
  {
  int begPage = 1;
  try
  {
  begPage = Convert.ToInt32(Request.QueryString["p"]);
  }
  catch
  {
  }
  if (begPage < 1)
  {
  begPage = 1;
  }
  int index = begPage - 1;
  Binders(index);
  hlPre.NavigateUrl = "Default.aspx?p=" + (begPage - 1);
  hlNext.NavigateUrl = "Default.aspx?p=" + (begPage + 1);
  lbMessage.Text = "当前为第" + begPage + "页";
  if (!IsPostBack)
{
DataBind();
}  
   
  }

  void Binders(int index)
  {
  PagedDataSource pds = new PagedDataSource();
  pds.DataSource = SqlDataSource1.Select(DataSourceSelectArguments.Empty);
  pds.AllowPaging = true;
  pds.PageSize = 6;
  pds.CurrentPageIndex = index;
  DataList1.DataSource = pds;
  }
  protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
  {
  DataList1.DataBind();
  }
  protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
  {
  switch (e.CommandName.ToLower())
  {
  case "unselect":
  {
  DataList1.SelectedIndex = -1;
  DataList1.DataBind();
  break;
  }
  default:
  {
  break;
  }
  }
  }
  protected void DataList1_EditCommand(object source, DataListCommandEventArgs e)
  {
  DataList1.EditItemIndex = e.Item.ItemIndex;
  DataList1.DataBind();
  }
  protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
  {
  SqlDataSource1.UpdateParameters["Id"].DefaultValue = DataList1.DataKeys[e.Item.ItemIndex].ToString();
  TextBox txt1 = (TextBox)e.Item.FindControl("txtDeptNo");
  if (txt1 != null)
  {
  SqlDataSource1.UpdateParameters["DeptNo"].DefaultValue = txt1.Text;
  }
  TextBox txt2 = (TextBox)e.Item.FindControl("txtAssetCode");
  if (txt2 != null)
  {
  SqlDataSource1.UpdateParameters["AssetCode"].DefaultValue = txt2.Text;
  }
  TextBox txt3 = (TextBox)e.Item.FindControl("txtDepreciationYear");
  if (txt3 != null)
  {
  SqlDataSource1.UpdateParameters["DepreciationYear"].DefaultValue = txt3.Text;
  }
  TextBox txt4 = (TextBox)e.Item.FindControl("txtDepreciationMonth");
  if (txt4 != null)
  {
  SqlDataSource1.UpdateParameters["DepreciationMonth"].DefaultValue = txt4.Text;


  }
  TextBox txt5 = (TextBox)e.Item.FindControl("txtDepreciationValue");
  if (txt5 != null)
  {
  SqlDataSource1.UpdateParameters["DepreciationValue"].DefaultValue = txt5.Text;
  }
  SqlDataSource1.Update();
  Response.Redirect("Default.aspx?p=" + Request.QueryString["p"]);
  }
 点击保存按钮 实现DataList1_UpdateCommand事件 但是如果不在load事件中加上if(!Ispostback)(红字部分) 就无法修改更新新数据 为什么啊 这里面 的databind()到底起什么作用

[解决办法]
if(!Ispostback) 意思是如果不是回传页面就执行databind();

你修改后重新返回该页面属于回传页面,也就是说你还没修改数据呢,就先绑定了,你修改框里的值就清空

了,所以修改不成功!你必须加上不是回传页面,是首次加载的时候帮顶!

热点排行