2011-10-28 32 views
2

Nridubai網站,我使用listview EditTemplate進行編輯。在我EditTemplate,有喜歡控制..如何使用ListView EditTemplate中控件的javascript驗證?

<asp:TextBox ID="txtEditEventName" runat="server" 
         Text='<%# Bind("event_name") %>' /> 

而像下拉列表幾個控件,日曆控件。現在我想驗證在這些控件上使用JavaScript,但它不起作用。

例如,

var eventStatus=document.getElementById("<%=txtEditEventName.ClientID%>").value; 

我沒有使用驗證控件。請幫助我如何使用javascript在EditTemplate控件上進行驗證?我EditTemplate結構是這樣的:

<EditItemTemplate> 
             <td class="command"><asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CommandName="Cancel" /> 
             <asp:LinkButton ID="LinkButton2" runat="server" Text="Update" CommandName="Update" /> 
             </td> 
             <div class="header">View Details for &#39;<%# Eval("event_name")%>&#39;</div> 

            <tr> 
             <td class="edit" colspan="6" > 
              <div class="details"> 
               <table class="detailview" cellpadding="0" cellspacing="0"> 
               <tr> 


       <td>Event Name:</td> 
       <td> 
        <asp:TextBox ID="txtEditEventName" runat="server" 
         Text='<%# Bind("event_name") %>' /> 
       </td> 
       <td>VenueAddress :</td> 
       <td> 
        <asp:TextBox ID="txtEditVenue" runat="server" Text='<%# Bind("venue") %>' /> 
       </td> 

       </tr> 


       <tr> 

       <td>Country :</td> 
       <td> 

        <asp:DropDownList ID="lstEditCountry" runat="server" 


           Width="174" /> 



       </td> 


       <td>Event Status:</td> 
       <td> 
       <asp:DropDownList ID="lstEditStatus" runat="server" Width="175px" > 
             <asp:ListItem value='0' Selected="True">-Select-</asp:ListItem> 

<asp:ListItem >In-Progress</asp:ListItem> 
<asp:ListItem >Completed</asp:ListItem> 
<asp:ListItem >Aborted</asp:ListItem> 

    </asp:DropDownList> 

       </td> 


       </tr> 
       <tr> 
       <td>Category :</td> 
       <td> 
         <asp:DropDownList ID="lstEditCategory" runat="server" 


           Width="174" /> 
       </td> 
       </tr> 
       <tr> 
       <td>Start Date:</td> 
       <td> 
        <asp:TextBox ID="txtEditStartDate" runat="server" 
         Text='<%# Bind("start_date", "{0:dd/MM/yyyy}") %>' /> 
       </td> 
       <td>End Date:</td> 
       <td> 
        <asp:TextBox ID="txtEditEndDate" runat="server" 
         Text='<%# Bind("end_date","{0:dd/MM/yyyy}") %>' /> 
       </td> 


       </tr> 






               </table> 
               <div class="footer command"> 
               <asp:LinkButton ID="LinkButton1" runat="server" Text="Close" CommandName="Cancel" /> 
               </div> 
              </div> 
             </td> 
            </tr> 
          </EditItemTemplate> 
+0

我們需要更多的爲您提供驗證值的工作代碼。爲我們提供edittemplate的完整標記 – Deeptechtons

回答

1

您可以訪問的元素上ItemDataBound併發出自己的ClientIDs爲你的JavaScript使用方法:

的ItemDataBound:

protected void ListView1_ItemDataBound(object sender, ListViewItemEventArgs e) 
{ 
    StringBuilder vars= new StringBuilder(); 

    if (ListView1.EditItem != null) 
    { 
     TextBox txtEditStartDate = ListView1.EditItem.FindControl("txtEditStartDate") as TextBox; 
     TextBox txtEditEndDate = ListView1.EditItem.FindControl("txtEditEndDate") as TextBox; 

     //example js, however I recommend passing the ClientIDs to functions 
     vars.Append(String.Format("var txtEditStartDate='{0}';" txtEditStartDate.ClientID); 
     vars.Append(String.Format("var txtEditStartDate='{0}';", txtEditEndDate.ClientID); 
     ClientScriptManager.RegisterStartUpScript(this.GetType(), "validationVars", vars.ToString(), true); 
    } 
} 

** *老應答時,.NET方式* ** * ** * ** * **

EditTemplate:

<asp:TextBox ID="txtEditEventName" runat="server" 
         Text='<%# Bind("event_name") %>' /> 
<asp:RequiredFieldValidator 
        id="rfvEditEventName" 
        ClientValidationFunction="txtEditEventNameClientValidate"  
        ControlToValidate="txtTitle" 
        runat="server"  
        Display="dynamic">* 
        </asp:RequiredFieldValidator> 

JS:

function txtEditEventNameClientValidate(sender, args) { 
    if (args.Value == '') { 
     args.IsValid = false; // field is empty  
     //so something 
    } 
    else { 
     //do something 
    } 
} 
+0

Thankssss ...我只想要javascript。我用var v = $('#<%= txtEditEventName.ClientID%>');但它顯示錯誤不存在..請幫助我......... –

+0

查看更新,增加了另一種方式將ClientID傳遞給客戶端。 –

+0

對不起,錯誤,在.NET方式代碼行應該不存在,我刪除它。 –

0

將驗證JavaScript放入EditTemplate本身。這樣,當它切換到編輯模式時,控件將在上下文中。

+0

我把javascript代碼放在editTemplate中,但它給了我同樣的錯誤。 –

+0

什麼是確切的錯誤。 –

+0

我在JavaScript中找不到Edittemplate控件進行驗證。 –

相關問題