2010-04-14 53 views
1

如何解决此异常?未将对象引用设置为对象的实例(NullreferenceException未由用户代码处理)

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() 
+0

更多的解释是必要的,添加相关的代码 – Brij 2010-04-14 12:18:01

+0

你可以粘贴中继ASPX一部分?确保imagepathlit不存在于您要查找的位置。如果你粘贴aspx,我们可以用适当的方法来回答 – 2010-04-14 12:22:53

回答

1

我的猜测是,有没有在DownloadsRepeater发现控制控制称为imagepathlit,因此调用后imagepathlit控件为空。

请记住,Control.FindControl()根据ID查找控件,而不是控件的名称。因此,要找到集合中的控制......你就必须有这样的事情早在应用程序:

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

UPDATE

由于您使用中继器,子控件有点不同。您将在Repeater中为Item中的每个Literal实例。因此,为了得到控制的每个实例,您可以通过在RepeaterItems必须循环,并呼吁FindControl()每个Item

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

字面值是这样的.aspx; Phil 2010-04-14 12:35:52

+0

非常感谢Justin – Phil 2010-04-14 12:50:23

1

假设你发布的代码是在异常的确抛出,我会说DownloadRepeater没有控件,它的ID为imagepathlit

检查您的aspx

1

由于控件位于ItemTemplate中,因此无法使用repeater.findcontrol;因为itemtemplate是可重复的,所以必须遍历中继器的项目来查找控件。因此,您必须遍历每一个来查找控件,如下所示:

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

使用该语法。

-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); 
    } 
} 
+0

您应该添加一些说明,以解决 – timiTao 2018-02-28 10:51:20

+0

8年前提出这个问题,它不是一个通用的问题其他人可以从您的解决方案中受益。如果**你**有问题,那么正确的做法是*提出一个新问题*,不要在这个或任何其他存在的问题上写出答案。 **请删除这个答案。** – Igor 2018-02-28 11:29:25

相关问题