1.在一个DataGrid里面有一个CheckBoxList,并已经绑定数据;
2.CheckBoxList里面分别有:A,B,C三个选项;
3.当点击DataGrid里面的"编辑"时进行编辑;问题:因为我在绑定DataGrid时 CheckBoxList里面已经进行选择了,即已经有数据了,
但当我点击"编辑"时,CheckBoxList的选项都没有"打勾",即是:我本来已经选择了"C",但编辑时CheckBoxList里面的C项不是打勾.我需要的是有打勾的.应该怎么做呢?说得不太清楚,希望有朋友能够明白我的意思并给我指点迷津,谢谢!
2.CheckBoxList里面分别有:A,B,C三个选项;
3.当点击DataGrid里面的"编辑"时进行编辑;问题:因为我在绑定DataGrid时 CheckBoxList里面已经进行选择了,即已经有数据了,
但当我点击"编辑"时,CheckBoxList的选项都没有"打勾",即是:我本来已经选择了"C",但编辑时CheckBoxList里面的C项不是打勾.我需要的是有打勾的.应该怎么做呢?说得不太清楚,希望有朋友能够明白我的意思并给我指点迷津,谢谢!
{
DataGrid.DataBind();绑定代码在这里面
}
并在 ItemDataBound事件中重新找到CheckBoxList并绑定你的数据private void DataGrid1_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
{
CheckBoxList c;
if(e.Item.ItemType == ListItemType.EditItem)
{
c = (CheckBoxList)e.Item.FindControl("CheckBoxList2");
if(c != null)
{
//重新绑定c的数据,并设置其选中项
}
}
}
以下是代码:<asp:TemplateColumn HeaderText="員工表現">
<ItemStyle VerticalAlign="Top"></ItemStyle>
<ItemTemplate>
<asp:Label runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.Behave") %>' ID="lblBehave">
</asp:Label>
</ItemTemplate>
<EditItemTemplate>
<asp:CheckBoxList runat="server" Width="156px" ID="cblBehave">
<asp:ListItem Value="0">代表工作表現很差</asp:ListItem>
<asp:ListItem Value="1">代表工作表現差</asp:ListItem>
<asp:ListItem Value="2">代表工作表現一般</asp:ListItem>
<asp:ListItem Value="3">代表工作表現好</asp:ListItem>
<asp:ListItem Value="4">代表工作表現很好</asp:ListItem>
<asp:ListItem Value="甲">代表甲班</asp:ListItem>
<asp:ListItem Value="乙">代表乙班</asp:ListItem>
<asp:ListItem Value="丙">代表丙班</asp:ListItem>
<asp:ListItem Value="丁">代表丁班</asp:ListItem>
</asp:CheckBoxList>
</EditItemTemplate>
</asp:TemplateColumn>
{
DataGrid.DataBind();绑定代码放在这里面
}
编辑以后会触发这个事件的,你把在ItemDataBound里面写的事件再写一遍(就绑定打钩的代码),这样就可以了阿
string strSQL = "Select Behave From RaisesTemp Where ViewOrder=@ViewOrder";
SqlCommand myCommand = new SqlCommand(strSQL,myConnection);
myConnection.Open();
myCommand.Parameters.Add(new SqlParameter("@ViewOrder",SqlDbType.Int));
myCommand.Parameters["@ViewOrder"].Value = ((Label)e.Item.FindControl("lblViewOrder")).Text;
ViewState["Behave"] = myCommand.ExecuteScalar();
myConnection.Close();
string str = ViewState["Behave"].ToString();
lblMessage.Text = str.ToString();后面关于赋值给CheckBoxList并作出带有打勾的动作就不知道怎么写了.求解,谢谢!
// 然后 ->protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e)
{
CheckBoxList chk;
if(e.Item.ItemType == ListItemType.EditItem)
{
chk = (CheckBoxList)e.Item.FindControl("cblBehave");
if(chk != null)
{
DataRowView drv = e.Item.DataItem as DataRowView;
ListItem item = chk.Items.FindByValue(drv["Behave"].ToString());
chk.SelectedIndex = item == null ? -1 : chk.Items.IndexOf(item);
}
}
}