I have a problem regarding with FindControl.I'm using ASPxRoundPanel(with DXperience component) in a repeater. I have a textbox called "txtAdet ID" in ASPxRoundPanel. I just can't access inside the Textbox.And I got the following the error.
"Object reference not set to an instance of an object."
ascx. code:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="urunLinq"
onitemcommand="Repeater1_ItemCommand">
<ItemTemplate>
<dxrp:ASPxRoundPanel ID="ASPxRoundPanel1" runat="server" Width="980px" ShowHeader="true" Height="550px">
<PanelCollection>
<dxrp:PanelContent>
<div style="width:980; overflow:hidden;height:350px;">
<div style="float:left; width:410px;height:310px;">
<asp:Image ID="Image1" Width="400" Height="300" ImageUrl='<%# "../urunResim/"+Eval("urunAnaResim") %>' runat="server" />
</div>
<div style="float:left;margin-top:0px;overflow:hidden;width:500px;">
<h2><%# Eval("urunAdi") %></h2>
<h4>Ürün Özellikleri</h4>
<p style="font-size:x-small;"><%# Eval("urunOzellikleri") %></p>
<br />
<table>
<tr>
<td><h5>Adet </h5></td><td><asp:TextBox ID="txtAdet" Width="50" runat="server"></asp:TextBox></td>
<td><h4> Peşin Fiyatı :</h4></td><td><h3 style="color:Red;"> <%# Eval("kdvliFiyat") %>TL</h3></td>
</tr>
</table>
<br />
<table>
<tr>
<td><a href="#">İnternet satış taksitlerini görmek için tıklayın.</a></td>
<td>
<asp:ImageButton ID="ImageButton1" CommandName="sepeteKaydet" runat="server" ImageUrl="~/img/icons/sepeteEkle.JPG" /></td>
</tr>
</table>
</div></div>
</dxrp:PanelContent>
</PanelCollection>
<HeaderTemplate>
<h3><%# Eval("urunAdi") %></h3>
</HeaderTemplate>
</dxrp:ASPxRoundPanel>
</ItemTemplate>
</asp:Repeater>
.cs code:
protected void Repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName=="sepeteKaydet")
{
TextBox bulunanTextKontrol = ((TextBox)e.Item.FindControl("txtAdet"));
string urunAdet = bulunanTextKontrol.Text;
string sessionId = Session.SessionID;
int urunId=Convert.ToInt32(Request["id"].ToString());
tempSepet geciciSepet = new tempSepet()
{
adet = Convert.ToInt32(urunAdet),
eklemeSaati = Convert.ToString(DateTime.Now.Hour.ToString()),
eklemeTarihi = Convert.ToString(DateTime.Now.ToShortDateString()),
sessionId = sessionId,
urunId = urunId
};
selcukData.tempSepets.InsertOnSubmit(geciciSepet);
selcukData.SubmitChanges();
Response.Write("<script type='text/javascript'>alert('Ürün sepetinize başarıyla eklendi')</script>");
((TextBox)e.Item.FindControl("txtAdet")).Text = null;
}
}
I got a error the following line.
string urunAdet = bulunanTextKontrol.Text;
ASPxRoundPanel control is a subclass of ASPxWebControl which implements INamingContainer interface. It means that FindControl called on any parent control (RepeaterItem at your case) won't be able to find anything inside ASPxRoundPanel (explanation here). So you need at least two steps to accomplish your task:
ASPxRoundPanel1 roundPanel = ((ASPxRoundPanel1)e.Item.FindControl("ASPxRoundPanel1"));
TextBox bulunanTextKontrol = ((TextBox)roundPanel.FindControl("txtAdet"));