1

嗨,我是 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;
          } 

      }

在网格视图中

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

有谁能够帮我..

谢谢你..

4

3 回答 3

1

使用GridView_RowDataBoundgridview 事件和属性,您可以附加 javascriptonchange事件

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
} 
于 2012-09-18T07:12:44.037 回答
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 + "'");
    }
}
于 2012-09-18T06:25:39.993 回答
0

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

无效客户GridView_RowDataBound(对象发件人,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+"')"+);


}

}

于 2012-09-18T07:44:46.903 回答