Identifying object in ItemTemplate and event not firing
-
24-06-2021 - |
Pregunta
I am using a RadListView control with an ItemTemplate that contains a button as shown here:
<ItemTemplate>
<tr class="rlvI">
//more TD elements here
<td>
<telerik:RadButton ID="ENABLEDToggle" runat="server" Width="75" ButtonType="StandardButton" AutoPostBack="true"
ToggleType="CustomToggle" Checked='<%# Enabled_Converter(Eval("ENABLED")) %>' OnCheckedChanged="TaskStateChange_Clicked">
<ToggleStates>
<telerik:RadButtonToggleState Text="Enabled" />
<telerik:RadButtonToggleState Text="Disabled" />
</ToggleStates>
</telerik:RadButton>
</td>
</tr>
</ItemTemplate>
My first question is why is it that when my button is pressed, I do not enter my TaskStateChange_Clicked event handler? It is as if the event is never fired.
Second, whenever a button is clicked, how do I get the object associated with that row?
Solución
As for the first question, check how you are binding your RadListView
. Problems like this usually appear if controls are bound with data on every postback. So if you have something like
void Page_Load(object sender, EventArgs e)
{
...
RadListView1.DataSource = dataSource;
RadListView1.DataBind();
...
}
replace it with
void Page_Load(object sender, EventArgs e)
{
...
if (!this.IsPostBack)
{
RadListView1.DataSource = dataSource;
RadListView1.DataBind();
}
...
}
Update from comments. Another reason might be that the type of your button is StandardButton
while spec implies that event CheckedChanged
is fired only when button type is ToggleButton
.
As for the second question consider using RadListView's ItemCommand
event. This way you can make use of CommandArgument
property of RadButton
and pass any info you want in it, say object's ID.