I have 3 columns in a GridView. The first column has CheckBox, Second column has outstanding amount due in Label and Third column contains TextBox to pay Amount.
My problem is that TextBox should be filled with second column value when the checkbox is checked and textbox values should be cleared when checkbox uncheck.
Please refer java script code I have used.
<script type="text/javascript">
function SelectChange(ChkId, txt1, total) {
var chk = document.getElementById(ChkId);
UpdateField(ChkId, txt1, total)
}
function UpdateField(ChkId, txt1, total) {
if (document.getElementById(ChkId).checked == true)
{
var lblvalue = document.getElementById(total).value;
document.getElementById(txt1).innerHTML = lblvalue;
}
else
{
document.getElementById(txt1).innerHTML = "0";
}
}
</script>
In code behind...
protected void grdFeesCollection_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
TextBox txtPayment = (TextBox)e.Row.FindControl("txtPayment");
Label lblDue = (Label)e.Row.FindControl("lblDue");
CheckBox chckSelecthead = (CheckBox)e.Row.FindControl("chckSelect");
chckSelecthead.Attributes["onclick"] = "javascript:return SelectChange('" + chckSelecthead.ClientID + "','" + "','" + txtPayment.ClientID + "','" + lblDue.ClientID + "')";
txtPayment.Attributes["onKeyup"] = "javascript:return UpdateField('" + chckSelecthead.ClientID + "','" + txtPayment.ClientID + "','" + lblDue.ClientID + "')";
}
}
This is not returning any values to TextBox, That is the issue.
Code Behind:
protected void GridView1_RowDataBound( object sender, GridViewRowEventArgs e ) {
if ( e.Row.RowType == DataControlRowType.DataRow ) {
Label lblDue = (Label) e.Row.FindControl("lblDue");
TextBox txtPayment = (TextBox) e.Row.FindControl("txtPayment");
CheckBox chckSelect = (CheckBox) e.Row.FindControl("chckSelect");
string js = string.Format( "javascript: return SelectChange( '{0}', '{1}', '{2}' ); ",
chckSelect.ClientID,
lblDue.ClientID,
txtPayment.ClientID );
chckSelect.Attributes[ "onclick" ] = js;
}
}
JavaScript:
function SelectChange(ChkID, DueID, PaymentID) {
var CheckBox = document.getElementById(ChkID);
var Due = document.getElementById(DueID);
var Payment = document.getElementById(PaymentID);
Payment.value = (CheckBox.checked ? Due.textContent : "");
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments