【发布时间】:2014-02-06 09:33:53
【问题描述】:
我有一个网格,我使用复选框来编辑网格行。在复选框上单击如何保留下拉值?
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:CheckBox ID="chkAll" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</HeaderTemplate>
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" OnCheckedChanged="OnCheckedChanged" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Scope">
<HeaderStyle HorizontalAlign="Center" Wrap="False" CssClass="header">
</HeaderStyle>
<ItemTemplate>
<asp:Label ID="lblScope" runat="server" Text='<%# Bind("Scope") %>'></asp:Label>
<asp:DropDownList ID="ddlCMS" Visible="false" runat="server">
<asp:ListItem>Yes</asp:ListItem>
<asp:ListItem>No</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
<ItemStyle Wrap="false" CssClass="header" />
</asp:TemplateField>
</Columns>
我正在使用下面的代码来检查和取消选中网格行以进行编辑。因此,当我单击复选框时,我无法零售 gridview 选择的下拉值。例如:第三行有一个名为 scope 的列有一个选定的值 No。但是当我点击复选框时,值是 Yes,因为这是我在下拉列表中绑定的顺序。
protected void OnCheckedChanged(object sender, EventArgs e)
{
bool isUpdateVisible = false;
CheckBox chk = (sender as CheckBox);
if (chk.ID == "chkAll")
{
foreach (GridViewRow row in Updates.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked = chk.Checked;
}
}
}
CheckBox chkAll = (Updates.HeaderRow.FindControl("chkAll") as CheckBox);
chkAll.Checked = true;
foreach (GridViewRow row in Updates.Rows)
{
if (row.RowType == DataControlRowType.DataRow)
{
bool isChecked = row.Cells[0].Controls.OfType<CheckBox>().FirstOrDefault().Checked;
for (int i = 1; i < row.Cells.Count; i++)
{
if (row.Cells[i].Controls.OfType<Label>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<Label>().FirstOrDefault().Visible = !isChecked;
}
if (row.Cells[i].Controls.OfType<TextBox>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<TextBox>().FirstOrDefault().Visible = isChecked;
}
if (row.Cells[i].Controls.OfType<DropDownList>().ToList().Count > 0)
{
row.Cells[i].Controls.OfType<DropDownList>().FirstOrDefault().Visible = isChecked;
}
if (isChecked && !isUpdateVisible)
{
isUpdateVisible = true;
}
if (!isChecked)
{
chkAll.Checked = false;
}
}
}
}
btnSave.Visible = isUpdateVisible;
}
【问题讨论】: