ASP.NET 2.0 中动态添加 GridView 模板列 以及取值的例子

<%...@ Page Language="C#" %>
<%...@ Import Namespace="System.Data" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">...
  ICollection CreateDataSource()
  ...{
    DataTable dt 
= new
 DataTable();
    DataRow dr;
    dt.Columns.Add(
new DataColumn("id"typeof
(Int32)));
    dt.Columns.Add(
new DataColumn("text"typeof(string
)));
    
for (int i = 0; i < 6; i++
)
    ...{
      dr 
=
 dt.NewRow();
      dr[
0=
 i;
      dr[
1= "列表项目 " +
 i.ToString();
      dt.Rows.Add(dr);
    }
    DataView dv 
= new
 DataView(dt);
    
return
 dv;
  }

   public class GridViewTemplate : ITemplate
    {
        private DataControlRowType templateType;
        private string columnName;

        public GridViewTemplate(DataControlRowType type, string colname)
        {
            templateType = type;
            columnName = colname;
        }

        public void InstantiateIn(System.Web.UI.Control container)
        {
            switch (templateType)
            {
                case DataControlRowType.Header:
                    Literal lc = new Literal();
                    lc.Text = columnName;
                    container.Controls.Add(lc);
                    break;
                case DataControlRowType.DataRow:
                    TextBox tb = new TextBox();
                    tb.ID = "ItemNum";
                    tb.Width = 50;
                    tb.DataBinding += new   EventHandler(this.OnDataBinding);     
                    container.Controls.Add(tb);
                    break;
                default:
                    break;
            }
        }
        public void OnDataBinding(object sender, EventArgs e)
        {
            TextBox l = (TextBox)sender;//TextBox发送绑定请求     
            GridViewRow container = (GridViewRow)l.NamingContainer;
            l.Text = ((DataRowView)container.DataItem)["ItemNum"].ToString();//绑定ItemNum字段     
        }    
    }

  
  
protected void Page_Load(object
 sender, EventArgs e)
  ...{
    
if (!
IsPostBack)
    ...{
      TemplateField customField 
= new
 TemplateField();
      customField.ShowHeader 
= true
;
      customField.HeaderTemplate 
= new GridViewTemplate(DataControlRowType.Header, "动态添加列"
);
      customField.ItemTemplate 
= new GridViewTemplate(DataControlRowType.DataRow, ""
);
      GridView1.Columns.Add(customField);
      GridView1.DataSource 
=
 CreateDataSource();
      GridView1.DataBind();
    }
  }

  
protected void GridView1_RowDataBound( object
 sender, GridViewRowEventArgs e )
  ...{
    
if (e.Row.RowType ==
 DataControlRowType.DataRow)
    ...{
      
//
可以在这里访问数据库的其它字段的值,可以设置默认选择项,具体应用,看自己的发挥了。
      
//下面只是例子,举一反三,不再费话了

      DataRowView gv = (DataRowView)e.Row.DataItem;
      
int itemSeleted = Int32.Parse(gv.Row["id"].ToString()) > 3 ? 0 : Int32.Parse(gv.Row["id"
].ToString());
      DropDownList dr 
= (DropDownList)e.Row.FindControl("dropdown"
);
      dr.SelectedIndex 
=
 itemSeleted;
    }
  }
</script>


<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
  
<title>GridView动态添加模板列的例子</title>
</head>
<body>
<form id="form1" runat="server">
  
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
     OnRowDataBound
="GridView1_RowDataBound">
    
<Columns>
      
<asp:BoundField HeaderText="标题"  DataField="text"/>
    
</Columns>
  
</asp:GridView> 
</form>

</body>
</html>

 

动态添加的TextBox,PostBack之后就没有了,要取得用户在里面输入的值,要用Request.Form[###]

其中的###需要填入的是控件在客户端的name属性

幸好,虽然嵌套在模版列里的TextBox的name是自动生成的,但终归有规律可循:

假设GridView控件的ID是GridView1,动态添加的TextBox控件的ID是MyText,那么:
protected void Button1_Click(object sender, EventArgs e)
{
    
for (int i = 0; i < GridView1.Rows.Count; i++)
    {
        
string txtName = "GridView1$ctl"+(i+2).ToString().PadLeft(2,'0')+"$MyText";
        
if(Request.Form[txtName]!=null)
         {
             Response.Write(Request.Form[txtName] 
+ "<br/>");
         }
    }
}

posted on 2010-08-25 13:41  J-Pei  阅读(811)  评论(0编辑  收藏  举报

导航