Disabling/enabled textbox in GridViewRow
I have a checkbox and a textbox in a GridView (for every row), and I'd like to be able to write some javascript that would enable 开发者_Go百科and disable the textbox depending on the state of the checkbox (checked or unchecked). I guess I'd have to give JS the row index somehow. Is this possible?
If you are ready to use jquery then this can be very easily accomplished as follows:
$(document).ready(function() {
$('.grid tr').each(function() {
var r = $(this);
var c = r.find('.check');
var t = r.find('.text');
var f = function(c1, t1) { return function() {
if (c1.attr('checked')) {
t1.removeAttr('disabled');
}
else {
t1.attr('disabled', 'disabled');
}
};
};
c.click(f(c, t)); // attach click event handler
f(c, t); // set initial state
});
});
Above script is assuming that the gridview table is decorated with grid
css class and each textbox and checkbox are decorated with check
and text
css class respectively.
Yes this is absolutely possible.
To implement this you need to do the following:
Write a generic JavaScript function to handle the enabling/disabling of a Text-Box based on the state of the Check-Box;
Example :-
function ManageControlEnabling(sourceCheckBoxId, affectingControlId)
{
var sourceCheckBox = document.getElementById(sourceCheckBoxId);
var affectingControl = document.getElementById(affectingControlId);
var shouldEnable = false;if (sourceCheckBox && affectingControl)
{
shouldEnable = sourceCheckBox.checked;if(shouldEnable) affectingControl.disabled = false; else affectingControl.disabled = true;
}
}Handle the Row-Data-Bound event of the Grid-View. Implement a similar functionality in your .Net code, to handle the enabling/disabling of the text-box (for each row) based on the state of the check-box. And also add the JavaScript function to the check-box;
Example :-CheckBox checkSource; TextBox textAffecting; checkSource = e.Row.FindControl("cbSource") as CheckBox; textAffecting = e.Row.FindControl("tbAffecting") as TextBox; if(checkSource != null) { textAffecting.Enabled = checkSource.Checked; } checkSource.Attributes.Add("onclick", "ManageControlEnabling('" + checkSource.ClientID + "', '" + textAffecting.ClientID + "', true);");
Please see if this helps.
Thanks.
Thanks for all your replies, I have worked around this issue by just doing a postback - not the best method but due to time constraints it was the logical choice
精彩评论