2010-09-03 45 views
1

我在gridview裏面有一個文本框。我需要在JavaScript中獲取文本框的ID。當我像這樣使用'<%= txtNewQty.ClientID%>';它給編譯錯誤。Asp.net gridview

+0

你想在javascript中獲得文本框的事件 – Azhar 2010-09-03 05:01:12

+0

在這裏做任何答案回答你的問題?或者你需要更多的信息? – JumpingJezza 2010-09-17 04:34:00

+0

沒有什麼可以幫助我。 iam無法獲取在gridview中作爲itemtemplate添加的文本框控件的客戶端ID。 – Bala 2010-09-17 07:14:18

回答

0

txtNewQty不會在頁面級別存在,因此你將無法引用它以這種方式。你必須做一些事情,比如在服務器端生成js,或者從gridview元素開始。張貼一些代碼並描述你想要發生的事情。

0

GridView中的控件不是頁面的成員,它們只存在於.aspx中。他們會在每一行重複,所以他們dinamically創建。

爲了獲得在GridView的行內部控制的參考,您可以使用FindControl

foreach(GridViewRow row in gridview.Rows) 
{ 
    Textbox tb = row.FindControl("txtNewQtv") as Textbox; 
    string id = tb.ClientID; 
    //Generate your javascript here so it can be callable on the client 
} 
0

這是一個不好的方法,因爲它被硬編碼爲列索引。它的工作原理,但必須有更好的方式使用jQuery。不幸的是,我不是這方面的專家。

<script type="text/javascript"> 
    function Update(btn) { 
     alert(btn.parentNode.parentNode.children[0].children[0].id); 
    } 
</script> 
<asp:GridView ID="GridView1" runat="server"> 
    <Columns> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <asp:TextBox ID="txtNewQtv" runat="server"></asp:TextBox> 
      </ItemTemplate> 
     </asp:TemplateField> 
     <asp:TemplateField> 
      <ItemTemplate> 
       <input type="button" onclick="Update(this)" value="Update" /> 
      </ItemTemplate> 
     </asp:TemplateField> 
    </Columns> 
</asp:GridView>