更新时间:2023-02-20 18:11:14
您可以onChange处理从codebehind添加的javascript:
You could add the javascript onchange handler from codebehind:
Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)
If e.Row.RowType = DataControlRowType.DataRow Then
Dim hidden = DirectCast(e.Row.FindControl("rowIsChanged"), HtmlInputHidden)
Dim txtMyDate = DirectCast(e.Row.FindControl("txtMyDate"), TextBox)
txtMyDate.Attributes("onchange") = "DoSomeValidation(this,'" & hidden.ClientID & "');"
End If
End Sub
C#
protected void GridView1_RowDataBound(object sender, System.Web.UI.WebControls.GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow) {
var hidden = (HtmlInputHidden)e.Row.FindControl("rowIsChanged");
var txtMyDate = (TextBox)e.Row.FindControl("txtMyDate");
txtMyDate.Attributes("onchange") = "DoSomeValidation(this,'" + hidden.ClientID + "');";
}
}
修改:这里是JS功能,它的工作原理设置的值,并回发后阅读:
Edit: here is the js-function, it works to set the value and read it after postback:
<script type="text/javascript">
function DoSomeValidation(ctrl, hiddenID) {
var hidden = document.getElementById(hiddenID);
if(ctrl.defaultValue != ctrl.value)
hidden.value = "1";
}
</script>
EDIT2 :添加当前文本框值之间的差值进行额外的检查,它的的设置defaultValue 。
Edit2: added an additional check for a difference between the current textbox' value and it's defaultValue.