IScriptControl Help
I have a radgrid in where I want to enable/disable a button when a user clicks a checkbox for a selected row. I originally just wanted to use some inline javascript and just enable to the button based on it's clientid but it can never find that id.
<
ClientSettings
ClientEvents-OnRowSelected
=
"RowSelected"
ClientEvents-OnRowDeselected
=
"RowDeselected"
>
<
Selecting
AllowRowSelect
=
"True"
/>
</
ClientSettings
>
function
RowSelected(sender, eventArgs)
document.getElementById(
'DeleteSelectedRadButton1'
).disabled =
false
;
// Register the namespace for client control.
Type.registerNamespace(
'MyGrid'
);
// ------------------------------------------------------------------------
// Definition of MyGrid class and properties
// ------------------------------------------------------------------------
MyGrid.Grid =
function
(element)
MyGrid.Grid.initializeBase(
this
, [element]);
// ------------------------------------------------------------------------
// MyGrid.Grid class properties
// ------------------------------------------------------------------------
this
._deleteButton =
null
;
this
._radGrid =
null
;
MyGrid.Grid.prototype =
// ------------------------------------------------------------------------
// initialization and clean-up
// ------------------------------------------------------------------------
initialize:
function
()
MyGrid.Grid.callBaseMethod(
this
,
'initialize'
);
Sys.Application.add_load(Function.createDelegate(
this
,
this
._onLoad));
,
dispose:
function
()
MyGrid.Grid.callBaseMethod(
this
,
'dispose'
);
,
// ------------------------------------------------------------------------
// public functions
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// private functions
// ------------------------------------------------------------------------
// ------------------------------------------------------------------------
// property accessors
// ------------------------------------------------------------------------
get_deleteButton:
function
()
return
this
._deleteButton;
,
set_deleteButton:
function
(value)
if
(
this
._deleteButton !== value)
this
._deleteButton = value;
this
.raisePropertyChanged(
'deleteButton'
);
,
get_radGrid:
function
()
return
this
._deleteButton;
,
set_radGrid:
function
(value)
if
(
this
._radGrid !== value)
this
._radGrid = value;
this
.raisePropertyChanged(
'radGrid'
);
,
_gridCommand:
function
(sender, args)
if
(args.get_commandName() ==
"RowSelected"
)
alert(
"Row: "
);
MyGrid.Grid.registerClass(
'MyGrid.Grid'
, Sys.UI.Control);
//expose the necessary data to the client control
public
IEnumerable<ScriptDescriptor> GetScriptDescriptors()
var descriptor =
new
ScriptBehaviorDescriptor(
this
.GetType().FullName,
this
.ClientID);
descriptor.AddProperty(
"deleteButton"
,
this
.DeleteSelectedRadButton1.ClientID);
descriptor.AddProperty(
"radGrid"
,
this
.MyGrid1.ClientID);
return
new
ScriptDescriptor[] descriptor ;
//create a script reference
public
IEnumerable<ScriptReference> GetScriptReferences()
ScriptReference reference =
new
ScriptReference(
this
.GridScript);
return
new
ScriptReference[] reference ;
private
string
GridScript=
"MyGrid.js"
;
Hello Kristian,
1. You should have <ClientEvents OnRowSelected="RowSelected" /> in the control html
2. You have to create a js function RowSelected
function RowSelected(sender, eventArgs)
var grid = sender;
var MasterTable = grid.get_masterTableView();
var row = MasterTable.get_dataItems()[eventArgs.get_itemIndexHierarchical()];
// or var row = eventArgs.get_gridDataItem();
// or var row= args.get_gridDataItem().get_element().rowIndex;
Ok, but how would I disable/enable my button? Like I said finding the ClientID doesn't work.
Assuming only one selected row, you could wire up some jQuery perhaps...?
Find the selected row with
var selectedRow = $('.rgSelectedRow');
var buttonid = selectedRow.find('[id$="Button1"]').attr('id');
var buttonref = $find(buttonid);
buttonref.set_enabled(false);
var selectedCol = $('.rgSelectedRow .buttonColumn');
Thanks steve, but your code looks like it suggests that my radbutton is in the radgrid, my radbutton is outside of the radgrid. Also wouldn't the jquery still run into the same problem, it is still looking for that radbutton's id?
Basically what I want is to have a radbutton that will be enabled when one or more rows are selected and disabled when 0 are selected. I have all the logic in place.. I am using a count to determine how many rows are selected.
it is just enabling / disabling the radbutton that is the problem.
<script type=
"text/javascript"
>
var
rowsSelected = 0;
function
RowSelected(sender, eventArgs)
rowsSelected++;
if
(rowsSelected > 0)
document.getElementById(
'DeleteSelectedRadButton1'
).disabled =
false
;
// <- is undefined
alert(
"Row: "
+ eventArgs.get_itemIndexHierarchical() +
" selected. Rows Selected="
+ rowsSelected +
"!"
);
var
e = eventArgs.get_domEvent();
function
RowDeselected(sender, eventArgs)
rowsSelected--;
if
(rowsSelected < 0)
document.getElementById(
'DeleteSelectedRadButton1'
).disabled =
true
;
// <- is undefined
alert(
"Row: "
+ eventArgs.get_itemIndexHierarchical() +
" deselected. Rows Selected="
+ rowsSelected);
var
e = eventArgs.get_domEvent();
</script>
<
telerik:RadButton
ID
=
"DeleteSelectedRadButton1"
runat
=
"server"
Skin
=
"Sitefinity"
Text
=
"Delete"
Font-Bold
=
"True"
Enabled
=
"True"
>
<
Icon
PrimaryIconCssClass
=
"rbRemove"
PrimaryIconLeft
=
"4"
PrimaryIconTop
=
"4"
/>
</
telerik:RadButton
>
<
telerik:RadGrid
ID
=
"RadGrid1"
runat
=
"server"
Skin
=
"Sitefinity"
AllowMultiRowSelection
=
"true"
AutoGenerateColumns
=
"False"
GridLines
=
"None"
AllowPaging
=
"True"
AllowSorting
=
"True"
PageSize
=
"7"
>
...
<
ClientSettings
AllowColumnsReorder
=
"True"
>
<
Selecting
AllowRowSelect
=
"True"
/>
<
ClientEvents
OnRowSelected
=
"RowSelected"
/>
<
ClientEvents
OnRowDeselected
=
"RowDeselected"
/>
</
ClientSettings
>
</
telerik:RadGrid
>
Oh! yes....I assumed the button lived in each grid row, sorry
So it should be easier then...you can still find the button by it's ID no problem.
Same code should still apply (assuming standard HTML button)
var button = $('[id$="Button1"]');
if(mycount ==0)
button.attr('disabled', true);
else
button.attr('disabled', false);
var buttonid = $('[id$="DeleteSelectedRadButton1"]').attr('id'); //gets the clientside ID
document.getElementById(buttonid).disabled = false; //Gets the object
var buttonid = $('[id$="Button1"]').attr('id'); //Gets unique ID for the button
var button = $find(buttonid); //Get the clientside object
if(mycount ==0)
button.set_enabled(false);
else
button.set_enabled(true);
I should note
This
document.getElementById(
'DeleteSelectedRadButton1'
).disabled =
true
;
// <- is undefined
DeleteSelectedRadButton1.ClientID
%>' in an external .js file...so whereas that converts itself to the retarded clientside ID on render, the jQuery searches the DOM after render.Awesome thanks! The clientidmode="static" works (in firebug) as well but it still can't enable/disable it that way. Which brings me to another question.
If you have more than one of those controls on a page would that effect every button with that id?