[英]C# GridView Dynamic CheckBox Disappearing
I have a fairly simple application which returns a list of failed reports with an ID, Name and Time Bound fields and Checkbox template field on the left. 我有一个相当简单的应用程序,该应用程序返回带有ID,名称和时间限制字段以及左侧复选框模板字段的失败报告列表。
I have a 'Reschedule' button which when pressed, should pick up the rows where the checkbox has been ticked and process them. 我有一个“重新计划”按钮,当按下该按钮时,应选择已选中复选框的行并进行处理。
The GridView loads up successfully and I can select/unselect the CheckBoxes but when I press the 'Reschedule' button and return to the code, the checkboxes no longer exist. GridView成功加载,我可以选择/取消选择复选框,但是当我按下“重新计划”按钮并返回到代码时,该复选框不再存在。
I know this is related to Dynamic Controls/Postback and that the Checkboxes need to be re-created and I've tried numerous suggestions to previous similar questions but nothing has worked 我知道这与动态控件/回发有关,并且需要重新创建复选框,并且我已经尝试了许多针对以前类似问题的建议,但是没有任何效果
GridView - AutoGenerate Columns False (tried true) GridView-自动生成列False(尝试过)
Button - OnClientClick="" (tried return false) 按钮-OnClientClick =“”(尝试返回false)
The fields are initially created and bound to a data table (the data table has 3 columns mapping to the 3 Bound fields) like this:- 这些字段最初是创建并绑定到数据表的(数据表有3列映射到3个Bound字段),如下所示:
TemplateField tfield = new TemplateField();
failedSchedulesGridView.Columns.Add(tfield);
BoundField bfield1 = new BoundField();
bfield1.HeaderText = "SI_ID";
bfield1.DataField = "si_id";
failedSchedulesGridView.Columns.Add(bfield1);
BoundField bfield2 = new BoundField();
bfield2.HeaderText = "SI_NAME";
bfield2.DataField = "si_name";
failedSchedulesGridView.Columns.Add(bfield2);
BoundField bfield3 = new BoundField();
bfield3.HeaderText = "SI_UPDATE_TS";
bfield3.DataField = "si_update_ts";
failedSchedulesGridView.Columns.Add(bfield3);
failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();
Page_Load As can be seen I've tried recreating the GridView columns here but it didn't work and is commented out 可以看到,Page_Load我尝试在这里重新创建GridView列,但是它没有用,已被注释掉
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//TemplateField tfield = new TemplateField();
//failedSchedulesGridView.Columns.Add(tfield);
//BoundField bfield1 = new BoundField();
//bfield1.HeaderText = "SI_ID";
//bfield1.DataField = "si_id";
//failedSchedulesGridView.Columns.Add(bfield1);
//BoundField bfield2 = new BoundField();
//bfield2.HeaderText = "SI_NAME";
//bfield2.DataField = "si_name";
//failedSchedulesGridView.Columns.Add(bfield2);
//BoundField bfield3 = new BoundField();
//bfield3.HeaderText = "SI_UPDATE_TS";
//bfield3.DataField = "si_update_ts";
//failedSchedulesGridView.Columns.Add(bfield3);
}
failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();
}
OnRowDataBound OnRowDataBound
protected void OnRowDataBound(Object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType != DataControlRowType.Header)
{
cbx++;
CheckBox cb = new CheckBox();
cb.ID = "cb" + cbx;
e.Row.Cells[0].Controls.Add(cb);
}
}
The Code fails when I try to access the Checkboxes after the 'Reschedule' button is pressed because the checbox is not found :- 当我尝试按“重新计划”按钮后尝试访问复选框时,代码失败,因为未找到复选框:
protected void ReschedulePB2_Click(object sender, EventArgs e)
{
int i = 0;
foreach (GridViewRow row in failedSchedulesGridView.Rows)
{
i++;
string cbName = "cb" + i;
CheckBox cb = (CheckBox)row.Cells[0].FindControl(cbName);
if (cb.Checked)
try this 尝试这个
Write this in your aspx page 写在你的aspx页面
<asp:GridView ID="failedSchedulesGridView" runat="server" AutoGenerateColumns="False" OnRowDataBound="GridView1_RowDataBound" Width="850px" onrowcommand="GridView1_RowCommand">
<Columns>
<asp:TemplateField >
<HeaderTemplate>
<asp:CheckBox ID="cbHeader" runat="server" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="cbItem" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="si_id" HeaderText="SI_ID" SortExpression="si_id" />
<asp:BoundField DataField="si_name" HeaderText="SI_NAME" SortExpression="si_name" />
<asp:BoundField DataField="si_update_ts" HeaderText="SI_UPDATE_TS" SortExpression="si_update_ts" />
</Columns>
</asp:GridView>
your page load should look like this 您的页面加载应如下所示
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
failedSchedulesGridView.DataSource = dt;
failedSchedulesGridView.DataBind();
}
}
your reschedule click would be 您的重新安排点击将是
protected void ReschedulePB2_Click(object sender, EventArgs e)
{
foreach (GridViewRow row in failedSchedulesGridView.Rows)
{
CheckBox cb = (CheckBox)row.Cells[0].FindControl("cbItem");
if (cb.Checked)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.