gridview动态生成列,动态添加控件问题

soundblas 2012-04-15 03:44:56
在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>
...全文
877 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
yyl8781697 2012-04-15
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
尽量少用动态生成的服务器控件,动态创建的不保存状态的,也就是你必须自己使用代码维持这些状态。你可以在模板添加想要的控件,不想显示就设置为Visiable=false,操作起来比较简单
[/Quote]
+++
我也觉得,你想实现那些复杂的动态生成什么什么的话 还是建议直接用html 控件加js+ajax来动态生成要方便的多
孟子E章 2012-04-15
  • 打赏
  • 举报
回复
尽量少用动态生成的服务器控件,动态创建的不保存状态的,也就是你必须自己使用代码维持这些状态。你可以在模板添加想要的控件,不想显示就设置为Visiable=false,操作起来比较简单

62,267

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