访问隐藏GridView控件中的现场设置在javascript中的价值? [英] access hidden field within gridview control to set a value in javascript?

查看:206
本文介绍了访问隐藏GridView控件中的现场设置在javascript中的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经得到了一堆模板列的gridview的。其中一个列是隐藏列如果在一个网格视图中的行改变了我的值设置为1,要么为0或1的值,否则我保留默认0。

I've got a gridview with a bunch of template columns. One of those columns is a hidden column that either has a value of 0 or 1. If a row in a grid view is changed I set the value to 1, otherwise I leave the default 0.

的模板列定义为:

<asp:TemplateField>
      <ItemTemplate>
          <input type="hidden" id="rowIsChanged" runat="server" />
      </ItemTemplate>
 </asp:TemplateField>

我的问题是(没有太多的细节),我有一个领域,我调用JavaScript函数,做在客户端上一些验证。在这个函数中,我想能够访问这个隐藏字段(rowIsChanged)这个特定行并将其值设置为1(在JavaScript)。这样,当我点击我的服务器端的更新按钮,我可以保证更新时给予我有一个条件checken为 rowIsChanged == 1

所以我的问题,我有话要这个效果现在:

So my question, I have something to this effect right now:

<asp:TemplateField>
  <ItemTemplate>
    <asp:TextBox id="txtMyDate" runat="server" onchange="DoSomeValidation(this)" />
  </ItemTemplate>
<asp:TemplateField>

和JavaScript:

And the javascript:

function DoSomeValidation(myParam)
 {
   //do some validation...

   //here is where I need to access the hidden field 'rowIsChanged' and set the value to "1"
 }

那么,如何可以访问这个特殊行的rowIsChanged领域并将其设置为1。

So how can I access this particular row's rowIsChanged field and set it to "1".

下面是详细信息:

在ASP标记:

<asp:TemplateField HeaderText="Date" SortExpression="LineItemDate">
      <ItemTemplate>
        <ajaxToolkit:CalendarExtender TargetControlID="txtMyDate" ID="CalendarExtender3" runat="server"></ajaxToolkit:CalendarExtender>
        <asp:TextBox ToolTip="Line Item Date"  Width="60px"  ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'></asp:TextBox>
      </ItemTemplate>
   </asp:TemplateField> 

问题是,当我第一次加载页面并加载网格数据,系统将自动假设一个变化的日期作出(隐藏字段的值是1)。但是,未进行任何更改,这只是一个初始加载...所以我不知道如何来处理这个问题。

The issue is when I first load the page and load the grid data the system automatically assumes a change was made to the date (value of hidden field is 1). But no changes were made it is just an initial load...So I dont know how to handle this.

在GridView的数据绑定行事件我有code以下片段:

In the gridview's row databound event I have the following snippet of code:

If e.Row.RowType = DataControlRowType.DataRow Then
            Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("rowIsChanged"), HtmlInputHidden)

            Dim tLID As TextBox = CType(e.Row.FindControl("txtMyDate"), TextBox)

            tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")
 ....

所以我尽快公布为RowDataBound事件code还有markup..but在页面加载与完成的野外编辑 txtMyDate 和我点击我的页面上的更新按钮,做的东西这样的效果:

So I've posted code for the rowdatabound event as well as the markup..but as soon as the page loads with NO edits done to the field txtMyDate and I click the "Update" button on my page which does something to this effect:

  For Each Row As GridViewRow In Me.gvLineItems.Rows
            Dim hiddenField As HtmlInputHidden = DirectCast(Row.FindControl("rowIsChanged"), HtmlInputHidden)
            'only update rows that have been edited / update IsDirty=1 :)
            If (hiddenField.Value = "1") Then

每一行似乎有值1的行已经更新,好象(的onchange接到电话......)。但这种情况并非如此。这对于不使用日历控件任何其他方面的正常工作。举例来说,如果我评论该行:

Every row seems to have the value 1 as if the row was already updated (onchange got called...). But this is not the case. This works fine for any other field that is not using the calendar control. For instance, if I comment the line:

tLID.Attributes.Add("onchange", "document.getElementById('" + hiddenField.ClientID + "').value=1")

和刚刚离开的onchange我所有其他字段,然后单击更新按钮,它按预期工作(也就是如果我改变字段中的值,则隐藏字段的值为1,如果我不改变在字段值,他们具有值0)。但问题是仅此一领域...

