访问listview中的复选框?

时间:2012-12-10 14:22:43

标签: c# asp.net listview checkbox

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

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

plzz帮帮我

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

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

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

更多信息。关于as  关键字