【问题标题】:In GridView, when click on checkbox add values from label to TextBox using JavaScript in the same row of Grid在 GridView 中,当单击复选框时,使用 JavaScript 在 Grid 的同一行中将值从标签添加到 TextBox
【发布时间】:2016-05-14 09:13:24
【问题描述】:

我在 GridView 中有 3 列。第一列是 CheckBox,第二列是 Label 中的应付金额,第三列是 TextBox to pay Amount。

我的问题是,当复选框被选中时,文本框应该填充第二列值,而当复选框取消选中时,文本框值应该被清除。

请参考我用过的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 返回任何值,这就是问题所在。

【问题讨论】:

  • 究竟是什么行不通?您收到任何 javascript 错误吗?
  • 实际上不起作用。单击复选框时没有得到任何输出
  • 试试 chckSelecthead.Attributes["onclick"] = "SelectChange('" + chckSelecthead.ClientID + "','" + "','" + txtPayment.ClientID + "',' " + lblDue.ClientID + "')";如果它不起作用,请发布您收到的 javascript 错误
  • 出现错误。未捕获的 ReferenceError:未定义 UpdateField
  • 何时单击复选框错误显示 Uncaught ReferenceError: UpdateField is not defined @Cyril

标签: javascript asp.net gridview checkbox


【解决方案1】:

代码背后:

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 : "");
}

【讨论】:

  • 如何通过同时选中或取消选中所有文本框来遍历此网格以将更改应用于所有文本框。
  • 这有点复杂。我已经看到它很容易用 jQuery 实现。将“CheckAll”复选框添加到标题列并将其 onclick 属性设置为一个函数,该函数使 jquery 调用来处理复选框的切换并根据需要更新/清除支付文本框。谷歌asp.net Gridview CheckAll你会发现很多例子
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-23
  • 2013-07-30
  • 1970-01-01
相关资源
最近更新 更多