How to specify attributes from code behind

K

K Viltersten

I have a repeater with an inner repeater and
i'd like to specify an OnItemCommand event
handler but instead of doing it from the ASPX
code, i prefer to shove it into the code
behind. How do i do that?

The code is as follows.
<asp:Repeater ID="Outer" runat="server">
<ItemTemplate>
<asp:Literal ID="Info" runat="server" />
<asp:Repeater ID="Inner" runat="server"
OnItemCommand="Inner_Command">
<ItemTemplate>
 
G

Gregory A. Beamer

I have a repeater with an inner repeater and
i'd like to specify an OnItemCommand event
handler but instead of doing it from the ASPX
code, i prefer to shove it into the code
behind. How do i do that?

The code is as follows.
<asp:Repeater ID="Outer" runat="server">
<ItemTemplate>
<asp:Literal ID="Info" runat="server" />
<asp:Repeater ID="Inner" runat="server"
OnItemCommand="Inner_Command">
<ItemTemplate>


Bind the event handler to the event using the delegate. As an example,
you can add the following to the Page_Load (or other events) to wire a
button up:

Button1.Click += new EventHandler(Button1_Click);

In fact, since delegates can be "multicastt", you can even do this:

protected void Page_Load(object sender, EventArgs e)
{
Button1.Click += new EventHandler(Button1_Click);
Button1.Click += new EventHandler(Button1_Click2);
}
protected void Button1_Click(object sender, EventArgs e)
{
Response.Write("You clicked the button<br/>");
}
protected void Button1_Click2(object sender, EventArgs e)
{
Response.Write("and this second event also fired<br/>");
}

I would not recommend this on event handlers necessarily, but it will
work as long as you can set the signature the same for both events.
 
K

K Viltersten

I have a repeater with an inner repeater and
Bind the event handler to the event using the
delegate. As an example, you can add the
following to the Page_Load (or other events)
to wire a button up:

Button1.Click += new EventHandler(Button1_Click);

I'm not sure it'll work in my case. Generally,
i agere with you and that's how i'd do it.
However, in this particular case, the event
will be fired by one of the controls created
INSIDE the inner repeater, so binding an event
to it (them?) doesn't make much sense...

Thoughts?
 
G

Gregory A. Beamer

I'm not sure it'll work in my case. Generally,
i agere with you and that's how i'd do it.
However, in this particular case, the event
will be fired by one of the controls created
INSIDE the inner repeater, so binding an event
to it (them?) doesn't make much sense...

Thoughts?

As long as the control is dynamically bound before you try to bind the
event, it should work. This may mean altering the way you are binding to
the controls in the repeaters, or putting the event binding somewhere
late in the page event model, but as long as the control is already
bound to the page, you should not have an issue binding the event
handler.

Now, if you want to bind a control in each repeat, you will have to go
to the data binding event(s) for the repeater and attempt to bind the
event handler for the control there. In fact, you may be best
dynamically creating the controls and then binding to the delegate,
although this is more a maintainability thing than a necessity.

I have no time to experiment for the next month or so. Otherwise, i
would play with this, as it sounds interesting.

Peace and Grace,
 
K

K Viltersten

I have a repeater with an inner repeater and
Now, if you want to bind a control in each
repeat, you will have to go to the data binding
event(s) for the repeater and attempt to bind the
event handler for the control there.

Are you suggesting something along the lines of this?

private void RepeaterInner_ItemDataBound(
Object sender, RepeaterItemEventArgs eventArgs)
{
eventArgs.Item.FindControl("Button1") +=
WhateverMethodNeeded;
}
 
G

Gregory A. Beamer

Are you suggesting something along the lines of this?

private void RepeaterInner_ItemDataBound(
Object sender, RepeaterItemEventArgs eventArgs)
{
eventArgs.Item.FindControl("Button1") +=
WhateverMethodNeeded;
}

Somewhat, but also inserting the button at that time to guarantee you
know the item name and it is inserted. I have not tested the idea, but
it should work.
 

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,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top