2012-07-09 24 views
0

好吧,這太令人沮喪了。我有一個9列的GridView。我有3個顯示,我想有一個單獨的表,顯示其他6,當我點擊行。當我點擊它時,我的javascript函數被設置爲將我選擇的行放入一個新表格中,除非它不起作用!無法將我的GridView放入我的javascript變量

<script type="text/javascript"> 
    var table = $('<table></table>'); 
    var grid = document.getElementById('<%=BillabilityView.ID %>'); // here it is inputting gridview. 
    $(document).ready(function() { 
     $(".tbl tr:has(td)").css({ background: "ffffff" }).click(
      function() { 
       var row = $('<tr></tr>').addClass('bar').text(grid.rows[$(this).index()]); // here it is saying 'unable to get the value of property rows' 
       table.append(row); 
       $('#please').append(table); 
      } 
     ); 
    }); 
</script> 

這只是沒有把我的變種gridview!即使我只是檢查grid.rows.length,它也不理解行。我正在查看其他代碼示例,他們做同樣的事情,我正在做,他們說,它工作正常。我100%確定它選擇了我的GridView。到底是怎麼回事?

編輯:這裏是它的asp.net側:

<asp:GridView ID="BillabilityView" BackColor="White" runat="server" AutoGenerateColumns="false" CssClass="tbl"> 
       <columns> 
        <asp:boundfield datafield="UserName" headertext="User Name" /> 
        <asp:boundfield datafield="UserID" headertext="User ID" /> 
        <asp:boundfield datafield="HrsTLB" headertext="Billable Hours" /> 
        <asp:boundfield datafield="HrsTLNB" headertext="Nonbillable Hours" /> 
        <asp:boundfield datafield="HrsTL" headertext="Total Hours" /> 
        <asp:boundfield datafield="HrsExp" headertext="Expected Hours" /> 
        <asp:boundfield datafield="Billability" headertext="Billability" /> 

       </columns> 
      </asp:GridView> 
      <div id = "please"> 
      </div> 

哎呀,我們甚至會扔在C#中它踢

BillabilityView.DataSource = dataList.retBillability(); 
BillabilityView.DataBind(); 
BillabilityView.RowDataBound += new GridViewRowEventHandler(BillabilityView_RowDataBound); 

    void BillabilityView_RowDataBound(object sender, GridViewRowEventArgs e) 
    { 
     if (e.Row.RowType == DataControlRowType.Header) 
     { 
      e.Row.BackColor = Color.FromName("#ebebeb"); 
     } 
     //e.Row.Attributes["onclick"] = ClientScript.GetPostBackClientHyperlink 
     // (this.BillabilityView, "Select$" + e.Row.RowIndex); 
    } 
+0

添加到您的html佈局,瞭解什麼是網格?和grid.Rows? – 2012-07-09 22:28:44

回答

1

請更改以下

var grid = document.getElementById('<%=BillabilityView.ID %>'); 
$(document).ready(function() { 

線要這樣

$(document).ready(function() { 
    var grid = $("#BillabilityView"); 

PS:出於一致性的緣故,請使用jQuery的getElementById代替的document.getElementById

0

變化:

var grid = document.getElementById('<%=BillabilityView.ID %>'); 

var grid = document.getElementById('<%=BillabilityView.ClientID %>'); 

另外,我認爲你正試圖從客戶端訪問服務器端的屬性,這是行不通的。