Reputation: 41
I'm facing a problem in my online shopping cart project .... The Problem is when i get/access the Textbox that is inside a repeater control... I used this below code but when i enter some value to that Textbox than it shows null value....
code behind:
protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addtoCartName")
{
foreach (RepeaterItem item in repeater1.Items)
{
TextBox txtName = (TextBox)item.FindControl("txtQuantity");
if (txtName!= null)
{
strText = strText + ", " + txtName.Text;
Response.Write("Text =" + strText);
}
}
}
aspx:
<asp:Button runat="server" ID="addtoCart" Text="Add to Cart" CommandName="addtoCartName" UseSubmitBehavior="false" />
please must reply thanks
Upvotes: 0
Views: 517
Reputation: 1025
You have TextBox inside ItemTemplate of ListItem not in Header/Footer etc. so you need to check Item Type of the Repeater Item. Try this :
foreach (RepeaterItem item in repeater1.Items)
{
if (item.ItemType == ListItemType.Item || item.ItemType == ListItemType.AlternatingItem)
{
TextBox txtName = (TextBox)item.FindControl("txtQuantity");
if (txtName!= null)
{
strText = strText + ", " + txtName.Text;
Response.Write("Text =" + strText);
}
}
}
Upvotes: 0
Reputation: 1319
I think looping through repeater control is causing the issue as you can't be sure it's accessing correct row's textbox control. You need to get textbox from the row from which the event is raised:
protected void repeater1_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName == "addtoCartName")
{
TextBox txtName = (TextBox)e.Item.FindControl("txtQuantity")
if (txtName!= null)
{
strText = strText + ", " + txtName.Text;
Response.Write("Text =" + strText);
}
}
}
Upvotes: 1