首页 新闻 会员 周边

我想获取DataList中CheckBox哪些被勾选了,可是程序运行时却出现了 “未将对象引用设置到对象的实例”

0
悬赏园豆:100 [已关闭问题] 关闭于 2015-03-26 16:27
 <div style=" width:825px; background-color:#f6f6f6; padding-bottom:20px;" >
            <div style=" width:800px;">
                <div style="width:100%; vertical-align:middle;"> 
                    <input type="checkbox"  onclick="GetAllCheckBox(this)"  />全选
                    <img src="../images/delete1.png" alt=""  runat="server" onclick="DeleteVideoByMultiID()"/>
                    <asp:Button ID="Button1" runat="server" Text="删除" onclick="Button1_Click" />
                </div>
                <div style=" width:800px;" class="datalist">
                    <asp:DataList ID="DataList1" runat="server" Width="752px">
                        <ItemTemplate>
                        <div style="width:752px; height:180px; margin-top:20px;">
                            <table style="width:700px; background-color:White; height:180px; margin-left:auto; margin-top:auto;">
                                <tr style="height:10px;">
                                </tr>
                                <tr>
                                    <td style=" text-align:left; vertical-align:top; width:35px;"><input type="checkbox" name="id"  id="ckboxitem"/></td>
                                    <td style="width:230; text-align:left;vertical-align:top;" > <a href="VideoPlay.aspx?VideoID=<%#Eval("VideoID")%>" target="_blank"><img src="../images/VideoUnSet.jpg" style=" border:solid 1px gray;"  alt="" /></a>
                                    </td>
                                    <td style="text-align:left;vertical-align:top; width:300px">
                                        <table >
                                            <tr>
                                                <td>
                                                    <h2 style="margin-left:5px; margin-top:5px; font-size:x-large; color:Black;"><strong><%#Eval("VideoName")%></strong></h2>
                                                </td>
                                            </tr>
                                            <tr style="text-indent: 5px"><td><%#Eval("VideoDateTime")%>上传</td></tr>
                                            <tr style="text-indent: 5px"><td>播放数:<%#Eval("ClickSum")%></td></tr>
                                        </table> 
                                    </td>
                                    <td style="width:135px;">
                                        <div style="margin-top:10px;  height:100px;">
                                            <div style="float:right;margin-right:10px;">
                                                
                                                <br />
                                                <div style=" height:25px;">
                                                <img src="../images/delete4.png"  id="Img1"  alt="" style=" vertical-align:middle" />
                                                <asp:LinkButton ID="Datalist_Delete" runat="server" CommandName="Delete" CausesValidation="False"  Text="删除" CommandArgument='<%#Eval("VideoID")%>' OnCommand="DeleteVideoByID" ForeColor="DimGray" Height="25px"></asp:LinkButton>
                                                </div>
                                            </div>
                                        </div>
                                        
                                    </td>
                                    <td>
                                        <div><asp:HiddenField ID="HiddenField1" runat="server"  Value='<%#Eval("VideoID")%>'/></div>
                                    </td>
                                </tr>
                                
                            </table>
                        </div>
                        </ItemTemplate>
                    </asp:DataList>
                </div>
            </div>
        </div>

//从数据库中通过不同VideoID删除多个视频
public void DeleteVideoByMultiID()
{
//存储要删除视频的VideoID
string videoIDList="";
//获取要删除的VideoID
foreach (DataListItem item in DataList1.Items)
{
CheckBox ckBox = (CheckBox)item.FindControl("ckboxitem"); //通过ID找到CheckBox
if (ckBox.Checked)
{
HiddenField hideID = (HiddenField)item.FindControl("HiddenField1");
videoIDList += hideID.Value;
videoIDList += ",";
}
}
if (bllVideo.DeleteList(videoIDList) == false)
{
Response.Write("<script>alert(\'请选择要删除的视频\')</script>");
}
else
{
Response.Write("<script>alert(\'删除成功!\')</script>");
//删除后重新绑定Datalist,绑定的页数为第一页
BindPaging("1");
}
}

问题补充:

//从数据库中通过不同VideoID删除多个视频 public void DeleteVideoByMultiID() { //存储要删除视频的VideoID string videoIDList=""; //获取要删除的VideoID foreach (DataListItem item in DataList1.Items) { CheckBox ckBox = (CheckBox)item.FindControl("ckboxitem"); //通过ID找到CheckBox if (ckBox.Checked) { HiddenField hideID = (HiddenField)item.FindControl("HiddenField1"); videoIDList += hideID.Value; videoIDList += ","; } } if (bllVideo.DeleteList(videoIDList) == false) { Response.Write(" "); } else { Response.Write(" "); //删除后重新绑定Datalist,绑定的页数为第一页 BindPaging("1"); } }

Paddy138的主页 Paddy138 | 初学一级 | 园豆:109
提问于:2015-03-26 15:44
< >
分享
所有回答(3)
0

只要是报 "未将对象引用设置到对象的实例的" 错误,

你检查那几个有带"."的地方就行了,某个对象为Nothing时会出这个错。

爱编程的大叔 | 园豆:30839 (高人七级) | 2015-03-26 15:51
0

进行断点跟踪,然应该是DataList1.Items是Null 值,你查看下获取这个值是否正确,另外就是绑定的时候是否是正确绑定

尐潴丶YvonneSKy | 园豆:204 (菜鸟二级) | 2015-03-26 16:07
0

因为我的CheckBox没写runat=“server”,不属于服务器控件,所以后台程序获取不到

Paddy138 | 园豆:109 (初学一级) | 2015-03-26 16:22
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册