如何将两个gridview文本框值传递给javascript函数 [英] how to pass two gridview textbox values to a javascript function

查看:53
本文介绍了如何将两个gridview文本框值传递给javascript函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是javascript的新手,我的问题是我有一个简单的日期检查JS函数,如下所示

i am new to javascript, my problem is i have a simple date check JS function as follow

function CompareDates(str1, str2)
   {
          var dt1 = parseInt(str1.substring(0, 2), 10);
          var mon1 = parseInt(str1.substring(3, 5), 10);
          var yr1 = parseInt(str1.substring(6, 10), 10);
          var dt2 = parseInt(str2.substring(0, 2), 10);
          var mon2 = parseInt(str2.substring(3, 5), 10);
          var yr2 = parseInt(str2.substring(6, 10), 10);
          var date1 = new Date(yr1, mon1, dt1);
          var date2 = new Date(yr2, mon2, dt2);

          if (date2 < date1) {
                alert("To date cannot be greater than from date");
                return false;
          }
          else
          {
                return true;
          }

      }


在Gridview


In Gridview

<asp:TemplateField HeaderText="Start Dtae">
                           <ItemTemplate>
                                        <asp:TextBox ID="txtStartDate" runat="server" Text='<%# Bind("StartDate") %>'></asp:TextBox>
                                        <asp:CalendarExtender ID="txtStartDate_CalendarExtender" runat="server" Format="dd/MM/yyyy"                 Enabled="True" TargetControlID="txtStartDate"></asp:CalendarExtender>
                            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="End Dtae">
                           <ItemTemplate>
                                        <asp:TextBox ID="txtEndDate" runat="server" Text='<%# Bind("EndDate") %>' **onchange="CompareDates(txtStartDate.Text,this.Text)**;" ></asp:TextBox>
                                        <asp:CalendarExtender ID="txtEndDate_CalendarExtender" runat="server" Format="dd/MM/yyyy" Enabled="True" TargetControlID="txtEndDate">**strong text**</asp:CalendarExtender>
                           </ItemTemplate>
         </asp:TemplateField>


网格是动态的,用户可以向其添加任意数量的行.我需要检查日期,而无需在单击提交按钮时循环遍历整个gridview行. txtenddate的onchange我想传递两个文本框的值..

有人可以帮我吗?

谢谢..


grid is dynamic and user can add any number of rows to it. my need is to check the date without looping the entire gridview rows on submit button click. onchange of the txtenddate i want to pass the values of both text boxes..

can anybody help me..

Thank You..

推荐答案


我认为最好将两个控件的ID传递给您的方法,然后从中获取值并继续您的验证代码.

例如......
将js方法修改为...
Hi
I think it will be better to pass the id of both control to your method and then get the values from that and continue with your validation code.

for example....
modify the js method as...
function CompareDates(ctrlStartID, ctrlEndID)
{
    var str1 = document.getElementByID(ctrlStartID);
    var str2 = document.getElementByID(ctrlEndID);
    // continue with your previous code.
}



而不是在html上分配onchange属性,而应使用网格视图的"rowdatabound"事件和该处的两个控件,并分配onchange属性并在此处传递两个控件的客户端ID.

喜欢......



and instead of assigning the onchange attribute on html, use the "rowdatabound" event of grid view and the both the control there and assign the onchange attribute and pass the client id of both the controls there.

like....

protected void yourgrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
        TextBox txtStart = (TextBox)e.Row.FindControl("txtStartDate");
        TextBox txtEnd = (TextBox)e.Row.FindControl("txtEndDate");
        txtStart.Attribute["onchange"] = "CompareDates('"+ txtStart.ClientID +"','"+ txtEnd.ClientID +"')";
txtEnd.Attribute["onchange"] = "CompareDates('"+ txtStart.ClientID +"','"+ txtEnd.ClientID +"')";
    }
}



我还将更喜欢使用"onblur"事件而不是"onchange"

希望对您有帮助.



I will also prefer to use "onblur" event instead of "onchange"

Hope this will help you.


这篇关于如何将两个gridview文本框值传递给javascript函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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