asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现

asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现

文章来源网络 属于生成html 分类 电脑编程网整理 20091114

简介:这是asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现的详细页面,介绍了和生成html,有关的知识,加入收藏请按键盘ctrl+D,谢谢大家的观看!要查看更多有关信息,请点击此处

根 据用户需要,一个页面中,有几个不同的button,点击不同的button,可在下方同一区域产生gridview,(当然他们的gridview是不 同的)。之前用MultiView来控制,用了几个view在html中绑定了几个Gridview,然后根据点击的按钮不同,切换不同的view。但总 觉得这种方式不爽~~,html代码太多,控制起来很麻烦~!
  于是就想到用这种动态的方式实现。Html中只有一个div,用来放置生成的控件,其他全部在后台实现。
  有一点需要注意:如果是在html中加入的控件,页面回发前后系统都会对其状态和属性做viewstate处理,所以,回发时,根据页面生命周期:页面重 新初始化并加载控件实例,而后loadViewstate加载控件属性和状态,之后回发前控件状态重新出现。但是对于动态加载的控件,viewstate 记住了他们的属性和状态,但是并没有存储控件本身。所以回发后,控件也就不见了~
  鉴于此,有多种处理办法,但是归根到底,都需要在页面回发时的初始化状态下对动态控件重新加载。我这里所做的是在page_load事件中,对其重新加载的,当然也可以在更早的事件中做。

和 "asp.net根据条件动态生成GridView,并动态绑定列,且可对其进行编辑的实现" 有关的编程小帖士:

strong>Locale.PositiveCurrencyMode类

提供用于指定用不同的方法代表正货币的字段。

Enum
  |
  +--Locale.PositiveCurrencyMode
package com.ms.wfc.app

public static final class Locale.PositiveCurrencyMode

extends Enum

说明

使用PositiveCurrencyMode类去判断Locale.getPositiveCurrencyMode方法的返回值并作为Locale.SetPositiveCurrencyMode方法的参数。

字段

 说明: 因为是几个不同的加载,所以这里做个一个sign标记(用的static string, 也可以用属性),从而在重新加载的时候判断到底加载哪个gridview.
  废话不多说,看代码:
HTML:


<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>无标题页</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
     <div id="list">
         <asp:Button ID="btngv" runat="server" Text="生成GridView 1" onclick="btngv_Click" />  

            <asp:Button ID="Button1" runat="server" Text="生成GridView 2" onclick="Button1_Click" />

        </div>
     <div id="divshow" runat="server" >
     <%...--在这里放置动态生成的gridview--%>
     </div>
    </div>
    </form>
</body>
</html>
  CS:


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;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
...{
    GridView gvshow=null;
    public static string sign; 

    protected void Page_Load(object sender, EventArgs e)
    ...{
        if (sign != null) //根据标记符号来判断,重新绑定那个控件
        ...{
            if (sign == "gv1")
                setBind();
            else
                setBind2();
        }

       }

       public void setBind()
    ...{
        DataTable dt = getDataTable();  //获得数据源

         gvshow = new GridView();
        gvshow.Width = Unit.Pixel(700);
      
        gvshow.AutoGenerateColumns = false;
        gvshow.RowEditing += new GridViewEditEventHandler(gvshow_RowEditing); //添加编辑事件
        gvshow.RowDeleting +=new GridViewDeleteEventHandler(gvshow_RowDeleting);
        gvshow.DataSource = dt;

        for (int i = 0; i < dt.Columns.Count; i++)
        ...{
            BoundField bc = new BoundField();
            bc.DataField = dt.Columns[i].ColumnName.ToString();
            bc.HeaderText = dt.Columns[i].Caption.ToString();
            gvshow.Columns.Add(bc);
        }

        CommandField cf = new CommandField();
        cf.ButtonType = ButtonType.Link;
        cf.ShowEditButton = true;
        cf.CausesValidation = false;
        gvshow.Columns.Add(cf);

        CommandField cf2 = new CommandField();
        cf2.ButtonType = ButtonType.Link;
        cf2.ShowDeleteButton = true;
        cf2.CausesValidation = false;    

        gvshow.Columns.Add(cf2);

        gvshow.DataBind();
      
        divshow.Controls.Add(gvshow);
    }
    public void setBind2()
    ...{
        DataTable dt = getDataTable();  //获得数据源

        gvshow = new GridView();
        gvshow.Width = Unit.Pixel(700);

        gvshow.AutoGenerateColumns = false;
        gvshow.RowDeleting +=new GridViewDeleteEventHandler(gvshow_RowDeleting); //添加删除事件
        gvshow.DataSource = dt;

        for (int i = 0; i < dt.Columns.Count; i++)
        ...{
            BoundField bc = new BoundField();
            bc.DataField = dt.Columns[i].ColumnName.ToString();
            bc.HeaderText = dt.Columns[i].Caption.ToString();
            gvshow.Columns.Add(bc);
        }

        CommandField cf = new CommandField();
        cf.ButtonType = ButtonType.Link;
       cf.ShowDeleteButton = true;
        cf.CausesValidation = false;

        gvshow.Columns.Add(cf);

        gvshow.DataBind();

        divshow.Controls.Add(gvshow);
    }

    void gvshow_RowDeleting(object sender, GridViewDeleteEventArgs e)
    ...{
        int i = e.RowIndex;
        Response.Write("delete:" + gvshow.Rows[i].Cells[0].Text.ToString());
    }
    protected void btngv_Click(object sender, EventArgs e)  //条件选择后,点击生成按钮,生成特定的gridview
    ...{
        divshow.Controls.Clear();
        setBind();
        sign = "gv1";
    }

   public void gvshow_RowEditing(object sender, GridViewEditEventArgs e)
    ...{
        int i = e.NewEditIndex;
        string id = gvshow.Rows[i].Cells[0].Text.ToString();

        Response.Write("you want to edit"+id);
    }

    public DataTable getDataTable()
    ...{
        DAL.ISDApp01 cDal = new DAL.ISDApp01();
        string sql = "select * from test";
        DataTable dt = cDal.ExecuteQuery(sql).Tables[0];
        return dt;
    }
    protected void Button1_Click(object sender, EventArgs e)
    ...{
        divshow.Controls.Clear();
        setBind2();
        sign = "gv2";
    }
}

你可能感兴趣的:(html,object,server,String,asp.net,button)