[英]Error while trying to get text value from textbox in C# (ASP.NET gridview)
When I'm trying to get a value from a textbox I keep getting this error (see image) and I don't know how to fix it. 当我尝试从文本框中获取值时,我不断收到此错误(参见图片),而且我不知道如何解决。 I'm using the following code:
我正在使用以下代码:
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;
}
I'm using javascript combined with ASP.NET and the code looks like this; 我将javascript与ASP.NET结合使用,代码看起来像这样; more info about the js: EDITABLE LABEL
有关js的更多信息:可编辑标签
JAVASCRIPT (it changes a label to a textbox on click) 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.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>
I'm 100% sure that my SQL code is correct. 我100%确定我的SQL代码正确。 Am I missing something?
我想念什么吗? I'd really appreciate the help!
我非常感谢您的帮助!
The value for ((TextBox)row.FindControl("txtEditTabel"))
appears to be returning a null or undefined value, hence the NullPointerException, when trying to dereference the .Text
property. 尝试取消引用
.Text
属性时, ((TextBox)row.FindControl("txtEditTabel"))
似乎返回null或未定义的值,因此返回NullPointerException。
Your use case seems really specific to your C# code, moreso than the JavaScript. 您的用例似乎真的特定于C#代码,而不是JavaScript。 My best guess is that you should try adding an
id
property to your control rather than a name. 我最好的猜测是,您应该尝试将
id
属性添加到控件中,而不是名称。 I could be wrong, but judging by the specs found here on FindControl method , you should be accessing the control by ID instead of name. 我可能是错的,但是从这里在FindControl方法上找到的规格来看,您应该通过ID而不是名称来访问控件。
Edit: If your input is inside of a form, try accessing it via the input's name property with the following syntax: Request.Form["txtName"];
编辑:如果您的输入在表单内,请尝试使用以下语法通过输入的name属性访问它:
Request.Form["txtName"];
instead of trying to do a FindControl
. 而不是尝试执行
FindControl
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.