简体   繁体   English

如何将两个gridview文本框值传递给javascript函数

[英]how to pass two gridview textbox values to a javascript function

hi,i am new to javascript, 嗨,我是javascript新手,

my problem is i have a simple date check 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;
          } 

      }

In Gridview 在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>

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. 我需要检查日期,而无需在单击提交按钮时循环遍历整个gridview行。 onchange of the txtenddate i want to pass the values of both text boxes.. txtenddate的onchange我想传递两个文本框的值。

can anybody help me.. 有谁能够帮助我..

Thank You.. 谢谢..

Use GridView_RowDataBound event of gridview and on attributes you can attach the javascript onchange event 使用gridview的GridView_RowDataBound事件,并在属性上可以附加javascript onchange事件

void GridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{

    if(e.Row.RowType == DataControlRowType.DataRow)
    {
        if(e.Item.ItemType == ListItemType.Item)
        {
            TextBox txtStartDate = e.Row.FindControl("txtStartDate") as TextBox;
            TextBox txtEndDate= e.Row.FindControl("txtEndDate") as TextBox;

            txtEndDate.Attributes.Add("onchange", "CompareDates('" + txtStartDate.ClientID+ "', '" +txtEndDate.ClientID+ "');");
        }
}

} }

And in your javascript function 并在您的javascript函数中

function CompareDates(ctrlStartID, ctrlEndID) 
{ 
   var startDate = document.getElementByID(ctrlStartID).value; 
   var endDate = document.getElementByID(ctrlEndID).value; 
   //your further code
} 

You can try following code 您可以尝试以下代码

TextBox txtStartDate = yourGridViewName.SelectedRow.FindControl("txtStartDate") as TextBox;
TextBox txtEndDate = yourGridViewName.SelectedRow.FindControl("txtEndDate") as TextBox;

Then you can get text as normally: 然后,您可以照常获取文本:

 string startDate = txtStartDate.Text;
 string endDate = txtEndDate .Text;

Please consider that follwong code is not tested . 请考虑以下代码是否经过测试 You can try RowDataBound event of GridView to add JavaScript function 您可以尝试GridView的RowDataBound事件添加JavaScript函数

protected void yourGridView_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType == DataControlRowType.DataRow)
    {
         //Get TextBoxes
         TextBox txtStartDate = yourGridViewName.SelectedRow.FindControl("txtStartDate") as TextBox;
         TextBox txtEndDate = yourGridViewName.SelectedRow.FindControl("txtEndDate") as TextBox;

         //Get text
         string startDate = txtStartDate.Text;
         string endDate = txtEndDate .Text;

         //Assign the function
         e.Row.Attributes.Add("Onchange", "CompareDates('" + startDate + "', '" + endDate + "'");
    }
}

The best method I would suggest you is to add the Row_DataBound and in this event read the rows and add the change event programmatically. 我建议您最好的方法是添加Row_DataBound,在这种情况下,请读取行并以编程方式添加change事件。

void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e) { void CustomerGridView_RowDataBound(Object sender,GridViewRowEventArgs e){

if(e.Row.RowType == DataControlRowType.DataRow)
{
            Textbox text1 = ((Textbox )e.Row.FindControl("Textbox1"));
            Textbox text2 = ((Textbox )e.Row.FindControl("Textbox2"));
            text2 .Attributes.Add("onfocus", "EventName('"+text1 .Text+"','"+text1.Text+"')"+);


}

} }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM