1
我有一個網格,我使用複選框來編輯網格行。在複選框上點擊我如何保留下拉值?在gridview中保留下拉值
<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的列,它有一個選定的值號。但是當我點擊複選框時,值爲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;
}
我不打算從數據庫中提取值。我很難編碼下拉值。 – Learner