当它处于编辑模式时,我尝试将列表框中的值多选到模板字段中。有办法吗?
我试过这样的事。我添加HiddenField来绑定一个值,然后我想要选择选择值的JS代码(我可以这样做但我在class
中没有HiddenField
所以这不是一个好的解决方案)。
此外,我不知道是否有机会通过服务器端进行此操作,因为它无法识别我的列表框中的ID
<asp:TemplateField HeaderText="Conditions" ItemStyle-Width="100px">
<EditItemTemplate>
<asp:HiddenField runat="server" ID="hdn_dd_conditions" class="bottom" Value='<%# Bind("conditions") %>' />
<asp:ListBox ID="dd_conditions" runat="server" CssClass="conditions" SelectionMode="Multiple" multiple="multiple">
<asp:ListItem Value="Value1" Text="Text2"></asp:ListItem>
<asp:ListItem Value="Value2" Text="Text2"></asp:ListItem>
<asp:ListItem Value="Others" Text="Others"></asp:ListItem>
</asp:ListBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("conditions") %>' Font-Size="10px"></asp:Label>
</ItemTemplate>
</asp:TemplateField>
有什么想法吗?
答案 0 :(得分:0)
您需要访问GridView
的RowDataBound事件中的ListBoxprotected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
//check if the row is a datarow and in edit mode
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) > 0)
{
//cast the row back to a datarowview
DataRowView row = e.Row.DataItem as DataRowView;
//use findcontrol to locate the listbox and cast it
ListBox lb = e.Row.FindControl("dd_conditions") as ListBox;
//loop all the items in a listbox
for (int i = 0; i < lb.Items.Count; i++)
{
//check the values and set the Selected property
if (lb.Items[i].Value == row["conditions"].ToString())
{
lb.Items[i].Selected = true;
}
}
}
}