Using AutoPostBack with a TextBox in a Repeater

  • Thread starter Nathan Sokalski
  • Start date
N

Nathan Sokalski

I have a Repeater who's ItemTemplate contains a TextBox that has
AutoPostBack set to True. However, I am not sure where to handle this
postback. When I have a Button in the ItemTemplate, I simply use the
Repeater's ItemCommand event handler, but this event handler is not
triggered for what would normally be the TextBox's TextChanged event. Can
someone help me figure out how to handle this postback? Any help would be
appreciated. Thanks.
 
P

Patrice

Note sure to understand the problem. As you said it should be textbox
TextChanged event... Have you tried to handle this event ? At which point
are you stuck ? (you don't know where to start or you tried something and it
doesn't work ?)

BTW, which programming language do you use ? (lloks you'll need a sample)
 
P

Phil Harvey

Hi Nathan,

A simple solution to this problem is to handle the TextChanged event on the
TextBox.

In your repeater, set the OnTextChanged property to an event handler method
in your code-behind.

<asp:Repeater ID="rptData" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" Text=<%#
Container.DataItem.ToString() %> OnTextChanged="TextChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>

In your code behind, have something like the following (I hope c# is ok).

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string[] data = new string[] { "s1", "s2" };
this.rptData.DataSource = data;
this.rptData.DataBind();
}
}

protected void TextChanged(object sender, EventArgs e)
{
TextBox textbox = sender as TextBox;
Debug.WriteLine("New text: " + textbox.Text);
}

Remember to only Databind the repeater if the request is not a postback,
otherwise the TextChanged won't be fired and the new value will be lost.

Regards,
 
N

Nathan Sokalski

I had thought of that, but I didn't think it would work, since the TextBox
was inside the Repeater and was not available in the list of controls in the
codebehind in Visual Studio. But I have tried it now, since I have been told
that it did work, and thanks to you, it did! Yes, I guess I do have to plead
guilty of thinking of something and not trying it, so I guess I've learned
my lesson. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Phil Harvey said:
Hi Nathan,

A simple solution to this problem is to handle the TextChanged event on
the
TextBox.

In your repeater, set the OnTextChanged property to an event handler
method
in your code-behind.

<asp:Repeater ID="rptData" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" Text=<%#
Container.DataItem.ToString() %> OnTextChanged="TextChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>

In your code behind, have something like the following (I hope c# is ok).

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string[] data = new string[] { "s1", "s2" };
this.rptData.DataSource = data;
this.rptData.DataBind();
}
}

protected void TextChanged(object sender, EventArgs e)
{
TextBox textbox = sender as TextBox;
Debug.WriteLine("New text: " + textbox.Text);
}

Remember to only Databind the repeater if the request is not a postback,
otherwise the TextChanged won't be fired and the new value will be lost.

Regards,

--
Phil Harvey
www.anotherblog.com



Nathan Sokalski said:
I have a Repeater who's ItemTemplate contains a TextBox that has
AutoPostBack set to True. However, I am not sure where to handle this
postback. When I have a Button in the ItemTemplate, I simply use the
Repeater's ItemCommand event handler, but this event handler is not
triggered for what would normally be the TextBox's TextChanged event. Can
someone help me figure out how to handle this postback? Any help would be
appreciated. Thanks.
 
E

Enis Hyseni

