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

时间:2012-09-18 05:48:51

标签: c# javascript asp.net

嗨,我是javascript的新手,

我的问题是我有一个简单的日期检查功能,如下所示

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中

        <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我希望传递两个文本框的值..

任何人都可以帮助我..

谢谢..

3 个答案:

答案 0 :(得分:1)

使用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+ "');");
        }
}

}

在你的javascript函数中

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

答案 1 :(得分:0)

您可以尝试以下代码

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

然后您可以正常获取文本:

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

请考虑以下代码未经过测试。您可以尝试使用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 + "'");
    }
}

答案 2 :(得分:0)

我建议您使用的最佳方法是添加Row_DataBound,并在此事件中读取行并以编程方式添加更改事件。

void CustomersGridView_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+"')"+);


}

}