And just leave the onchange for all my other fields and then click the "Update" button it works as expected (that is if I change the value in the fields then the hidden field has the value 1 and if i dont change the values in the field they have the value 0). But the issue is just this one field...

只是为了测试我甚至尝试这样的:

Just for testing I even tried this:

Protected Sub gvLineItems_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvLineItems.RowDataBound
        If e.Row.RowType = DataControlRowType.DataRow Then
            'this is used to add a trigger to the <TRIGGERS> tag for the update panel
            'I could not do it in the HTML / ASP code because the button is inside of a gridview control
            Dim hiddenField As HtmlInputHidden = DirectCast(e.Row.FindControl("hdnIsChanged"), HtmlInputHidden)

            'the line item date
            Dim tLID As TextBox = CType(e.Row.FindControl("txtMyDate"), TextBox)

            'just a test...
            tLID.Attributes("onchange") = "helloworld();"

我所做的只是添加一个js函数调用的HelloWorld(),看起来像这样:

All I did was add a js function called helloworld() that looks like this:

 function helloworld()
   {
   alert ("hello world");
   }

我跑我的项目,马上它就会提醒出的hello world(我在网格2行,因此它提醒了世界你好两次)。这是不受任何更改数据,问题是为什么叫做平变化没有任何改变?它必须能够与它是如何设置从从数据库返回数据的日期,是,认为是onchange事件,当它看到的标记,像这样:

I run my project and right away it alerts out hello world (I have 2 rows in the grid so it alerts out hello world twice). This is without any change whatsoever to the data, the question is why is onchange called without any change? It must have to be with how it is setting the date from the the data returned from the database, is that considered an onchange event when it sees markup like so:

<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>' />

具体的的DataBinder.Eval(集装箱,DataItem.MyDate)...

@Tim

我决定做一个小的变化,你在你的意见,你在txtMyDate静态地分配一个值,它填补提到的(而不是从数据库中提取的价值像我一样)。因此,而不是这样的:

I decided to make a small change, you mentioned in your comments you filled in the txtMyDate statically by assigning a value to it (rather then pulling the value from the database like I did). So instead of this:

<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'

只是为了笑声我改成了这样:

Just for giggles I changed it to this:

<asp:TextBox ToolTip="Line Item Date"  Width="60px"  ID="txtMyDate" runat="server" Text="1/1/2011">

这是我设置的值的静态的像你这样......我跑我的项目,现在警报不火,因此没有的onchange ,这可能是为什么你不能重现错误。所以会出现这样的:

That is I set the value statically like you did...I run my project and now the alert doesn't fire, hence no onchange, this is probably why you could not reproduce the error. So it appears this:

<asp:TextBox ToolTip="Line Item Date" ID="txtMyDate" runat="server" Text='<%# DataBinder.Eval(Container, "DataItem.MyDate") %>'

引起的onchange 事件触发,这对我来说是不好的:(......所以问题是如何处理还是这个?

Causes the onchange event to fire, which for me is not good :(...so question is how to still handle this??

哇对不起,所有的编辑,我想在这里消除的可能性...
所以,现在我所做的就是删除此行:

Wow sorry for all the edits, I am trying to eliminate possibilities here... So now what I did was remove this line:

&LT; ajaxToolkit:CalendarExtender的TargetControlID =txtMyDateID =CalendarExtender3=服务器&GT;&LT; / ajaxToolkit:CalendarExtender&GT;

含义我删除从标记和onchange事件上的负载事件不再火灾calendarextender。为什么会这样会导致这种情况发生。即使我离开这个:

Meaning I removed the calendarextender from the markup and the onchange event no longer fires on the load event. Why would this be causing this to happen. Even If I leave this:

&LT; ASP:文本框工具提示=行项目日期ID =txtMyDate=服务器文本='&LT;%#DataBinder.Eval的(集装箱,DataItem.MyDate) %GT;'&GT;

只要我消除它的工作原理罚款calendarextender ......我说过这GridView控件是一个UpdatePanel内?难道这是问题?问题是我不希望删除calendarextender ......我希望能够让用户选择从一个日期而不是在键入它。

As long as I eliminate the calendarextender it works fine...Did I mention this gridview is inside an updatepanel? Could this be the issue?? The problem is I do not want to remove the calendarextender...I would like to be able to allow users to select a date from that rather then typing it in.

推荐答案

您可以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.

这篇关于访问隐藏GridView控件中的现场设置在javascript中的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