Nathan Sokalski did you ever solved the problem that you had, because I`m
stucked at that.

If you did please tell mehow can I do that?

Nathan Sokalski said:
I had thought of that, but I didn't think it would work, since the TextBox
was inside the Repeater and was not available in the list of controls in the
codebehind in Visual Studio. But I have tried it now, since I have been told
that it did work, and thanks to you, it did! Yes, I guess I do have to plead
guilty of thinking of something and not trying it, so I guess I've learned
my lesson. Thanks.
--
Nathan Sokalski
(e-mail address removed)
http://www.nathansokalski.com/

Phil Harvey said:
Hi Nathan,

A simple solution to this problem is to handle the TextChanged event on
the
TextBox.

In your repeater, set the OnTextChanged property to an event handler
method
in your code-behind.

<asp:Repeater ID="rptData" runat="server">
<ItemTemplate>
<asp:TextBox runat="server" Text=<%#
Container.DataItem.ToString() %> OnTextChanged="TextChanged"
AutoPostBack="true" />
</ItemTemplate>
</asp:Repeater>

In your code behind, have something like the following (I hope c# is ok).

protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
string[] data = new string[] { "s1", "s2" };
this.rptData.DataSource = data;
this.rptData.DataBind();
}
}

protected void TextChanged(object sender, EventArgs e)
{
TextBox textbox = sender as TextBox;
Debug.WriteLine("New text: " + textbox.Text);
}

Remember to only Databind the repeater if the request is not a postback,
otherwise the TextChanged won't be fired and the new value will be lost.

Regards,

--
Phil Harvey
www.anotherblog.com



Nathan Sokalski said:
I have a Repeater who's ItemTemplate contains a TextBox that has
AutoPostBack set to True. However, I am not sure where to handle this
postback. When I have a Button in the ItemTemplate, I simply use the
Repeater's ItemCommand event handler, but this event handler is not
triggered for what would normally be the TextBox's TextChanged event. Can
someone help me figure out how to handle this postback? Any help would be
appreciated. Thanks.
 
Joined
Aug 5, 2021
Messages
1
Reaction score
0
Dear All
I am facing a problem with textbox OnTextChange event in repeater.
i also set AutoPostBack as true for textbox and call event Textchange but page response only with postback not arrive at Ontextchange event

i also mention code in below :

<asp:Repeater ID="userCartdb" runat="server" OnItemDataBound="userCartdb_ItemDataBound" OnItemCommand="userCartdb_ItemCommand">
<HeaderTemplate>
<table class="table ps-table--shopping-cart ps-table--responsive"">
<thead>
<tr>

<th>Product name</th>
<th>PRICE</th>
<th>QUANTITY</th>
<th>TOTAL</th>
<th>Actions</th>
</tr>
</thead>
</HeaderTemplate>
<ItemTemplate>

<tbody>
<tr>
<td data-label="Product">
<div class="ps-product--cart">
<div class="ps-product__thumbnail">
<a href="product-default.html">
<asp:Image ID="img2" runat="server" ImageUrl='<%#Eval("image") %>' /></a>
</div>
<asp:Label ID="lblid" runat="server" Text='<%# Bind("PRO_ID") %>' Visible="false"></asp:Label>
<div class="ps-product__content"> <asp:Label ID="lblName" runat="server" Text='<%#Eval("ITEM_NAME")%>'></asp:Label>
</div>
</div>
</td>

<td class="price" data-label="Price">
<asp:Label ID="lblprice" runat="server" Text='<%#Eval("AMOUNT")%>'></asp:Label></td>
<td data-label="Quantity">

<asp:TextBox ID="txtCartProductQuantity" runat="server" CssClass="form-control" Width="100" textmode="Number" Text='<%#DataBinder.Eval(Container.DataItem,"QTY").ToString()%>'></asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtCartProductQuantity" ErrorMessage="Required" Display="Dynamic" CssClass="errormsg" Enabled="false"></asp:RequiredFieldValidator>
<asp:RegularExpressionValidator ID="RegularExpressionValidator2" runat="server" ControlToValidate="txtCartProductQuantity" ErrorMessage="Error" ValidationExpression="^\d*\.{0,1}\d+$" Display="Dynamic" SetFocusOnError="True" CssClass="errormsg"></asp:RegularExpressionValidator>
<%--<asp:TextBox ID="txtCartProductQuantity" runat="server" CssClass="form-control" TextMode="Number" style="width:100%;" Text='<%# Eval("ProductQty")%>'></asp:TextBox>--%>
</td>
<td data-label="Total">
<asp:Label ID="lblTotal" runat="server"></asp:Label></td>
<td data-label="Actions">
<asp:LinkButton ID="lnkMyCartDelete" CssClass="icon-cross" CommandName="lnkMyCartDelete" runat="server"></asp:LinkButton></td>
</tr>
</tbody>
</ItemTemplate>
<FooterTemplate>
</table>

</FooterTemplate>
</asp:Repeater>


code behind is:

if (!IsPostBack)
{
rpProduct.DataSource = dt1;
rpProduct.DataBind();

}

protected void txtCartProductQuantity_TextChanged(object sender, EventArgs e)
{
TextBox textBox = sender as TextBox;
if (textBox != null)
{
string theText = textBox.Text;
var item = (RepeaterItem)textBox.NamingContainer;
if (item != null)
{
TextBox txtQTY = (TextBox)item.FindControl("txtCartProductQuantity");
if (txtQTY != null)
{
txtQTY.Text = theText;
}

Label lblprice = (Label)item.FindControl("lblprice");
if (lblprice != null)
{
lblprice.Text = (Convert.ToDecimal(txtQTY.Text)).ToString();
}

Label lblTotal = (Label)item.FindControl("lblTotal");

lblTotal.Text = ((Convert.ToDecimal(lblprice.Text) * (Convert.ToInt32(txtQTY.Text)))).ToString();

}
}
}

in my application My txtCartProductQuantity_TextChanged(object sender, EventArgs e) not called. please help me for the same
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top