2017-04-25 101 views
0

當我試圖從文本框中獲取值時,我不斷收到此錯誤(請參閱圖像),但我不知道如何解決它。我正在使用以下代碼:嘗試從C#中的文本框中獲取文本值時發生錯誤

protected void Button3_Click(object sender, EventArgs e) 
    { 
     _controller = new Controller(); 

     //Variablen 
     string afspraak =""; 

     foreach (GridViewRow row in GridView1.Rows) 
     { 
      afspraak = ((TextBox)row.FindControl("txtEditTabel")).Text;    
     } 

我正在使用JavaScript與ASP.NET結合,代碼如下所示; 關於JS的詳細信息:EDITABLE LABEL

JAVASCRIPT(它可以改變一個標籤上點擊文本框)

$(function() { 
//Loop through all Labels with class 'editable'. 
$(".editable").each(function() { 
    //Reference the Label. 
    var label = $(this); 

    //Add a TextBox next to the Label. 
    label.after("<input type = 'text' style = 'display:none' />"); 

    //Reference the TextBox. 
    var textbox = $(this).next(); 

    //Set the name attribute of the TextBox. 
    var id = this.id.split('_')[this.id.split('_').length - 1]; 
    //textbox[0].name = id.replace("Label","Textbox"); //tis dit hier van die id's 
    textbox[0].name = "txtEditTabel"; 
    //Assign the value of Label to TextBox. 
    textbox.val(label.html()); 

    //When Label is clicked, hide Label and show TextBox. 
    label.click(function() { 
     $(this).hide(); 
     $(this).next().show(); 
    }); 

    //When focus is lost from TextBox, hide TextBox and show Label. 
    textbox.focusout(function() { 
     $(this).hide(); 
     $(this).prev().html($(this).val()); 
     $(this).prev().show(); 
    }); 
}); 

});

ASP.NET

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4" ForeColor="#333333" GridLines="None" Width="1000px" HorizontalAlign="Center" OnSelectedIndexChanged="GridView1_SelectedIndexChanged"> 
     <AlternatingRowStyle BackColor="White" ForeColor="#284775" /> 
     <Columns> 
      <asp:TemplateField HeaderText="IDAfspraken"> 
       <ItemTemplate> 
        <asp:Label ID="Label4" runat="server" Text='<%# Eval("IDAfspraken") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      </asp:GridView> 

我100%肯定,我的SQL代碼是正確的。我錯過了什麼嗎?我非常感謝幫助!

錯誤: Error image

+0

爲什麼不簡單爲您的網格定義編輯模板?我認爲你在這裏混合使用asp.net和手工構建的客戶端代碼的方式過於複雜。例如,請參閱[編程式的[gridview編輯模式]](http://stackoverflow.com/q/16280495/205233)。 – Filburt

回答

0

((TextBox)row.FindControl("txtEditTabel"))的值出現試圖取消引用.Text屬性時被返回一個空值或未定義的值,因此空指針異常。

您的用例看起來非常特定於您的C#代碼,而不是JavaScript。我最好的猜測是你應該嘗試添加一個id屬性到你的控件而不是名字。我可能是錯的,但根據規格found here on FindControl method來判斷,您應該通過ID而不是名稱訪問控件。

編輯: 如果輸入是一個形式裏面,嘗試通過輸入的name屬性使用以下語法訪問它:Request.Form["txtName"];,而不是試圖做一個FindControl

相關問題