Solved

When Accessing repeater items of repeater in AJAX Updatepanel, it returns wrong values after update

Posted on 2008-10-30
8
1,170 Views
Last Modified: 2013-12-17
Hi,

I've got a repeater control within an AJAX update panel. The repeater shows product info and a textbox where the user can insert a quantity to order.

Because there are too many products to show them all, I only show products starting with 'A', and above the repeater links with 'B', 'C', etc. If the user clicks the 'B'-link, the repeater is newly databound with products starting with 'B'. As the repeater is in an updatepanel, this happens without postback as wanted.

This works fine and the Repeater shows the right products. The problem is, that if the user clicks the submit button on the page, i loop through the repeateritems. BUT: the repeater items contain values from the page that was originally loaded; lblID still contains the IDs from the 'A' products instead of the correct ID for the 'B'-products.

What am i doing wrong? How can i access the current values of the repeater items?


 In simplified form, the repeater looks like this:
<asp:Repeater ID="rptProducts" runat="server">
 <ItemTemplate>
  <tr>
   <td><asp:Label ID="lblID" runat="server" Visible="false"></asp:Label></td>
   <td><asp:TextBox ID="txtQuantity" Columns="1"  runat="server" EnableViewState="false"></asp:TextBox></td>
  </tr>
 </ItemTemplate>
</asp:Repeater>

In the button click handler, i'm looping like this:
foreach (RepeaterItem rpi in rptProducts.Items)
{
 TextBox txtQuantity = (TextBox)rpi.FindControl("txtQuantity");                
 Label lblID = (Label)rpi.FindControl("lblID");
}

Thanks!
0
Comment
Question by:dready
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:techExtreme
ID: 22842088
Hello,
first a quick fix:
If your whole repeater is in an update panel and your links are not, put them into one also.

Also, if your update panel is not updated automatically, i.e. its updatemode is 'Conditional', make sure you update it on each link click i.e. either user clicks a or b or c... your update your panel.
Basically the moment your repeater binds, update your update panel.
Also, put your submit button in update panel as well.


Now if this isn't the case,
I'm arranging your controls here, using three udpate panels (All having updatemode set to always)


<updatepanel.... id"upanel1"...
  your 'A','B','C'.. links here
</updatepanel


<updatepanel.... id"upanel2"...
  your repeater control here
</updatepanel


<updatepanel.... id"upanel1"...
  your submit button here
</updatepanel

Plz note if you go by this solution , i.e. updatemode set to always, you need not call the .Update method of the updatepanel , which otherwise you must call all time if conditional mode is et.
Hopefully this will be your solution.
Let me know if any doubt/issue.

Happy Coding!
0
 
LVL 11

Author Comment

by:dready
ID: 22842242
Hi TechExtreme,

Thanks for your reply. I've been playing around with the updatepanels quite a bit. My current situation is like this:
<updatepanel id="updatepanel1"..
my 'A', 'B', links
my repeater control
my button
</updatepanel

The update panel is not set to conditional mode. I do not call update() anywhere.

So basically the whole page is in an update panel now. I realise this sort of makes the partial update function of the UpdatePanel useless, but i'd first like to get this to work and at least the browserhistory doesn't get full when clicking a lot of different letter-links.
Like I said before: all the updates seem to take place and the right products are shown. Only when accessing the values of the repeater items in the click handler it shows the old values.

Anything set up wrongly like this?
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22842313
Hi,
this is even better, you have everything in one place.

If you are able to see the new values in the page which is rendered,
you should get them in the backend in the server side code.

However to be more sure, can you paste the backend code event handlers here
so that i can have a look at how you are fetching stuff and why old values
are persisted?
0
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 
LVL 11

Author Comment

by:dready
ID: 22842459
Here the simplified version of my code behind
* Repeater is page variable:
protected System.Web.UI.WebControls.Repeater rptProducts;

* Letter-link Click handler:
private void lbB_Click(object sender, EventArgs e)
{
            rptProducts.DataSource = BLL.PricelistManager.GetPriceList("b");
            rptProducts.DataBind();            
}

Databind
private void rptProducts_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
switch (e.Item.ItemType)
{
    case ListItemType.AlternatingItem:
    case ListItemType.Item:
        ProductEntity pe = (ProductEntity)e.Item.DataItem;
        Label lblID = (Label)e.Item.FindControl("lblID");
        lblID.Text = pe.ID.ToString();
        ... etc
        break;
    default:
        break;
}
}

Submit click:
private void btnSubmit_Click(object sender, ImageClickEventArgs e)
{  
    foreach (RepeaterItem rpi in rptProducts.Items)
   {
    TextBox txtQuantity = (TextBox)rpi.FindControl("txtQuantity");                
    Label lblCode = (Label)rpi.FindControl("lblCode"); //here it contains wrong value
    }  
}
0
 
LVL 12

Accepted Solution

by:
techExtreme earned 500 total points
ID: 22842530
I hope the lblCode what you are referring in the submit button click is
set properly in the itemdatabound of the repeater as I can only see
lblID being bound.

Otherthan that, your code seems clean. However I have one doublt, did you
dragged and dropped the repeater on your page or it is dynamically added to
the page?
0
 
LVL 11

Author Comment

by:dready
ID: 22842589
sorry, the lblCode is actually lblID. For making the code easier to understand, i renamed the original name of lblCode to lblID, but just forgot to do that in the submit button click.

I didn't drag and drop the repeater on the page.
I just typed in <repeater.... > lots of typing</repeater> in the aspx page, and added a page variable to the code behind manually, and in InitializeComponent i added

this.rptProducts.ItemDataBound += new RepeaterItemEventHandler(rptProducts_ItemDataBound);

and created the handler.

Anything forgotten there maybe?
0
 
LVL 11

Author Comment

by:dready
ID: 22842820
Hey TechExtreme!

I found the problem!!!! After you said all looks OK, i started searching for a cause away from the repeater and updatepanel issue; Now, the page I used has a different way of handling the viewstate; it doesn't derive from page class, but some custom class that persists viewstate to a file on the server. I just tried what would happen if i derive this page from Page again, and then it worked!!!!! So the issue lies in the 'persistViewstateToFile' class and not in the UpdatePanel;

Thanks so much for narrowing down the problem for me. Full points more then deserved!!!!!!!
0
 
LVL 12

Expert Comment

by:techExtreme
ID: 22843374
Hia,
Thanks. Yes persisting viewstate in a file does reduce some overhead as the page becomes light, but you never know if you intruduce other bugs because of it.
Happy Coding!
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question