对象引用未设置为对象的实例(NullreferenceException未被用户代码处理)

时间:2010-04-14 12:16:30

标签: asp.net syntax nullreferenceexception literals

如何解决此异常?

Dim imagepathlit As Literal = DownloadsRepeater.FindControl("imagepathlit")
        imagepathlit.Text = imagepath

这是转发器:

<asp:Repeater ID="DownloadsRepeater" runat="server">

<HeaderTemplate>
<table width="70%">
<tr>
<td colspan="3"><h2>Files you can download</h2></td>
</tr>
</HeaderTemplate>

<ItemTemplate>
<tr>
<td width="5%">
<asp:Literal ID="imagepathlit" runat="server"></asp:Literal></td>
<td width="5%"></td>
<td>&nbsp;</td>
</tr>
</table>
</ItemTemplate>

</asp:Repeater>

以下是获取转发器数据的代码:

c.Open()
        r = x.ExecuteReader
        While r.Read()
            If r("filename") Is DBNull.Value Then
                imagepath = String.Empty
            Else
                imagepath = "<img src=images/" & getimage(r("filename")) & " border=0 align=absmiddle>"
            End If

        End While
        c.Close()
        r.Close()

4 个答案:

答案 0 :(得分:1)

我的猜测是DownloadsRepeater控件中没有找到名为imagepathlit的控件,因此调用后imagepathlit控件为空。

请记住Control.FindControl()根据ID查找控件,而不是控件的名称。因此,要在集合中找到控件......您必须在应用程序的早期版本中使用此类内容:

Dim imagepathlit As Literal = new Literal()
imagepathlit.ID = "imagepathlit"

<强>更新

由于您使用的是转发器,因此子控件的布局略有不同。您将在Literal中为每个Item添加Repeater个实例。因此,要获取控件的每个实例,您必须遍历Items中的Repeater并在每个FindControl()上调用Item

For Each item As Item In DownloadsRepeater.Items
    Dim imagepathlit As Literal = item.FindControl("imagepathlit")
Next

答案 1 :(得分:1)

假设您发布的代码是确实抛出异常的地方,我会说DownloadRepeater中没有ID为imagepathlit的控件。

检查aspx

答案 2 :(得分:1)

因为控件在ItemTemplate中,所以不能使用repeater.findcontrol;你必须遍历转发器的项目来寻找控件,因为itemtemplate是可重复的。所以你必须循环遍历每一个以寻找控件,如:

foreach (var item in repeater.Items)
{
   var control = item.FindControl("ID") as Type;
}

使用该语法。

答案 3 :(得分:-1)

我的代码是:

protected void GridView2_RowCommand(object sender, GridViewCommandEventArgs e)
{
    SqlConnection Conn = new
  SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ToString());
    SqlCommand Cmd = new SqlCommand();
    if (e.CommandName =="Buy")
    {
        ImageButton img = (ImageButton)e.CommandSource;
        int Index = Convert.ToInt32(e.CommandArgument);
        GridViewRow Row = GridView2.Rows[Index];
        Label l1 = (Label)Row.FindControl("Label3");
        Label l2 = (Label)Row.FindControl("Label2");
        Label l3 = (Label)Row.FindControl("Label1");
        Session["user"] = "mohammad";
        Cmd = new SqlCommand("Insert Into Trade(pname,pdesc,price,uname)values('" + l1.Text + "','" + l2.Text + "','" + l3.Text.Replace("$", "") + "','" + Session["user"].ToString() + "')", Conn);
        Conn.Open();
        Cmd.ExecuteNonQuery();
        Conn.Close();
        string Url = "";
        Url += "https://www.sandbox.paypal.com/cgibin/webscr?cmd=_xclick&business=" + 
        ConfigurationManager.AppSettings["paypalemail"].ToString();
        Url += "&first_name=Mohamed";
        Url += "&city=chennai";
        Url += "&state=tamilnadu";
        Url += "&item_name=" + l1.Text;
        Url += "&amount=" + l3.Text.Replace("$", "");
        Url += "&shipping=5";
        Url += "&handling=5";
        Url += "&tax=5"; ;
        Url += "&quantity=1";
        Url += "&currency=USD";
        Url += "&return=" +
        ConfigurationManager.AppSettings["SuccessURL"].ToString();
        Url += "&cancel_return=" +
        ConfigurationManager.AppSettings["FailedURL"].ToString();
        Response.Redirect(Url);
    }
}