问题和说明:
我有一个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的示例:
答案 0 :(得分:0)
如果要在更新面板内单击按钮上停止回发,则可以尝试此
<asp:UpdatePanel runat="server" ID="RotaUpdatePanel" UpdateMode="Conditional">
<ContentTemplate>
<asp:Button ID="btn_AddRota" runat="server" Text="Add" OnClick="btn_AddRota_Click" CssClass="ButtonAdminPage" ClientIDMode="AutoID"/>
</ContentTemplate>
<Triggers>
<asp:AsyncPostBackTrigger ControlID="btn_AddRota" EventName="Click" />
</Triggers>
</asp:UpdatePanel>