实际上,点击下拉列表中的值后,当它进行回发时,它总是显示相同的项目(第一个)。
我一直在阅读有关可能的解决方案,我还检查了我是否在Page Load中进行数据绑定,然后将绑定代码包装为:
if(!Page.IsPostBack)
{
// Your binding code here ...
}
但实际上我包装了每个数据绑定(我只是按照上面评论过的URL)。因此,我有点疯狂。
这是我的标记:
<asp:GridView ID="gridviewTitulaciones" runat="server" ShowFooter="true"
AutoGenerateColumns="false" OnRowCreated="Gridview1_RowCreated"
>
<Columns>
<asp:BoundField DataField="datafieldTitulacion" HeaderText="Nº Titulación" />
<asp:TemplateField HeaderText="Titulación">
<ItemTemplate>
<asp:DropDownList ID="dropdownTitulacion" runat="server" Width="250px" AppendDataBoundItems="true"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Universidad">
<ItemTemplate>
<asp:DropDownList ID="dropdownUniversidad" runat="server" AppendDataBoundItems="true" Width="250px"></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Fecha fin de estudios">
<ItemTemplate>
<asp:TextBox ID="txtStartDateFechaFinEstudios" runat="server" CssClass="CampoNormal" Width="250px"
ReadOnly="true"></asp:TextBox>
</ItemTemplate>
<FooterStyle HorizontalAlign="Right" />
<FooterTemplate>
<asp:Button ID="ButtonAdd_newRowTitulacionUniversidad" runat="server"
Text="Add New Row"
onclick="ButtonAdd_Click" />
<!-- <asp:Button ID="ButtonAdd" runat="server"
Text="Add New Row"
/> -->
</FooterTemplate>
</asp:TemplateField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
onclick="LinkButton1_Click">Remove</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
这是我的.aspx:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
SetInitialRow();
}
}
protected void ButtonAdd_Click(object sender, EventArgs e)
{
AddNewRowToGrid();
}
private void FillDropDownList_titulacion(DropDownList ddl)
{
ddl.DataSource = BC.Titulacion.Listar();
ddl.DataTextField = "DESC_TITULACION";
ddl.DataValueField = "TITULACION_ID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("", "-1"));
ddl.Items[0].Selected = true;
}
private void FillDropDownList_universidad(DropDownList ddl)
{
ddl.DataSource = BC.CentroFormativo.Listar();
ddl.DataTextField = "DESC_CENTRO";
ddl.DataValueField = "CENTRO_TIPO_ID";
ddl.DataBind();
ddl.Items.Insert(0, new ListItem("", "-1"));
ddl.Items[0].Selected = true;
}
private void SetInitialRow()
{
DataTable dt = new DataTable();
DataRow dr = null;
dt.Columns.Add(new DataColumn("datafieldTitulacion", typeof(string)));
//dt.Columns.Add(new DataColumn("Column1", typeof(string)));//for TextBox value
//dt.Columns.Add(new DataColumn("Column2", typeof(string)));//for TextBox value
dt.Columns.Add(new DataColumn("ColumnTitulaciones", typeof(string)));//for DropDownList selected item
dt.Columns.Add(new DataColumn("ColumnUniversidades", typeof(string)));//for DropDownList selected item
dr = dt.NewRow();
dr["datafieldTitulacion"] = 1;
dr["ColumnTitulaciones"] = string.Empty;
dr["ColumnUniversidades"] = string.Empty;
dt.Rows.Add(dr);
//Store the DataTable in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Bind the Gridview
gridviewTitulaciones.DataSource = dt;
gridviewTitulaciones.DataBind();
//After binding the gridview, we can then extract and fill the DropDownList with Data
DropDownList ddl1 = (DropDownList)gridviewTitulaciones.Rows[0].Cells[1].FindControl("dropdownTitulacion");
DropDownList ddl2 = (DropDownList)gridviewTitulaciones.Rows[0].Cells[2].FindControl("dropdownUniversidad");
FillDropDownList_titulacion(ddl1);
FillDropDownList_universidad(ddl2);
}
private void AddNewRowToGrid()
{
if (ViewState["CurrentTable"] != null)
{
DataTable dtCurrentTable = (DataTable)ViewState["CurrentTable"];
DataRow drCurrentRow = null;
if (dtCurrentTable.Rows.Count > 0)
{
drCurrentRow = dtCurrentTable.NewRow();
drCurrentRow["datafieldTitulacion"] = dtCurrentTable.Rows.Count + 1;
//add new row to DataTable
dtCurrentTable.Rows.Add(drCurrentRow);
//Store the current data to ViewState for future reference
ViewState["CurrentTable"] = dtCurrentTable;
//String p = dtCurrentTable.Rows[0].ToString();
for (int i = 0; i < dtCurrentTable.Rows.Count - 1; i++)
{
//extract the DropDownList Selected Items
DropDownList ddl1 = (DropDownList)gridviewTitulaciones.Rows[i].Cells[1].FindControl("dropdownTitulacion");
DropDownList ddl2 = (DropDownList)gridviewTitulaciones.Rows[i].Cells[2].FindControl("dropdownUniversidad");
//String s = ddl1.Items[5].Text;
// Update the DataRow with the DDL Selected Items
// int igh = ddl1.SelectedIndex;
dtCurrentTable.Rows[i]["ColumnTitulaciones"] = ddl1.SelectedItem.Text;
dtCurrentTable.Rows[i]["ColumnUniversidades"] = ddl2.SelectedItem.Text;
}
//Rebind the Grid with the current data to reflect changes
gridviewTitulaciones.DataSource = dtCurrentTable;
gridviewTitulaciones.DataBind();
}
}
else
{
Response.Write("ViewState is null");
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void SetPreviousData()
{
int rowIndex = 0;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
DropDownList ddl1 = (DropDownList)gridviewTitulaciones.Rows[rowIndex].Cells[1].FindControl("dropdownTitulacion");
DropDownList ddl2 = (DropDownList)gridviewTitulaciones.Rows[rowIndex].Cells[2].FindControl("dropdownUniversidad");
//Fill the DropDownList with Data
FillDropDownList_titulacion(ddl1);
FillDropDownList_universidad(ddl2);
if (i < dt.Rows.Count - 1)
{
//Set the Previous Selected Items on Each DropDownList on Postbacks
ddl1.ClearSelection();
ddl1.Items.FindByText(dt.Rows[i]["ColumnTitulaciones"].ToString()).Selected = true;
ddl2.ClearSelection();
ddl2.Items.FindByText(dt.Rows[i]["ColumnUniversidades"].ToString()).Selected = true;
}
rowIndex++;
}
}
}
}
protected void Gridview1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
LinkButton lb = (LinkButton)e.Row.FindControl("LinkButton1");
if (lb != null)
{
if (dt.Rows.Count > 1)
{
if (e.Row.RowIndex == dt.Rows.Count - 1)
{
lb.Visible = false;
}
}
else
{
lb.Visible = false;
}
}
}
}
protected void LinkButton1_Click(object sender, EventArgs e)
{
LinkButton lb = (LinkButton)sender;
GridViewRow gvRow = (GridViewRow)lb.NamingContainer;
int rowID = gvRow.RowIndex;
if (ViewState["CurrentTable"] != null)
{
DataTable dt = (DataTable)ViewState["CurrentTable"];
if (dt.Rows.Count > 1)
{
if (gvRow.RowIndex < dt.Rows.Count - 1)
{
//Remove the Selected Row data and reset row number
dt.Rows.Remove(dt.Rows[rowID]);
ResetRowID(dt);
}
}
//Store the current data in ViewState for future reference
ViewState["CurrentTable"] = dt;
//Re bind the GridView for the updated data
gridviewTitulaciones.DataSource = dt;
gridviewTitulaciones.DataBind();
}
//Set Previous Data on Postbacks
SetPreviousData();
}
private void ResetRowID(DataTable dt)
{
int rowNumber = 1;
if (dt.Rows.Count > 0)
{
foreach (DataRow row in dt.Rows)
{
row[0] = rowNumber;
rowNumber++;
}
}
}
答案 0 :(得分:0)
好吧,我想出了办法。关键是下拉列表有几个相等的值(项),所以我只重建我的项目列表,使它们成为独特的。然后,它有效。