如何解决此异常?
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> </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 :(得分: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 += "¤cy=USD";
Url += "&return=" +
ConfigurationManager.AppSettings["SuccessURL"].ToString();
Url += "&cancel_return=" +
ConfigurationManager.AppSettings["FailedURL"].ToString();
Response.Redirect(Url);
}
}