Retrieving value from Repeater in SelectIndexChanged event

M

Mike Robbins

In a repeater, for each row, I have a <td> with a Site Name, an invisible
Label Web Control with the Site ID, and a DropDownList that allows the user
to select a name of a person registered at the site.

In the SelectIndexChanged event for the DropDownList, I retrieve the
person's ID from the SelectedValue property. I also need to get the Site ID
from the Label but cannot figure out how to do it.

Any assistance is appreciated.

Mike
 
P

Phillip Williams

Hi Mike,

A label within a repeater translates to a span on the browser. Span tags do
not hold ViewState upon postback. You can replace the Label control within
your ItemTemplate with an HTMLInputHidden as follows:

<input type=hidden ID="lblSiteID" Runat="server"
value='<%#DataBinder.Eval(Container.DataItem, "CurrencyValue")%>'/>

In the eventhandler to the dropdownlist you can access that value as follows:
Ctype(CType(sender,
DropDownList).NamingContainer.FindControl("lblSiteID"),HTMLInputHIdden).value

The NamingContainer to the dropdownlist is the RepeaterItem that contains
the HTMLInputHidden control that has the value you were looking to retrieve.
 
M

Mike Robbins

Thanks, Phillip. I'll try that. I had been experimenting with something close
to that. Unfortunately, my OnSelectedIndexChanged events are suddenly not
being invoked. That happened to me a few days ago and I ended up having to
recreate the page from scratch as I couldn't figure out why. It's weird. If I
comment out the code behind functions for the OnSelectedIndexChanged events,
it complains that they aren't there, but it will not call them.

Mike
 
P

Phillip Williams

Make sure you do the repeater databinding in the Page_Init otherwise the
eventhandler would not fire up.
 
M

Mike Robbins

Thanks, Phillip.

That did fix the event firing. But I'm puzzled as to why it worked before
when I had the data binding in the Load event.

Mike
 
P

Phillip Williams

Hi Mike,

ASP Webcontrols are sensitive beings, if one does not observe the subtlety
and finesse of their page’s lifecycle they file for divorce.

The databinding for web controls placed declaratively on the page works in
Page_Load only and only if you check for the Page.IsPostBack condition before
you databind :
If Not Page.IsPostBack Then
repeater1.DataSource = CreateDataSource()
repeater1.DataBind()
End If

Otherwise place them in Page_Init to have the entire control created and its
events wired during the initialization phase.
 
M

Mike Robbins

Thanks, Phillip.

Everything seems to be working fine now. It's odd that it would work at
all when I had the binding in the Page_Load. It worked for about a day and a
half and then suddenly stopped working.

Appreciate your time.

Mike
 
M

Mike Robbins

That would explain it.

Thanks, Phillip.

Mike
Phillip Williams said:
Hi Mike,

ASP Webcontrols are sensitive beings, if one does not observe the subtlety
and finesse of their page's lifecycle they file for divorce.

The databinding for web controls placed declaratively on the page works in
Page_Load only and only if you check for the Page.IsPostBack condition before
you databind :
If Not Page.IsPostBack Then
repeater1.DataSource = CreateDataSource()
repeater1.DataBind()
End If

Otherwise place them in Page_Init to have the entire control created and its
events wired during the initialization phase.

--
HTH,
Phillip Williams
http://www.societopia.net
http://www.webswapp.com
 

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

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top