2012-12-10 98 views
0

我有一组列表视图中,在每个列表视图复选框为:一个按钮访问listview内的复选框?

<telerik:RadListView Skin="Vista" ID="RadListView3" DataSourceID="SqlDataSource_subentry" 
            runat="server" ItemPlaceholderID="EmployeesContainer" DataKeyNames="ID"> 
            <LayoutTemplate> 
             <fieldset id="FieldSet1"> 
              <legend>Issues</legend> 
              <asp:PlaceHolder ID="EmployeesContainer" runat="server" ></asp:PlaceHolder> 

              <br /> 
              <br />             
             </fieldset> 
            </LayoutTemplate> 
            <ItemTemplate> 
             <div style="width:200px; height:165px; -webkit-border-radius: 20px;-moz-border-radius: 20px; border-radius: 20px; 
              border:2px solid black; background-color:#00FFFF; text-align:center; margin-top:20px; margin-bottom:10px; 
              margin-left:20px; "> 
              <br /> 
              <div style=" width:190px; height:auto; font-size:small;color:#000099; text-align:left; margin-left:12px;"><%#Eval("Worked_Project")%>  
               &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; 
               <label id="Label1" runat="server"><%# Eval("ID") %></label> 
               <asp:CheckBox ID="MyCheckBox" runat="server" Text=<%# Eval("ID") %>/></div> 
              <div style="width:190px; height:auto; font-size:small;color:black;text-align:center; padding-left:2px; margin-left:5px;" ><%# DataBinder.Eval(Container.DataItem, "Week_Ending", "{0:d/M/yyyy }")%></div> 
              <div style=" width:190px; height:75px; font-size:large; color:#000099; text-align:center; "><%#Eval("Activity")%> </div> 
              <div style=" width:190px; height:auto; font-size:small;color:black; text-align:left; margin-left:12px; margin-bottom:5px; "><%#Eval("UserId")%> </div> 

             </div> 


            </ItemTemplate> 

点击我要挑与特定的复选框作为assocaiated的ID:

protected void Button1_Click(object sender, EventArgs e) 
{ 


    foreach (Control ctl in RadListView3.Controls) 
    { 


     CheckBox chk = (CheckBox)ctl.FindControl("MyCheckBox"); 

     if (chk.Checked== true) 
      { 

       string value = chk.Text.ToString(); 
       Session["id"] = value.ToString(); 
       Label2.Text = Session["id"].ToString(); 
      } 

    } 




} 

但上的代码行给错误:

if (chk.Checked== true) 

和误差是

Object reference not set to an instance of an object. 

plzz帮我出

回答

0

你必须检查这种控制第一

CheckBox chk = ctl as CheckBox ; 
if (chk !=null) 
{ 
//do something 
} 

更多信息的类型。约as 关键字

1

试试这个:

protected void RadListView1_ItemCommand(object sender,Telerik.Web.UI.RadListViewCommandEventArgs e) 
{ 
    CheckBox button = (CheckBox)e.ListViewItem.FindControl("CheckBox1"); 

    if (e.CommandName == "Approve") 
    { 
     if (button.Checked == true) 

     { 
      //your code 
     } 

     else 

     { 
      //your code 
     } 

    } 

} 

或者只有当你需要选择的按钮点击ID(环路)按照下面的链接。

http://www.telerik.com/community/forums/aspnet-ajax/listview/get-selected-value-of-radlistview.aspx