0

我有一组列表视图,每个列表视图中都有一个复选框:

<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>

在按钮上单击我想选择与特定复选框关联的 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.

请帮帮我

4

2 回答 2

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
        }

    }

}

或者,如果您只需要在 Button click (循环)上选择 id,请按照以下链接进行操作。

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

于 2013-08-03T08:01:14.580 回答
0

您必须先检查此控件的类型

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

更多信息。关于作为 关键字

于 2012-12-10T16:18:24.143 回答