Accessing a Control inside a Repeater from a code behind file

R

Ric

im new to asp.net. please help if u can.
is it possible to refer to a control(ie lable, placeholder, textbox)
that is inside a repeater object from a code behind file? when i place
the control object outside of the repeater, i can refer to it from the
code behind file. when i place the control object inside the repeater,
i get a 'need to instanciate the control object' error. if i declare
the control object inside the control behind file, the error goes
away, but i cant refer to the control objects components. please help.
 
R

Ric

thx again for the help martin. i was able to use your suggestion to
get to the control within the repeater. also, i was able to understand
a bit more bout objects, events and arguments. but, im sure i have a
lot to more to learn.

if u or someone else can help me with another question, please do.

i have a repeater pulling job information from a database. each
repeater itemtemplate has with a linkbutton and a row with a label
(for now).

<asp:repeater ID="EmployeeInfo" OnItemCommand="getActivityList"
runat="server">

<itemtemplate>
<tr bgcolor="#CCCCCC">
<td><%# Container.DataItem(0)%></td>
<td><%# Container.DataItem(1)%></td>
<td><%# Container.DataItem(2)%></td>
<td><%# Container.DataItem(3)%></td>
<td><%# Container.DataItem(4)%></td>
<td><asp:linkbutton Text=<%# Container.DataItem(5)%>
runat="server"/></td>
</tr>
<tr>
<td><asp:label id="litLabel" Runat="Server" Text="labelti"
Visible="false"/></td>
</tr>
</itemtemplate>

when u click on the linkbutton, the label(eventually it will be a
placeholder with more database info in another repeater) will become
visible with detailed info bout the job.

the first Container.DataItem(0) has an ID number that I need to
extract and send to the stored procedure. how can i extract that
dataItem. ive been able to loop throw the items in the repeater item
and extract the entire row. i tried to convert that info to a
DataBoundLiteralControl and pull the text from it. but when i tried to
use string functions to pull the ID number or even get a length of
string i could not consistently get what i needed. i figured out that
row is coded with <html> table and row tags. thus, im back to trying
to get the data from the first container.dataitem. so, to make a long
request even longer, what object or class do i use to pull individual
dataitems from an item. i looked at the dataitem property from
repeateritem class, but i cant get it to work. again, thx for the
help.
 
M

Martin Dechev

Hi, Ric,

You can pass this value in the CommandName or CommandArgument property:
<asp:repeater ID="EmployeeInfo" OnItemCommand="getActivityList"
runat="server">

<itemtemplate>
<tr bgcolor="#CCCCCC">
<td><%# Container.DataItem(0)%></td>
<td><%# Container.DataItem(1)%></td>
<td><%# Container.DataItem(2)%></td>
<td><%# Container.DataItem(3)%></td>
<td><%# Container.DataItem(4)%></td>
<td><asp:linkbutton Text='<%# Container.DataItem(5)%>'

CommandName="ShowDetails"
CommandArgument=' said:
runat="server"/></td>
</tr>
<tr>
<td><asp:label id="litLabel" Runat="Server" Text="labelti"
Visible="false"/></td>
</tr>
</itemtemplate>

If the ID is an integer you will have to parse it:

[C#]
protected void getActivityList(object s, RepeaterCommandEventArgs e)
{
if(e.CommandName == "ShowDetails")
{
int ID = int.Parse(e.CommandArgument);
//....
}
}

[VB.NET]
Protected Sub getActivityList(s As Object, e As RepeaterCommandEventArgs)
If e.CommandName = "ShowDetails" Then
Dim ID As Int32 = Int32.Parse(e.CommandArgument)
'....
End If
End Sub

Hope this helps
Martin
 
R

ric carrasquilla

thx again for the help martin. just curiously, if u had to send more
than one command arguement how would u do that? i used a commandName and
commandArgument and wondered what would u do to send two
commandArguments with one commandName or two commandNames each with a
commandArgument.

thx again for the push in the right direction.
 
M

Martin Dechev

Hi, ric carrasquilla,

just curiously, if u had to send more
than one command arguement how would u do that?

You normally don't need to do that. The common design is to identify your
objects with a single identifier. It is possible to pass 2 parameters
anyway - one for the action and another for the object identifier. This is
how it was designed, this is how it works and I believe it is enough for any
case.

Greetings
Martin
 
R

ric carrasquilla

thx again martin. i really appreciate the time and thoroughness of your
answers. have a good day.

ric
 
M

Monika Mesik

I'd like to know which site helped to answer the question. I have a similar
issue. Can you provide the link?

TIA
 

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

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top