none
【ASP.NET】gridview如何才能动态添加模板 RRS feed

  • 问题

  • 当我点击编辑按钮的时候想在gridview指定列动态添加一个模板列。在其中放入控件,请问高手,我应该怎样做呢?

     


    大连海参专卖 年底促销活动中 店铺地址:http://jinweb.taobao.com
    • 已移动 eryang 2010年12月3日 2:55 asp.net 相关 (发件人:.NET Framework 一般性问题讨论区)
    2010年12月2日 6:33

答案

全部回复

  • 你好!

    建议在设计时就把相应的模板列设计好,在点击按钮时控制列的显示与隐藏。


    知识改变命运,奋斗成就人生!
    2010年12月4日 10:08
    版主
  • 你好!

    建议在设计时就把相应的模板列设计好,在点击按钮时控制列的显示与隐藏。


    知识改变命运,奋斗成就人生!

    起初我就是在设计的时候把模板设计好了并隐藏,点击编辑之后再显示出来,可问题就是在点击编辑的时候由于隐藏列显示。造成整个Gridview列窜(多了)了一列,从而使页面风格发生改变。所以我想解决此问题


    大连海参专卖 年底促销活动中 店铺地址:http://jinweb.taobao.com
    2010年12月5日 12:36
  • 你好!

    将整个列一起隐藏掉,而不是列中的内容。


    知识改变命运,奋斗成就人生!
    2010年12月5日 12:47
    版主
  • 你好,看下DEMO

    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm2.aspx.cs" Inherits="WebApplication1.WebForm2" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
      <title>无标题页</title>
    </head>
    <body>
      <form id="form1" runat="server">
      <div>
      
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
          onrowediting="GridView1_RowEditing" DataKeyNames="examineeid" 
          onrowcancelingedit="GridView1_RowCancelingEdit" 
          onrowupdating="GridView1_RowUpdating" 
          onrowdeleting="GridView1_RowDeleting">
        <Columns>
        
         <asp:TemplateField HeaderText="学号">
           <ItemTemplate>
            <%#DataBinder.Eval(Container.DataItem,"pwd") %>
           </ItemTemplate>
           <EditItemTemplate> 
           <asp:TextBox ID="TextBox1" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"pwd")%>>
            
           </asp:TextBox>
           </EditItemTemplate>
           
         </asp:TemplateField>
         
         <asp:TemplateField HeaderText="名称">
           <ItemTemplate>
            <%#DataBinder.Eval(Container.DataItem,"examineename") %>
           </ItemTemplate>
            <EditItemTemplate>
           <asp:TextBox ID="TextBox2" runat="server" Text=<%#DataBinder.Eval(Container.DataItem,"examineename") %>>
           </asp:TextBox>
           </EditItemTemplate>
         </asp:TemplateField>
         
         <asp:TemplateField>
           
          <ItemTemplate>
          <asp:Button ID="Button2" runat="server" Text="编辑" CommandName="Edit" 
               />
               <asp:Button ID="Button4" runat="server" Text="删除" CommandName="Delete" />
          </ItemTemplate>
           <EditItemTemplate>
             <asp:Button ID="Button1" runat="server" Text="更新" CommandName="Update" />
              <asp:Button ID="Button3" runat="server" Text="取消" CommandName="Cancel"/>
              
           </EditItemTemplate>
         </asp:TemplateField>
        </Columns>
        </asp:GridView>
      
        <asp:Button ID="Button2" runat="server" Text="Button" />
      
      </div>
      </form>
    </body>
    </html>
    
    

     

    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.SqlClient;
    
    namespace WebApplication1
    {
      public partial class WebForm2 : System.Web.UI.Page
      {
        string strCon = "Data Source=.;Initial Catalog=userinfo;User ID=sa;Password=sa";
        protected void Page_Load(object sender, EventArgs e)
        {
          if (!IsPostBack)
          {
            BindGridView();
          }
        }
        private void BindGridView()
        {
          SqlConnection con = new SqlConnection(strCon);
    
          con.Open();
          SqlCommand cmd = new SqlCommand("select * from examinee",con);
          SqlDataAdapter adp = new SqlDataAdapter(cmd);
          DataSet ds = new DataSet();
          adp.Fill(ds);
          this.GridView1.DataSource=ds.Tables[0];
          this.GridView1.DataBind();
          con.Close();
        }
        
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
          GridView1.EditIndex = e.NewEditIndex;
          BindGridView();
        }
    
        protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
          GridView1.EditIndex = -1;
          BindGridView();
        }
    
        protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
          string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
          string pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;
    
          string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
    
          string strUpdate = "UPDATE EXaminee set pwd='"+pwd+"',examineename='"+name+"' where examineeid="+id;
    
          SqlConnection con = new SqlConnection(strCon);
          con.Open();
          SqlCommand cmd = new SqlCommand(strUpdate,con);
          cmd.ExecuteNonQuery();
          con.Close();
          GridView1.EditIndex = -1;
          BindGridView();
        }
    
        protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
          string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
          string strDel = "DELETE FROM EXAMINEE where examineeid="+id;
          SqlConnection con = new SqlConnection(strCon);
          con.Open();
          SqlCommand cmd = new SqlCommand(strDel, con);
          cmd.ExecuteNonQuery();
          con.Close();
          BindGridView();
        }
      }
    }
    
    

    http://blog.csdn.net/zx13525079024
    2010年12月5日 13:43
  • 你好,看下DEMO

    <%
    @
     Page
     Language
    =
    "C#"
     AutoEventWireup
    =
    "true"
     CodeBehind
    =
    "WebForm2.aspx.cs"
     Inherits
    =
    "WebApplication1.WebForm2"
     %>
    

    <! DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >

    < html xmlns = "http://www.w3.org/1999/xhtml" >
    < head runat = "server" >
    < title > 无标题页</ title >
    </ head >
    < body >
    < form id = "form1" runat = "server" >
    < div >

    < asp : GridView ID = "GridView1" runat = "server" AutoGenerateColumns = "False"
    onrowediting = "GridView1_RowEditing" DataKeyNames = "examineeid"
    onrowcancelingedit = "GridView1_RowCancelingEdit"
    onrowupdating = "GridView1_RowUpdating"
    onrowdeleting = "GridView1_RowDeleting" >
    < Columns >

    < asp : TemplateField HeaderText = "学号" >
    < ItemTemplate >
    <% #DataBinder.Eval(Container.DataItem,"pwd") %>
    </ ItemTemplate >
    < EditItemTemplate >
    <asp:TextBox ID="TextBox1" runat="server" Text=<% #DataBinder.Eval(Container.DataItem,"pwd")%> >

    </ asp : TextBox >
    </ EditItemTemplate >

    </ asp : TemplateField >

    < asp : TemplateField HeaderText = "名称" >
    < ItemTemplate >
    <% #DataBinder.Eval(Container.DataItem,"examineename") %>
    </ ItemTemplate >
    < EditItemTemplate >
    <asp:TextBox ID="TextBox2" runat="server" Text=<% #DataBinder.Eval(Container.DataItem,"examineename") %> >
    </ asp : TextBox >
    </ EditItemTemplate >
    </ asp : TemplateField >

    < asp : TemplateField >

    < ItemTemplate >
    < asp : Button ID = "Button2" runat = "server" Text = "编辑" CommandName = "Edit"
    />
    < asp : Button ID = "Button4" runat = "server" Text = "删除" CommandName = "Delete" />
    </ ItemTemplate >
    < EditItemTemplate >
    < asp : Button ID = "Button1" runat = "server" Text = "更新" CommandName = "Update" />
    < asp : Button ID = "Button3" runat = "server" Text = "取消" CommandName = "Cancel" />

    </ EditItemTemplate >
    </ asp : TemplateField >
    </ Columns >
    </ asp : GridView >

    < asp : Button ID = "Button2" runat = "server" Text = "Button" />

    </ div >
    </ form >
    </ body >
    </ html >

     

    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.SqlClient;

    namespace WebApplication1
    {
    public partial class WebForm2 : System.Web.UI.Page
    {
    string strCon = "Data Source=.;Initial Catalog=userinfo;User ID=sa;Password=sa";
    protected void Page_Load(object sender, EventArgs e)
    {
    if (!IsPostBack)
    {
    BindGridView();
    }
    }
    private void BindGridView()
    {
    SqlConnection con = new SqlConnection(strCon);

    con.Open();
    SqlCommand cmd = new SqlCommand("select * from examinee",con);
    SqlDataAdapter adp = new SqlDataAdapter(cmd);
    DataSet ds = new DataSet();
    adp.Fill(ds);
    this.GridView1.DataSource=ds.Tables[0];
    this.GridView1.DataBind();
    con.Close();
    }

    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
    GridView1.EditIndex = e.NewEditIndex;
    BindGridView();
    }

    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
    GridView1.EditIndex = -1;
    BindGridView();
    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
    string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
    string pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1")).Text;

    string name = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;

    string strUpdate = "UPDATE EXaminee set pwd='"+pwd+"',examineename='"+name+"' where examineeid="+id;

    SqlConnection con = new SqlConnection(strCon);
    con.Open();
    SqlCommand cmd = new SqlCommand(strUpdate,con);
    cmd.ExecuteNonQuery();
    con.Close();
    GridView1.EditIndex = -1;
    BindGridView();
    }

    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
    string id = GridView1.DataKeys[e.RowIndex].Values[0].ToString();
    string strDel = "DELETE FROM EXAMINEE where examineeid="+id;
    SqlConnection con = new SqlConnection(strCon);
    con.Open();
    SqlCommand cmd = new SqlCommand(strDel, con);
    cmd.ExecuteNonQuery();
    con.Close();
    BindGridView();
    }
    }
    }


    http://blog.csdn.net/zx13525079024
    你好 感谢你的答复。但我发现里面也没有增加模板隐藏模板 甚至是动态添加模板的演示啊

    大连海参专卖 年底促销活动中 店铺地址:http://jinweb.taobao.com
    2010年12月6日 1:26
  • 2010年12月6日 3:20
    版主
  • 2010年12月6日 5:02
    版主