我正在向dropDownlist动态添加值,这是Gridview中的一列。
问题是当添加新行时,未保存或维护下拉列表中选择的值?知道我可能做错了什么。这是我的代码..
这是我的Gridview
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333" GridLines="None" OnDataBound="GridView1_DataBound">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:boundfield datafield="ID" headertext="categoryid" />
<asp:boundfield datafield="univirsity" headertext="category name" />
<asp:templatefield headertext="products">
<itemtemplate>
<asp:dropdownlist id="dropdownlist1" runat="server">
</asp:dropdownlist>
</itemtemplate>
</asp:templatefield>
</Columns>
</asp:GridView>
这是在单击按钮时动态地将数据绑定到Gridview的代码。
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", System.Type.GetType("System.Int32"));
dt.Columns.Add("univirsity", System.Type.GetType("System.Int32"));
dt.Columns.Add("major", System.Type.GetType("System.Int32"));
Session["MyDataTable"] = dt;
}
}
protected void Button1_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["ID"] = t.Rows.Count + 1;
row1["univirsity"] = 3;
// row1["major"] = 31;
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView1.DataSource = t;
GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
//addControl();
ArrayList list = new ArrayList();
list.Add("DMG");
list.Add("SVC");
foreach (GridViewRow row in GridView1.Rows)
{
// int key = (int)GridView1.DataKeys[row.RowIndex].Value;
if (row.RowType == DataControlRowType.DataRow)
{
DropDownList dl = (DropDownList)row.FindControl("dropdownlist1");
string sel = dl.SelectedValue;
if (!String.IsNullOrEmpty(sel))
{
dl.DataSource = list;
dl.DataBind();
dl.SelectedValue = sel;
}
else
{
dl.DataSource = list;
dl.DataBind();
}
}
}
}
答案 0 :(得分:2)
你不是做任何“错误”,你只是期待错误的事情。网格视图等模板化控件以这种方式运行;如果您希望模板中控件的选定值保持不变,则必须自己完成。
我建议不要在Session中存储大量数据,但我会假设您知道这一点,并且您的方案无意扩展到数据库中有多行的数百万用户..每个。
在您的情况下,我会在该数据表中添加另一列来存储'product'值;将它与每个回发同步,并在重新绑定时确保从表值中设置下拉列表值: -
protected override void OnLoad(EventArgs e)
{
if (!Page.IsPostBack)
{
DataTable dt = new DataTable();
dt.Columns.Add("ID", typeof(int));
dt.Columns.Add("univirsity", typeof(int));
dt.Columns.Add("major", typeof(int));
// our new column
dt.Columns.Add("selectedProduct", typeof(string));
Session["MyDataTable"] = dt;
}
else
{
// on each postback, loop through the grid and update
// the datatable
// get our table AsEnumerable, so we can use LINQ expressions on it
var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();
foreach (GridViewRow gridRow in GridView1.Rows)
{
// get this row's ID from the grid
int id = Convert.ToInt32(gridRow.Cells[0].Text);
// and get the matching datarow from the table
var tableRow = table.Where(x => (int)x["ID"] == id)
.SingleOrDefault();
if (tableRow != null)
{
var ddl = (DropDownList)gridRow.FindControl("dropdownlist1");
tableRow["selectedProduct"] = ddl.SelectedValue;
}
}
}
base.OnLoad(e);
}
void button1_Click(object sender, EventArgs e)
{
DataTable t = (DataTable)Session["MyDataTable"];
DataRow row1 = t.NewRow();
row1["ID"] = t.Rows.Count + 1;
row1["univirsity"] = 3;
// set default value for new column
row1["selectedProduct"] = "DMG";
t.Rows.Add(row1);
Session["MyDataTable"] = t;
GridView1.DataSource = t;
GridView1.DataBind();
}
protected void GridView1_DataBound(object sender, EventArgs e)
{
ArrayList list = new ArrayList();
list.Add("DMG");
list.Add("SVC");
// get our table again
var table = ((DataTable)Session["MyDataTable"]).AsEnumerable();
foreach (GridViewRow gridRow in GridView1.Rows)
{
if (gridRow.RowType == DataControlRowType.DataRow)
{
// get our id and tablerow again
int id = Convert.ToInt32(gridRow.Cells[0].Text);
var tableRow = table.Where(x => (int)x["ID"] == id)
.SingleOrDefault();
// bind the list
DropDownList dl = (DropDownList)gridRow.FindControl("dropdownlist1");
dl.DataSource = list;
dl.DataBind();
// set value from tablerow.selectedProduct
dl.SelectedValue = (string)tableRow["selectedProduct"];
}
}
}
答案 1 :(得分:0)
当您在gridview中有新行时,您是否尝试使用循环重新加载此下拉列表?