I have ajaxified RadGrid.
In one of the columns I am dynamically creating ImageButtons, like here:
In one of the columns I am dynamically creating ImageButtons, like here:
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
DataRowView v = (DataRowView)e.Item.DataItem; |
ImageButton lnk = new ImageButton(); |
lnk.ImageUrl = "~/Images/view.gif"; |
lnk.ToolTip = GetString("TimeSheet"); |
lnk.CommandArgument = "test"; |
lnk.CommandName = "Redirect"; |
lnk.Command += new CommandEventHandler(lnk_Command); |
GridDataItem tem = e.Item as GridDataItem; |
Item["Column"].Controls.AddAt(0, lnk); |
} |
} |
protected void lnk_Command(object sender, CommandEventArgs e) |
{ |
Session["Redirect"] = e.CommandArgument; |
Response.Redirect("Timesheet.aspx"); |
} |
When page is loaded first time ImageButton is successfully created and displayed in each row,
but clicking on it doesn't fire lnk_Command event (I have checked this adding breakpoints in lnk_Command),
Instead of redirecting I get the same page without ImageButtons displayed.
Please advice.
10 Answers, 1 is accepted
0

Shinu
Top achievements
Rank 2
answered on 26 Feb 2010, 12:43 PM
Hi,
A better approach is adding control in ItemCreated event than ItemDataBound.
aspx:
c#:
The documentation describes the differences between ItemCreated and ItemDataBound.
Distinguishing the major differences between ItemCreated and ItemDataBound events
-Shinu.
A better approach is adding control in ItemCreated event than ItemDataBound.
aspx:
<telerik:GridTemplateColumn UniqueName="Column"> |
<ItemTemplate> |
</ItemTemplate> |
</telerik:GridTemplateColumn> |
c#:
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridDataItem) |
{ |
GridDataItem Item = (GridDataItem)e.Item; |
ImageButton lnk = new ImageButton(); |
lnk.ImageUrl = "~/Images/img1.gif"; |
lnk.CommandArgument = "test"; |
lnk.CommandName = "Redirect"; |
Item["Column"].Controls.Add(lnk); |
} |
} |
The documentation describes the differences between ItemCreated and ItemDataBound.
Distinguishing the major differences between ItemCreated and ItemDataBound events
-Shinu.
0
Hello Igor,
Please examine the following thread:
Solution: Dynamic Link buttons created using ItemCreated not firing ItemCommand
Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Please examine the following thread:
Solution: Dynamic Link buttons created using ItemCreated not firing ItemCommand
Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0

spt
Top achievements
Rank 1
answered on 26 Feb 2010, 02:27 PM
Unfortunatelly that doesn't help.
I forgot to explain that I create button in GridGroupHeader row, not in simple DataItem
I forgot to explain that I create button in GridGroupHeader row, not in simple DataItem
protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
{ |
if (e.Item is GridGroupHeaderItem) |
{ |
DataRowView v = (DataRowView)e.Item.DataItem; |
ImageButton lnk = new ImageButton(); |
lnk.ImageUrl = "~/Images/view.gif"; |
lnk.ToolTip = GetString("TimeSheet"); |
lnk.CommandArgument = "test"; |
lnk.CommandName = "Redirect"; |
lnk.Command += new CommandEventHandler(lnk_Command); |
GridDataItem tem = e.Item as GridDataItem; |
Item["Column"].Controls.AddAt(0, lnk); |
} |
} |
protected void lnk_Command(object sender, CommandEventArgs e) |
{ |
Session["Redirect"] = e.CommandArgument; |
Response.Redirect("Timesheet.aspx"); |
} |
0

Adam Heeg
Top achievements
Rank 1
answered on 26 Feb 2010, 07:00 PM
The event wont fire until after the page is posted back. You have to specify a unique ID for your button when you create, and you have to recreate it in the OnInit event. The OnInit event will then be able to create the controls before the page loads the viewstate. This means the controls exist allowing viewstate to put the correct data and fire the click event after the pageload event.
So, your buttons are not firing because they no longer exist after the controls are reloaded.
So, your buttons are not firing because they no longer exist after the controls are reloaded.
0

spt
Top achievements
Rank 1
answered on 01 Mar 2010, 10:28 AM
Can you provide a simple example?
0

