gridview动态生成列,动态添加控件问题
在GRIDVEIW的行绑定ROWDATABOUND里生成了N个列,并且往行里的某些单元格里加了DROPDOWNLIST,显示都没问题,但是我一点页面的按钮,本想通过循环GRIDVIEW,找到每一行里的DROPDWNLIST,可是结果是不能找到。而且动态生成的列也没有了。
我又把代码放在ROWCREADER事件里创建列,及为列的单元格里增加DROPDOWNLIST,在执行调试时发现原先在页面上的列里放的HIDDEN控件也找不到,后来通过e.Row.DataItem可以取到我我存在HIDDEN的值。但是,点页面确定按钮后,e.Row.DataItem,又变成空了。
简单说就是我想动态生成列并动态的加入控件,点提交时后台能取到我动态加的控件对象
代码如下
namespace MPMS.Web.Page.EstimateManage
{
public partial class ProviderEstimate : System.Web.UI.Page
{
IList<Domain.User.Department> DeptList
{
get {
IList<Domain.User.Department> list= IocContainer.Get<ISrv.User.IDepartmentSrv>().SelectList();
IList<Domain.User.Department> templist = new List<Domain.User.Department>();
for (int i = 0; i < list.Count; i++)
{
if ((list[i].Company.IndexOf("分公司") != -1 && list[i].DepartName == "采购中心")||list[i].Company=="省本部")
{
templist.Add(list[i]);
}
}
return templist;
}
}
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack)
{
BindData();
}
}
void BindData()
{
IList<Domain.EstimateManage.EstimateIndex> lsit= IocContainer.Get<ISrv.EstimateManage.IEstimateIndexSrv>().SelectList(null, 5, null, null, null, null, null);
this.gv.DataSource = lsit;
this.gv.DataBind();
GroupRows(this.gv, 0);
}
void GroupRows(GridView gv, int cellNum)
{
int i = 0, rowSpanNum = 1;
while (i < gv.Rows.Count - 1)
{
GridViewRow gvr = gv.Rows[i];
for (++i; i < gv.Rows.Count; i++)
{
GridViewRow gvrNext = gv.Rows[i];
if (gvr.Cells[cellNum].Text == gvrNext.Cells[cellNum].Text)
{
gvrNext.Cells[cellNum].Visible = false;
rowSpanNum++;
}
else
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
rowSpanNum = 1;
break;
}
if (i == gv.Rows.Count - 1)
{
gvr.Cells[cellNum].RowSpan = rowSpanNum;
}
}
}
}
protected void gvq_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void gv_RowDataBound(object sender, GridViewRowEventArgs e)
{
}
protected void a_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in gv.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
HiddenField hd = (HiddenField)row.FindControl("hddept");
Response.Write(hd.Value);
string dpsoc = "dp_0_" + (2).ToString();
DropDownList dp = (DropDownList)gv.FindControl(dpsoc);
DropDownList dp3 = (DropDownList)row.FindControl(dpsoc);
if (dp != null)
{
Response.Write("ok1");
}
if (dp3 != null)
{
Response.Write("ok3");
}
string dpsoc1 = "cph_gv_dp_0_2_0";
DropDownList dp1 = (DropDownList)gv.FindControl(dpsoc1);
DropDownList dp4 = (DropDownList)row.FindControl(dpsoc1);
if (dp1 != null)
{
Response.Write("ok2");
}
if (dp4 != null)
{
Response.Write("ok4");
}
}
}
}
protected void gv_RowCreated(object sender, GridViewRowEventArgs e)
{
#region
if (e.Row.RowType == DataControlRowType.Header)
{
IList<Domain.User.Department> deptlist = (IList<Domain.User.Department>)DeptList;
for (int i = 0; i < deptlist.Count; i++)
{
TableCell cell = new TableCell();
if (deptlist[i].Company == "省本部")
{
cell.Text = deptlist[i].DepartName;
}
else { cell.Text = deptlist[i].Company.TrimEnd("分公司".ToCharArray()); }
cell.HorizontalAlign = HorizontalAlign.Center;
cell.Width = Unit.Pixel(80);
cell.Wrap = false;
cell.EnableViewState = true;
e.Row.Cells.Add(cell);
//http://localhost:12345/Page/EstimateManage/ProviderEstimate.aspx
}
}
#endregion
if (e.Row.RowType == DataControlRowType.DataRow)
{
MPMS.Domain.EstimateManage.EstimateIndex modes = (MPMS.Domain.EstimateManage.EstimateIndex)e.Row.DataItem;
HiddenField hdindextype = (HiddenField)e.Row.FindControl("hdindextype");
//HiddenField hddept = (HiddenField)e.Row.FindControl("hddept");
int type = modes.Indextype.Value;
string[] deptids = modes.selDeptment.Split(',');
IList<Domain.User.Department> deptlist = (IList<Domain.User.Department>)DeptList;
for (int i = 0; i < deptlist.Count; i++)
{
TableCell tbsoccell = new TableCell();
for (int k = 0; k < deptids.Length; k++)
{
if (deptlist[i].Id.ToString() == deptids[k])
{
if (type == (int)MPMS.Domain.EstimateManage.SocType.Num)
{
TextBox txsoc = new TextBox();
txsoc.EnableViewState = true;
txsoc.ID = "tx" + e.Row.RowIndex.ToString() + "_" + (k + 2).ToString();
txsoc.Width = 40;
tbsoccell.Controls.Add(txsoc);
}
else
{
DropDownList dpsoc = new DropDownList();
dpsoc.EnableViewState = true;
dpsoc.ID = "dp_" + e.Row.RowIndex.ToString() + "_" + (k + 2).ToString();
Mr.Cool.Utility.EnumUtility.BindDropDownList(dpsoc, typeof(Domain.EstimateManage.TaskTypeSoc));
TextBox tbreson = new TextBox();
tbreson.ID = "tbreson_" + e.Row.RowIndex + "_" + (k + 2).ToString();
tbreson.Attributes.Add("style", "display:none");
tbreson.Width = 40;
tbreson.EnableViewState = true;
tbsoccell.Controls.Add(dpsoc);
string dpidstring = "cph_gv_" + dpsoc.ClientID + "_" + e.Row.RowIndex.ToString();
string tbidstring = "cph_gv_" + tbreson.ClientID + "_" + e.Row.RowIndex.ToString();
dpsoc.Attributes.Add("onchange", "Writereson('" + tbidstring + "','" + dpidstring + "')");
tbsoccell.Controls.Add(tbreson);
}
}
}
tbsoccell.Width = 50;
tbsoccell.EnableViewState = true;
e.Row.Cells.Add(tbsoccell);
}
}
}
}
}
页面如下
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False"
onrowdatabound="gv_RowDataBound" CssClass="listTable" Width="100%"
onrowcreated="gv_RowCreated">
<Columns>
<asp:BoundField DataField="pid" HeaderStyle-HorizontalAlign=Center HeaderStyle-Wrap=false/>
<asp:TemplateField>
<ItemTemplate >
<%# Eval("Indexname")%>
<asp:HiddenField ID="hddept" runat="server" Value='<%# Eval("selDeptment") %>'/>
<asp:HiddenField ID="hdindextype" runat=server Value='<%# Eval("indextype") %>' />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:Button ID=a runat=server Text=a onclick="a_Click" />
</asp:Content>