2010-04-14 55 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

相關問題