I'm using a grid and i would like to have a button that when the user clicks on it binds my CheckBoxList that i have later on the page. I placed an image button in a template field but i'm guessing i need to force a postback so on the page reload the CheckBoxList can get bound to the selected item in the grid.
Does anyone know how to do this? I was thinking that maybe i need to go in the code behind and on the grid item create event register my image button with the ajax manager?
I don't have any emotional ties to the template field and the image button so if there is a better way to bind the selected grid item to my CheckBoxList , I'm open to suggestions.
--Mike
Does anyone know how to do this? I was thinking that maybe i need to go in the code behind and on the grid item create event register my image button with the ajax manager?
I don't have any emotional ties to the template field and the image button so if there is a better way to bind the selected grid item to my CheckBoxList , I'm open to suggestions.
--Mike
3 Answers, 1 is accepted
0
Hello Mike,
GridTemplateColumn with an ImageButton would work for you, but you can also use a GridButtonColumn that saves you some markup and does the job. You will need to set a custom command name for your button, and in the ItemCommand event handler of RadGrid, implement your custom code under this command name. Something like the following:
If you need to specify certain command arguments for each data item from RadGrid, you can do this in the ItemDataBound event handler in the following way:
This piece of code sets the ItemIndex value of the current item as the CommandArgument of the respective button control in the GridButtonColumn.
As for updating controls outside of Ajaxified RadGrid, you will need to register all of these controls with the Ajax manager, so that updating these controls from inside RadGrid can be accomplished using Ajax callbacks.
Best regards,
Veli
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
GridTemplateColumn with an ImageButton would work for you, but you can also use a GridButtonColumn that saves you some markup and does the job. You will need to set a custom command name for your button, and in the ItemCommand event handler of RadGrid, implement your custom code under this command name. Something like the following:
| void RadGrid1_ItemCommand(object source, GridCommandEventArgs e) |
| { |
| if (e.CommandName == "MyCustomCommand") |
| { |
| //..your custom code |
| } |
| } |
If you need to specify certain command arguments for each data item from RadGrid, you can do this in the ItemDataBound event handler in the following way:
| void RadGrid1_ItemDataBound(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridDataItem) |
| { |
| GridDataItem item = (GridDataItem)e.Item; |
| (item["ButtonColumn"].Controls[0] as Button).CommandArgument = item.ItemIndex; |
| } |
| } |
This piece of code sets the ItemIndex value of the current item as the CommandArgument of the respective button control in the GridButtonColumn.
As for updating controls outside of Ajaxified RadGrid, you will need to register all of these controls with the Ajax manager, so that updating these controls from inside RadGrid can be accomplished using Ajax callbacks.
Best regards,
Veli
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
0
The KID
Top achievements
Rank 2
answered on 09 Jun 2008, 04:16 PM
Thank you for the excellent example. I think I almost got it working up
to the part of registering the control with the script manger. This is
what I used to try to register the control but it throws an error
because the button is null?
Here is how i declare the button that i'm working with:
So I'm attempting to bind the ID to button so i can use it on the item command like you instructed i could do previously
Any ideas as to why my button is null? Would it be because in the code i told it to look for an ImageButton and the grid uses it's own special image button?
| protected void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridDataItem) |
| { |
| //Get the properties of the button |
| ImageButton btn = (ImageButton)e.Item.FindControl("MyButton"); |
| //Get the current script manager from the page |
| ScriptManager SM = ScriptManager.GetCurrent(Page); |
| //Register the control with the scriptmanager to preform the post back |
| SM.RegisterPostBackControl(btn); |
| } |
| } |
Here is how i declare the button that i'm working with:
| <telerik:GridButtonColumn |
| UniqueName="MyButton" |
| HeaderText="My Button" |
| ButtonType="ImageButton" |
| ImageUrl="~/Images/Icons/house_go.gif" |
| CommandName="StoredProcedureId" |
| CommandArgument="StoredProcedureId"> |
| <telerik:GridButtonColumn>; |
So I'm attempting to bind the ID to button so i can use it on the item command like you instructed i could do previously
| protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e) |
| { |
| if (e.CommandName == "StoredProcedureId") |
| { |
| //My logic here .. |
| } |
| } |
Any ideas as to why my button is null? Would it be because in the code i told it to look for an ImageButton and the grid uses it's own special image button?
0
Accepted
Hello Mike,
You need to access the button through the Controls[] collection of the current item cell:
This should work now and you can handle your custom command in the ItemCommand event handler. Please note that for a GridButtonColumn of ButtonType="ImageButton", you need to set a valid ImageUrl for the custom command to work, otherwise the ItemCommand event is not fired.
Sincerely yours,
Veli
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center
You need to access the button through the Controls[] collection of the current item cell:
| void RadGrid1_ItemCreated(object sender, GridItemEventArgs e) |
| { |
| if (e.Item is GridDataItem) |
| { |
| GridDataItem item = (GridDataItem)e.Item; |
| ImageButton imgButton = (ImageButton)item["MyButton"].Controls[0]; |
| } |
| } |
This should work now and you can handle your custom command in the ItemCommand event handler. Please note that for a GridButtonColumn of ButtonType="ImageButton", you need to set a valid ImageUrl for the custom command to work, otherwise the ItemCommand event is not fired.
Sincerely yours,
Veli
the Telerik team
Instantly find answers to your questions at the new Telerik Support Center