首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

急求gridview动态生成绑定控件取值有关问题,重分相送!

2012-09-05 
急求gridview动态生成绑定控件取值问题,重分相送!!gridview列是动态生成,然后给了两个事件如下:protectedv

急求gridview动态生成绑定控件取值问题,重分相送!!
gridview列是动态生成,然后给了两个事件如下:
protected   void   gvInfo_RowCreated(object   sender,   GridViewRowEventArgs   e)
                {
                        if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                        {
                                string   c;
                                string[]   ac;
                                for   (int   i   =   3;   i   <   e.Row.Cells.Count;   i++)
                                {
                                        c   =   e.Row.Cells[i].Text;
                                        ac   =   c.Split( ', ');
                                        DropDownList   ddl   =   new   DropDownList();
                                        ddl.ID   =   "ddlRow "   +   e.Row.RowIndex.ToString()   +   i.ToString();
                                        ddl.DataSource   =   ac;
                                        ddl.DataBind();
                                        ddl.EnableViewState   =   true;
                                        e.Row.Cells[i].Controls.Add(ddl);

                                }
                        }
                }

                protected   void   gvInfo_RowDataBound(object   sender,   GridViewRowEventArgs   e)
                {
                        if   (e.Row.RowType   ==   DataControlRowType.DataRow)
                        {
                                string   c;
                                string[]   ac;


                                for   (int   i   =   3;   i   <   e.Row.Cells.Count;   i++)
                                {
                                        c   =   e.Row.Cells[i].Text;
                                        ac   =   c.Split( ', ');
                                        DropDownList   ddl   =   new   DropDownList();
                                        ddl.ID   =   "ddlRow "   +   e.Row.RowIndex.ToString()   +   i.ToString();
                                        ddl.DataSource   =   ac;
                                        ddl.DataBind();
                                        e.Row.Cells[i].Controls.Add(ddl);
                                }

                        }
                }      
现在问题是:点击按钮后,取gridview里的dropdownlist里的值总是取不到,请问哪们大虾遇到过或者解决了这样的问题,请不吝赐教啊,小弟先谢谢啊!!!

[解决办法]
你是怎么取的?
[解决办法]
用findcontrol方法找控件!!!
[解决办法]
有没有报错,还是就是的为空值!!!
[解决办法]
学习哈

[解决办法]
我试过都是可以的.
[解决办法]
以下是我的测试例子,你参考一下:using System;using System.Data;using System.Configuration;using System.Collections;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;namespace Test{ public partial class GridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { this._BindGrid(); } } void _BindGrid() { this.GridView1.DataSource = new string[] { "A ", "b ", "c " }; this.GridView1.DataBind(); } protected void GridView1_DataBound(object sender, EventArgs e) { } protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddl = new DropDownList(); ddl.ID = "ddl " + e.Row.RowIndex.ToString(); ddl.DataSource = new string[] { "A ", "B ", "C " }; ddl.DataBind(); e.Row.Cells[1].Controls.Add(ddl); } } protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e) { if (e.CommandName == "aa ") { int iRowIndex = Convert.ToInt32(e.CommandArgument); DropDownList ddl = (DropDownList)this.GridView1.Rows[iRowIndex].Cells[1].Controls[0]; Response.Write(ddl.SelectedValue); } } protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DropDownList ddl = new DropDownList(); ddl.DataSource = new string[] { "A ", "B ", "C " }; ddl.ID = "ddl " + e.Row.RowIndex.ToString(); ddl.DataBind(); e.Row.Cells[1].Controls.Add(ddl); } } }}


[解决办法]
<asp:GridView ID= "GridView1 " runat= "server " OnDataBound= "GridView1_DataBound " OnRowDataBound= "GridView1_RowDataBound " OnRowCommand= "GridView1_RowCommand " OnRowCreated= "GridView1_RowCreated "> <Columns> <asp:ButtonField HeaderText= "点击 " CommandName= "aa " Text= "click "/> </Columns> </asp:GridView>
[解决办法]
因为你的GridView中的DropDownList是在GridView创建行的过程中才创建并添加到Page上面的,这就导致它无法正常加载ViewState,也就是说DropDownList如果没被更改的话它无法从ViewState读取到上一次的值,而如果被更改的话这个值要到Page生命周期中更晚的阶段才会被触发更改事件,于是DropDownList就是没值的。

这个问题你不可能单纯在代码中看出来或者找到解决方案,你必须调整这些代码在Page生命周期中所处的阶段才能解决问题。最简单的做法是不要手动创建DropDownList,直接在GridView的Template上面放置DropDownList。
[解决办法]
DataRowView row = (DataRowView)e.Row.DataItem;这句页面一运行便报错:Unable to cast object of type 'System.Data.Common.DbDataRecord ' to type 'System.Data.DataRowView '. 微软示例中也是这么写得,事件也没错,到了2005里怎么就这样了,费解!==> 既然不是datarow就用.DbDataRecord就可以了.DataRowView row = (DataRowView)e.Row.DataItem;==> System.Data.Common.DbDataRecord row = (System.Data.Common.DbDataRecord)e.Row.DataItem;
[解决办法]
托拽一个上去不就好了迈?

又是自找麻烦的,非得用”动态“,WebForm 不同于 WinForm ,

不熟悉,基本的 ASP.NET 控件以及页生命周期先看看这里

创建动态数据输入用户界面
http://www.microsoft.com/china/msdn/library/webservices/asp.net/dnasppDynamicUI.mspx?mfr=true

热点排行