2012-12-10 31 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