好的,本周早些时候,我根据条件在GridView中突出显示了一个单元格,我得到了一些帮助。
现在,我想在列状态中添加一个下拉列表(已完成或未完成)。我得到了它的工作,但我不知道如果用新的用户输入更新字段后如何更改颜色。
我尝试使用以下代码来执行此操作,但我猜测我需要调用DDL或模板吗?
关于如何实现这一目标的任何想法?
我仍然没有运气。看起来当状态字段通过下拉列表更新时,它不会解释新数据(autopostback设置为true。)基本上,它认为该值仍然是" null"。有什么想法吗?
<asp:GridView ID="GridView2" runat="server" AutoGenerateColumns="False" DataKeyNames="SID" DataSourceID="SqlDataSource2" OnDataBound="GridView2_SelectedIndexChanged" OnSelectedIndexChanged="GridView2_SelectedIndexChanged">
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="SID" HeaderText="SID" InsertVisible="False" ReadOnly="True" SortExpression="SID" />
<asp:BoundField DataField="Servers" HeaderText="Servers" SortExpression="Servers" />
<asp:TemplateField HeaderText="Status" SortExpression="Status">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("Status") %>' OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
<asp:ListItem>done</asp:ListItem>
<asp:ListItem>not done</asp:ListItem>
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
代码背后:
protected void GridView2_SelectedIndexChanged(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView2.Rows)
{
if (row.Cells[3].Text == "done")
{
row.BackColor = System.Drawing.Color.LimeGreen;
}
else if (row.Cells[3].Text == "not done")
{
row.BackColor = System.Drawing.Color.Red;
}
}
}
答案 0 :(得分:0)
如果我正确理解你,你需要处理DropDownList的SelectedIndexChanged
事件,而不是GridView。然后,您将使用与已有内容类似的逻辑,但根据更改的DropDownList找到GridViewRow。
确保您的DropDownList将处理SelectedIndexChanged
事件。请记住DropDownLists不会自动导致回发,除非被告知。
<asp:DropDownList ID="DropDownList1" runat="server"
SelectedValue='<%# Bind("Status") %>'
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged"
AutoPostBack="True">
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList ddl = (DropDownList)sender;
GridViewRow row = (GridViewRow)ddl.NamingContainer;
if(ddl.SelectedItem.Text == "done")
{
row.BackColor = System.Drawing.Color.LimeGreen;
}
else if(ddl.SelectedItem.Text == "not done")
{
row.BackColor = System.Drawing.Color.Red;
}
}