为什么我收到错误:“对象引用未设置为对象的实例” [英] Why I am getting the error: "Object reference not set to an instance of an object"

查看:97
本文介绍了为什么我收到错误:“对象引用未设置为对象的实例”的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助!!



这是.aspx代码

Please help!!

This is the .aspx code

<%@ Page Title="" Language="C#" MasterPageFile="~/shopmaster.master" AutoEventWireup="true" CodeFile="Productdetails.aspx.cs" Inherits="Productdetails" %>
<%@ MasterType VirtualPath="~/shopmaster.master" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    
     

    
    

    
    <asp:DataList ID="DataList1" runat="server" DataKeyField="pcode" 

        DataSourceID="SqlDataSource1">
        <ItemTemplate>
        <div style="width:100%">
            <table style="width: 45%; float:left">
                <tr>
                    <td>
                        <asp:Image ID="Image1" runat="server" Height="100%" 

                            ImageUrl='<%# Eval("image")%>' Width="90%" />
                    </td>
                </tr>
            </table>
            <table style="width: 50%; float:right">
                <tr>
                    <td>
                        <span style="color: #FF0000">Product Neme</span>:<asp:Label ID="Label1" runat="server" Text='<%# Eval("pname")%>'></asp:Label>
                        <br /><br />
                        <span style="color: #FF0000">Product Code</span>:<asp:Label ID="Label2" runat="server" Text='<%# Eval("pcode")%>'></asp:Label>
                        <br /><br />
                        <span style="color: #FF0000">Product Description</span>:<asp:Label ID="Label3" runat="server" Text='<%# Eval("description")%>'></asp:Label>
                        <br /><br /><br />
                        <span style="color: #FF0000">Price</span>:Rs.<asp:Label ID="Label4" runat="server" Text='<%# Eval("price")%>'></asp:Label>
                        <br /><br /><br />
                        <asp:ImageButton ID="ImageButton1" runat="server" Height="39px" 

                            ImageUrl="~/web/images/add-to-cart-button-blue.png" 

                            onclick="ImageButton1_Click1" 

                            PostBackUrl='<%# Eval("pcode","cart.aspx?pcode={0}") %>' Width="147px" />
                        <br />
                    </td>
                </tr>
            </table>
            </div>
            <br />
            <br />
            <br />
            <br />
            <br />
        </ItemTemplate>
    </asp:DataList>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 

        ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 

        SelectCommand="SELECT [pname], [pcode], [image], [description], [price] FROM [products] WHERE ([pcode] = @pcode)">
        <SelectParameters>
            <asp:QueryStringParameter Name="pcode" QueryStringField="pcode" 

                Type="Decimal" />
        </SelectParameters>
    </asp:SqlDataSource>

    
    


    
</asp:Content>





这是.aspx.cs代码



this is the .aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Data;

public partial class Productdetails : System.Web.UI.Page
{
    string un;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Master.FindControl("Label_UserName").Visible == false)
        {
            DataList1.FindControl("ImageButton1").Visible = false;
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }


    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {

    }
}

推荐答案

ConnectionStrings:ConnectionStri ng %>

SelectCommand = SELECT [pname],[pcode],[image],[description],[price] FROM [products] WHERE([pcode] = @pcode) >
< SelectParameters >
< asp:QueryStringParameter 名称 = pcode QueryStringField = pcode

类型 = 十进制 / >
< / SelectParameters >
< / asp:SqlDataSource >






< / asp:Content >
ConnectionStrings:ConnectionString %>" SelectCommand="SELECT [pname], [pcode], [image], [description], [price] FROM [products] WHERE ([pcode] = @pcode)"> <SelectParameters> <asp:QueryStringParameter Name="pcode" QueryStringField="pcode" Type="Decimal" /> </SelectParameters> </asp:SqlDataSource> </asp:Content>





这是.aspx.cs代码



this is the .aspx.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Configuration;
using System.Data.SqlClient;
using System.IO;
using System.Data;

public partial class Productdetails : System.Web.UI.Page
{
    string un;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (this.Master.FindControl("Label_UserName").Visible == false)
        {
            DataList1.FindControl("ImageButton1").Visible = false;
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {

    }


    protected void DataList1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }
    protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void ImageButton1_Click1(object sender, ImageClickEventArgs e)
    {

    }
}


The problem is that DataList1.FindControl(\"Image Button1\") is null. The reason for that is ImageButton1 is part of a template, so not really ever will be a ImageButton1 control at runtime. The controls you declared in the template used to create - repeatedly - runtime controls according the data the container bind to...
The problem is that DataList1.FindControl("ImageButton1") is null. The reason for that is ImageButton1 is part of a template, so not really ever will be a ImageButton1 control at runtime. The controls you declared in the template used to create - repeatedly - runtime controls according the data the container bind to...


这篇关于为什么我收到错误:“对象引用未设置为对象的实例”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