【发布时间】:2014-06-10 10:20:34
【问题描述】:
问题及说明:
我有一个 GridView,它以编程方式将 RowDataBound 上的 DropDownLists 添加到每个单元格。
DropDownLists 都有Data Sources。
当我按下“保存”按钮时,它意味着读取 DropDownLists 中的所有 selecteditems 并将它们保存到数据库中。
但是,当我单击按钮时,它会导致 postback 并删除 GridView 中的所有控件,因此在我的 button_click 事件中找不到它们。
在之前提出问题并尝试不同的技术以尝试将下拉列表存储在缓存/会话状态等中之后,当我单击按钮时,我似乎无法保留下拉列表或数据。
因此,我现在尝试添加一个 UpdatePanel 并使用 AJAX 来尝试停止 GridView 的刷新。
我的尝试是这样的:
<asp:GridView ID="gv_Rota" runat="server" AutoGenerateColumns="false" OnRowDataBound="gv_Rota_RowDataBound">
<HeaderStyle BackColor="#6a3d98" ForeColor="White" Height="20" />
<RowStyle HorizontalAlign="Center" Height="20px" Width="100px" />
<AlternatingRowStyle Height="20px" />
</asp:GridView>
</div>
<asp:Label ID="lbl_NameOfRota" runat="server" Text="New rota name:"></asp:Label>
<input runat="server" id="txt_RotaName" />
<asp:UpdatePanel runat="server" ID="RotaUpdatePanel">
<ContentTemplate>
<asp:Button ID="btn_AddRota" runat="server" Text="Add" OnClick="btn_AddRota_Click" CssClass="ButtonAdminPage" />
</ContentTemplate>
</asp:UpdatePanel>
如您所见,我只在按钮周围放置了 UpdatePanel,但它似乎仍然刷新了 GridView 并删除了所有控件。
问题:
首先,为什么我的尝试没有成功?
其次,我怎样才能使用 AJAX 和更新面板来解决这个问题,或者还有其他方法可以让我得到我需要的东西吗? (请记住,如果有其他建议,我可能已经尝试过了)。
编辑 2:
提供的代码:
这是我绑定 DropDownLists 的时候:
protected void gv_Rota_RowDataBound(object sender, GridViewRowEventArgs e)
{
for (int i = 1; i <= ColumnCount; i++)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
int day = e.Row.RowIndex;
day += 1;
ddlShift = new DropDownList();
ddlShift.ID = "ddlShift" + "WK" + i.ToString() + "DAY" + day.ToString();
ddlShift.DataSource = DCListOfShifts;
ddlShift.DataValueField = "SHIFT_ID";
ddlShift.DataTextField = "SHIFT_NAME";
ddlShift.Attributes.Add("Place", i.ToString());
ddlShift.DataBind();
ddlShift.Items.Insert(0, new ListItem("Shift..."));
ddlShift.CssClass = "ddl_rotamanager";
e.Row.Cells[i].Controls.Add(ddlShift);
}
}
}
这是我根据传递的列数创建 GridView 时:
private void BindGrid(int Amount)
{
gv_Rota.DataSource = null;
gv_Rota.Columns.Clear();
BoundField bfield = new BoundField();
bfield.HeaderText = "Days";
bfield.DataField = "Days";
gv_Rota.Columns.Add(bfield);
for (int i = 0; i < Amount; i++)
{
int week = i + 1;
string sWeek = "Week " + week.ToString();
TemplateField tfield = new TemplateField();
tfield.HeaderText = sWeek;
gv_Rota.Columns.Add(tfield);
}
DataTable dt = new DataTable();
dt.Columns.Add(new DataColumn("Days", typeof(string)));
dt.Rows.Add("M");
dt.Rows.Add("T");
dt.Rows.Add("W");
dt.Rows.Add("T");
dt.Rows.Add("F");
dt.Rows.Add("S");
dt.Rows.Add("S");
gv_Rota.DataSource = dt;
gv_Rota.DataBind();
}
这是我获取所选列数量并调用方法创建 GridView 的地方,我还将数量存储在缓存中:
protected void ddl_RotaAmountOfWeeks_SelectedIndexChanged(object sender, EventArgs e)
{
if (IsPostBack)
{
int Amount;
int.TryParse(ddl_RotaAmountOfWeeks.SelectedItem.ToString(), out Amount);
ColumnCount = Amount;
Cache.Add("columnCount", ColumnCount, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 60, 0), CacheItemPriority.Default, null);
BindGrid(Amount);
}
}
按钮代码:
protected void btn_AddRota_Click(object sender, EventArgs e)
{
//My first attempt at trying to save the GridView, realising I Cache'd the GridView before selecting items.
//gv_Rota = (GridView)Cache["cacheGridView"];
//Set of the size of the array to the amount of rows * colums
//this will be the maximum amount of events added
int arraysize = gv_Rota.Rows.Count * (int)Cache["columnCount"];
//the current array item being added
int arrayitem = 0;
//Finally an array of ROTA_EVENTS to pass to WCF
wsPersonnel.DC_WFM_ROTA_EVENTS[] arrayofRotaEvents = new wsPersonnel.DC_WFM_ROTA_EVENTS[arraysize];
foreach (GridViewRow row in gv_Rota.Rows)
{
for (int i = 1; i <= (int)Cache["columnCount"]; i++)
{
int day = row.RowIndex;
day += 1;
DropDownList DDL1 = (DropDownList)gv_Rota.Rows[row.RowIndex].Cells[i].FindControl("ddlShiftWK" + i.ToString() + "DAY" + day.ToString());
wsPersonnel.DC_WFM_ROTA_EVENTS dcEvent = new wsPersonnel.DC_WFM_ROTA_EVENTS
{
SHIFT_ID = DDL1.SelectedItem.Value,
WEEK = i,
WEEKSpecified = true,
DAY = day,
DAYSpecified = true,
};
arrayofRotaEvents[arrayitem++] = dcEvent;
}
}
wsP.AddRota(new wsPersonnel.DC_WFM_ROTA
{
ROTA_NAME = txt_RotaName.Value,
PERIOD_TYPE = 1,
PERIOD_TYPESpecified = true,
PERIOD_AMOUNT = ColumnCount,
PERIOD_AMOUNTSpecified = true,
ROTA_EVENTS = arrayofRotaEvents
});
}
GridView 外观示例:
【问题讨论】:
-
您的问题的答案不是 UpdatePanels(恕我直言,没有答案是使用 UpdatePanels)我猜这是因为您正在动态添加控件,然后当您回发控件时不再存在因为你没有反弹它们。您需要提供更多代码,例如在哪里绑定 GridView 和 RowDataBound 方法等
-
@matt_lethargic 是对的,你需要添加更多的cs页面代码
-
@matt_lethargic 我已经提供了我的 C# 代码。问题已编辑。感谢回复。
-
@EliotE123 你尝试使用 GridView1_RowCreated 方法而不是 rowdatabound 事件
-
@EliotE123 保存按钮的代码是什么 btn_AddRota_Click 代码?请添加代码
标签: c# asp.net ajax gridview updatepanel