2017-04-25 77 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

相关问题