日期:2014-05-20  浏览次数:20490 次

datalist嵌套datalist,checkbox无法选中问题
datalist嵌套datalist,里层datalist中有控件checkbox,代码选中checkbox却不能实现,都说要在内层的datalist再绑定次,我也绑定了却不起作用,大家帮忙看下啊!

------解决方案--------------------
用js写check看看
for (int i = 0; i < dlTypeList.Items.Count; i++)
{
DataList dlWeightList = (DataList)dlTypeList.Items[i].FindControl( "dlWeightList ");
for (int j = 0; j < dlWeightList.Items.Count; j++)
{
ClientScript.RegisterStartupScript(this.GetType(), " ", " <script> document.getElementById( 'ckb ').check = true ; </script> ");

CheckBox ckb = (CheckBox)dlWeightList.Items[j].FindControl( "ckb ");

ckb.Checked = true;

int Dimension_ID = int.Parse(dlWeightList.DataKeys[j].ToString());
InitdltDimension(dlWeightList, Dimension_ID);//重新绑定
}
}

------解决方案--------------------
用 javascript 和隐藏INPUT 来处理选中的Checkbox对应的记录ID

------解决方案--------------------
eg:
嵌套的DataList里面放一个:
<input type= "checkbox " runat= "server " id= "checkbox1 " onclick= "clickBox() ">

嵌套的DataList绑定时:
private void DataList2_ItemDataBound(object sender, System.Web.UI.WebControls.DataListItemEventArgs e)
{
if(e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
{
HtmlInputCheckBox chk = e.Item.FindControl( "checkbox1 ") as HtmlInputCheckBox;
if(chk != null)
{
DataRowView dr = e.Item.DataItem as DataRowView;
if(dr != null)
chk.Attributes.Add( "sublistid ", "sub_ " + dr[ "IntegerValue "].ToString());
}
}
}
------解决方案--------------------
<asp:datalist id= "DataList1 " runat= "server " width= "100% ">
<itemtemplate>
<div> <%# DataBinder.Eval(Container.DataItem, "IntegerValue ") %> </div>
<hr width= "100% ">
<asp:datalist id= "DataList2 " runat= "server " width= "100% ">
<itemtemplate>
<hr width= "50% ">
<div> <%# DataBinder.Eval(Container.DataItem, "DateTimeValue ") %> : <input type= "checkbox " runat= "server " id= "checkbox1 " onclick= "clickBox() ">
checkbox
</div>
</itemtemplate>
</asp:datalist>
</itemtemplate>
</asp:datalist>

<script language= "javascript ">
<!--
function clickBox()
{
var obj = event.srcElement;
//sublistid= "sub_6 "
if(obj.checked)
alert( obj.getAttribute( "sublistid ") );
}
//-->
</script>

把选中的Checkbox的自定义属性sublistid的值保存到下来,POST后就可以处理了

------解决方案--------------------
如果你想让checkbox选中,那么“ ckb.Checked = true;”应该在整个页面的page_load事件之后的事件中执行。如果你的代码是在page_load 事件中或者之前执行的,好好考虑的你的逻辑。page_load是用来恢复上一个页面处理过程中的状态的,不是用来设置本次页面处理过程的状态的。