datagrid 模板列的问题
<asp:DataGrid ID= "dgMain " runat= "server " AutoGenerateColumns= "False " Font-Size= "13px ">
<SelectedItemStyle Font-Names= "宋体 " Font-Size= "9pt " />
<EditItemStyle BackColor= "Info " Font-Bold= "True " Font-Names= "宋体 " Font-Size= "9pt "/>
<AlternatingItemStyle BackColor= "White " Font-Names= "宋体 " Font-Size= "9pt " ForeColor= "ControlText " />
<ItemStyle BackColor= "WhiteSmoke " Font-Names= "宋体 " Font-Size= "9pt " ForeColor= "ControlText " />
<HeaderStyle BackColor= "#99CCCC " Font-Bold= "True " ForeColor= "Black " HorizontalAlign= "Center "
VerticalAlign= "Middle " Font-Italic= "False " Font-Overline= "False " Font-Strikeout= "False " Font-Underline= "False " />
<Columns>
<asp:TemplateColumn HeaderText= "字段 ">
<HeaderStyle Width= "150px " />
<ItemTemplate>
<asp:DropDownList ID= "TFld " runat= "server " Width= "150px ">
'****** 动态增加 ListItem 并在回传后保持里面的内容 *******
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
[解决办法]
要在模板列里面动态增加列,并在回传即 ispostback 后,仍保持里面 dropdownlist 中选择的内容
====================================================
动态的列写到ViewState中,提交到服务器后再从ViewState里读出来写到DropDownList里
[解决办法]
要在模板列里面动态增加列,并在回传即 ispostback 后,仍保持里面 dropdownlist 中选择的内容
----------------
强烈不支持如此动态添加删除列,很难以维护状态,请考虑使用 visible 属性,简单又方便
[解决办法]
请考虑使用 visible 属性,简单又方便
==================================
呵呵,怎么听着像在做广告
[解决办法]
重写
override void LoadViewState(object savedState)
override object SaveViewState()
[解决办法]
............
[解决办法]
你这个动态模版列的内容是全是手工填加的,还是有部分从数据库来,有部分自己填加 ?
[解决办法]
而且你这个DropDownList里的内容动态填加以后,是每个Item都一样,还是都有各自的内容,后者显然不大好做
[解决办法]
给你个测试代码,你看看需要什么功能再交流
//
<%@ Page language= "c# " Codebehind= "DataGridDemo.aspx.cs " AutoEventWireup= "false " Inherits= "DataGridDemo " %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN " >
<HTML>
<HEAD>
<title> DataGrid </title>
<meta name= "GENERATOR " Content= "Microsoft Visual Studio .NET 7.1 ">
<meta name= "CODE_LANGUAGE " Content= "C# ">
<meta name= "vs_defaultClientScript " content= "JavaScript ">
<meta name= "vs_targetSchema " content= "http://schemas.microsoft.com/intellisense/ie5 ">
</HEAD>
<body>
<form id= "Form1 " method= "post " runat= "server ">
<asp:DataGrid id= "DataGrid1 " runat= "server " AutoGenerateColumns= "False ">
<Columns>
<asp:TemplateColumn>
<ItemTemplate>
<asp:CheckBox id= "CheckBox1 " runat= "server "> </asp:CheckBox>
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField= "au_id " ReadOnly= "True " HeaderText= "au_id "> </asp:BoundColumn>
<asp:TemplateColumn HeaderText= "state ">
<ItemTemplate>
<asp:DropDownList id= "DropDownList1 " runat= "server "> </asp:DropDownList>
</ItemTemplate>
</asp:TemplateColumn>
</Columns>
</asp:DataGrid>
<asp:TextBox id= "TextBox1 " runat= "server "> </asp:TextBox>
<asp:Button id= "Button1 " runat= "server " Text= "添加 "> </asp:Button>
</form>
</body>
</HTML>
//aspx.cs
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public class DataGridDemo : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private DataTable BindDropDownList()
{
SqlConnection cn = new SqlConnection( "server=.;uid=sa;pwd=;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select distinct state from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
return ds.Tables[0];
}
private void BindGrid()
{
SqlConnection cn = new SqlConnection( "server=.;uid=sa;pwd=;database=pubs ");
SqlDataAdapter da = new SqlDataAdapter( "select au_id, state, contract from authors ", cn);
DataSet ds = new DataSet();
cn.Open();
da.Fill(ds);
cn.Close();
DataGrid1.DataSource = ds.Tables[0].DefaultView;
DataGrid1.DataKeyField = "au_id ";
DataGrid1.DataBind();
}
private void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
this.DataGrid1.ItemDataBound +=new DataGridItemEventHandler(DataGrid1_ItemDataBound);
}
#endregion
private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
DropDownList dropTemp;
string state;
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
state = ((DataRowView)e.Item.DataItem).Row[ "state "].ToString();
dropTemp = (DropDownList)e.Item.FindControl( "DropDownList1 ");
if(dropTemp != null)
{
dropTemp.DataSource = BindDropDownList();
dropTemp.DataTextField = "state ";
dropTemp.DataBind();
ListItem li = dropTemp.Items.FindByText(state);
if(li != null)
{
li.Selected = true;
}
}
}
}
private void Button1_Click(object sender, System.EventArgs e)
{
if(TextBox1.Text.Trim() == string.Empty) return;
foreach(DataGridItem dgi in DataGrid1.Items)
{
CheckBox chkTemp = (CheckBox)dgi.FindControl( "CheckBox1 ");
if(chkTemp != null)
{
if(chkTemp.Checked)
{
((DropDownList)dgi.FindControl( "DropDownList1 ")).Items.Add(new ListItem(TextBox1.Text, TextBox1.Text));
}
}
}
}
}