我在GridView中有3列。第一列具有CheckBox,第二列具有在Label中到期的未付金额,第三列包含TextBox以支付金额。
我的问题是,当复选框处于选中状态时,TextBox应填充第二列值,而取消选中复选框时,应清除文本框值。
请参考我使用过的Java脚本代码。
<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>
在后面的代码中...
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 + "')";
}
}
这不会向TextBox返回任何值,这就是问题所在。
背后的代码:
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 : "");
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句