Adam Heeg
Top achievements
Rank 1
answered on 01 Mar 2010, 02:55 PM
Sorry, I don't have the time to post code for you. It isn't too hard, just google something like 'maintaining state in dynamic controls in asp.net' or something. There are lots of examples and explainations available. You will also need to get a handle on the order of events and what they do in asp.net. There are very nice and simple flow charts available online for that as well.
Good luck
Good luck
0
Hello Igor,
Please download the attached sample project. It is based on the forum thread I posted below.
Let me know if you need further assistance.
Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
Please download the attached sample project. It is based on the forum thread I posted below.
protected
void
RadGrid1_ItemCreated(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridGroupHeaderItem)
{
GridGroupHeaderItem item = e.Item
as
GridGroupHeaderItem;
ImageButton lnk =
new
ImageButton();
lnk.ID =
"myBtn"
+ e.Item.ItemIndex;
lnk.CommandName =
"Redirect"
;
lnk.Command +=
new
CommandEventHandler(lnk_Command);
item.DataCell.Controls.Add(lnk);
}
}
void
lnk_Command(
object
sender, CommandEventArgs e)
{
RadAjaxPanel1.Alert(
"Command name: "
+ e.CommandName);
}
protected
void
RadGrid1_ItemDataBound(
object
sender, GridItemEventArgs e)
{
if
(e.Item
is
GridGroupHeaderItem)
{
GridGroupHeaderItem item = e.Item
as
GridGroupHeaderItem;
ImageButton lnk =
new
ImageButton();
lnk.ID =
"myBtn"
+ e.Item.ItemIndex;
item.DataCell.Controls.Add(lnk);
lnk.ImageUrl =
"~/btn.png"
;
lnk.CommandName =
"Redirect"
;
lnk.Command +=
new
CommandEventHandler(lnk_Command);
}
}
Let me know if you need further assistance.
Regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0

spt
Top achievements
Rank 1
answered on 02 Mar 2010, 12:38 PM
Thanks for example, it works.
But I have the following problem now.
I want to pass a parameter with data from the GridGroupHeader row.
So I planned to have a code something like this:
This works in ItemDataBound event, but in ItemCreated I get an exception in the following string:
But I have the following problem now.
I want to pass a parameter with data from the GridGroupHeader row.
So I planned to have a code something like this:
<GroupByExpressions> |
<telerik:GridGroupByExpression > |
<SelectFields > |
<telerik:GridGroupByField FieldName="StartDate" HeaderText=" " HeaderValueSeparator="" FormatString="{0:D}" /> |
</SelectFields> |
<GroupByFields> |
<telerik:GridGroupByField FieldName="StartDate" SortOrder="Ascending" /> |
</GroupByFields> |
</telerik:GridGroupByExpression> |
</GroupByExpressions> |
.............. |
if (e.Item is GridGroupHeaderItem) |
{ |
GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; |
ImageButton lnk = new ImageButton(); |
lnk.ID = "myBtn" + e.Item.ItemIndex; |
lnk.CommandName = "Redirect"; |
DataRowView v = (DataRowView)e.Item.DataItem; |
DateTime groupDate = Convert.ToDateTime(v["StartDate"]); |
lnk.CommandArgument = groupDate.ToString("d"); |
lnk.Command += new CommandEventHandler(lnk_Command); |
item.DataCell.Controls.Add(lnk); |
} |
This works in ItemDataBound event, but in ItemCreated I get an exception in the following string:
DateTime groupDate = Convert.ToDateTime(v["StartDate"]); |
So, I cannot specify a CommandArgument for Button. Please advice.
0
Accepted
Hello Igor,
You should create the control on ItemCreated and then populate it on ItemDataBound.
Please find the modified version of my previous sample project, attached to this post.
Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
You should create the control on ItemCreated and then populate it on ItemDataBound.
Please find the modified version of my previous sample project, attached to this post.
Best regards,
Daniel
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items.
0

spt
Top achievements
Rank 1
answered on 05 Mar 2010, 03:39 PM
Thank you it works as I want now.
Btw, I also tried
protected void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) | ||||||||||||||||
{ | ||||||||||||||||
if (e.Item is GridGroupHeaderItem) | ||||||||||||||||
{ | ||||||||||||||||
GridGroupHeaderItem item = e.Item as GridGroupHeaderItem; | ||||||||||||||||
ImageButton lnk = new ImageButton(); | ||||||||||||||||
lnk.ID = "myBtn" + e.Item.ItemIndex; | ||||||||||||||||
lnk.ImageUrl = "~/btn.png"; | ||||||||||||||||
lnk.CommandName = "Redirect"; | ||||||||||||||||
lnk.CommandArgument = DataBinder.Eval(e.Item.DataItem, "CategoryName").ToString(); | ||||||||||||||||
lnk.Command += new CommandEventHandler(lnk_Command); | ||||||||||||||||
item.DataCell.Controls.Add(lnk); | ||||||||||||||||
} | ||||||||||||||||
} | ||||||||||||||||
instead of your
Can you explain thios behaviour? |