Solved

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

Posted on 2008-10-30
8
1,168 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
 
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Visual Studio editor? 1 38
Resolve Dependency Issues 4 47
Visual Studio TFS - how do I check in my code? 2 29
Get Client IP on RDS - VB.NET 15 29
Introduction HyperText Transfer Protocol (http://www.ietf.org/rfc/rfc2616.txt) or "HTTP" is the underpinning of internet communication.  As a teacher of web development I have heard many questions, mostly from my younger students who have come to t…
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now