Connecting Tech Pros Worldwide Help | Site Map

Accessing controls defined inside DataRepeater

xandercoded's Avatar
Newbie
 
Join Date: Sep 2008
Location: South Florida
Posts: 4
#1: 4 Weeks Ago
I have a series of controls 4 controls. 3 of which I need to disable onPageLoad not on PostBack.

Basically I need to cast the "object sender" onPageLoad and drill down to the the controls and set the

controlId.Visible = False;

I have tried many alternatives and I can't seem to figure out a way to disable them. I would use Javascript. Though that would introduce other facets that I don't want to deal with at the moment, as time is of the essence...

I want to do something similar to this, I do this on a btnAddToCart_Click Event

Expand|Select|Wrap|Line Numbers
  1.   protected void AddToCart_Click(object sender, CommandEventArgs e) {
  2.  
  3.         //some other code...
  4.  
  5.  
  6.         LinkButton ctrl = (LinkButton)sender;
  7.         RepeaterItem rpItem = ctrl.NamingContainer as RepeaterItem;
  8.         if (rpItem != null) {
  9.             LinkButton btn = (LinkButton)rpItem.FindControl("btnRemoveFromCart");
  10.             btn.Visible = true;
  11.             btn = (LinkButton)rpItem.FindControl("btnAddToCart");
  12.             btn.Visible = false;
  13.             Image img = (Image)rpItem.FindControl("imgAdded");
  14.             img.Visible = true;
  15.         }
  16.  
  17.  
I would appreciate if someone would advise me on how to cast the sender into an object that I can work with and that has the references to the controls on the page that need to be altered (changing the img.Visibility = false)
best answer - posted by xandercoded
this is the solution I came up with...

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.         string galleryID = Session["selectedGalleryID"].ToString();
  3.  
  4.         if (!IsPostBack) {
  5.             h1GalleryTitle.InnerText = Session["selectedGalleryName"].ToString();
  6.  
  7.             #region bind and set default cart button state
  8.             getItems();
  9.  
  10.             LinkButton btn;
  11.             Image img;
  12.             RepeaterItem item;
  13.  
  14.             ControlCollection ctrls = repeaterGallery.Controls;
  15.             foreach (Control i in ctrls) {
  16.                 item = i as RepeaterItem;
  17.                 btn = item.FindControl("btnRemoveFromCart") as LinkButton;
  18.                 btn.Visible = false;
  19.                 item = i as RepeaterItem;
  20.                 btn = item.FindControl("btnAddToCart") as LinkButton;
  21.                 btn.Visible = true;
  22.                 item = i as RepeaterItem;
  23.                 img = item.FindControl("imgAdded") as Image;
  24.                 img.Visible = false;
  25.             }
  26.             #endregion
  27.         }
  28.     }
  29.  
jhardman's Avatar
Moderator
 
Join Date: Jan 2007
Location: logan, utah
Posts: 2,690
#2: 4 Weeks Ago

re: Accessing controls defined inside DataRepeater


You accidentally posted in the classic ASP forum, and I am pretty certain you meant to post in the ASP.NET forum. I'll move you over, hopefully you will get some answers there.

Jared
xandercoded's Avatar
Newbie
 
Join Date: Sep 2008
Location: South Florida
Posts: 4
#3: 4 Weeks Ago

re: Accessing controls defined inside DataRepeater


Appreciate the help jhardman
xandercoded's Avatar
Newbie
 
Join Date: Sep 2008
Location: South Florida
Posts: 4
#4: 4 Weeks Ago

re: Accessing controls defined inside DataRepeater


this is the solution I came up with...

Expand|Select|Wrap|Line Numbers
  1. protected void Page_Load(object sender, EventArgs e) {
  2.         string galleryID = Session["selectedGalleryID"].ToString();
  3.  
  4.         if (!IsPostBack) {
  5.             h1GalleryTitle.InnerText = Session["selectedGalleryName"].ToString();
  6.  
  7.             #region bind and set default cart button state
  8.             getItems();
  9.  
  10.             LinkButton btn;
  11.             Image img;
  12.             RepeaterItem item;
  13.  
  14.             ControlCollection ctrls = repeaterGallery.Controls;
  15.             foreach (Control i in ctrls) {
  16.                 item = i as RepeaterItem;
  17.                 btn = item.FindControl("btnRemoveFromCart") as LinkButton;
  18.                 btn.Visible = false;
  19.                 item = i as RepeaterItem;
  20.                 btn = item.FindControl("btnAddToCart") as LinkButton;
  21.                 btn.Visible = true;
  22.                 item = i as RepeaterItem;
  23.                 img = item.FindControl("imgAdded") as Image;
  24.                 img.Visible = false;
  25.             }
  26.             #endregion
  27.         }
  28.     }
  29.  
Reply

Tags
accessing, controls, datarepeater, namingcontainer