【问题标题】:Error while trying to get text value from textbox in C# (ASP.NET gridview)尝试从 C# 中的文本框获取文本值时出错(ASP.NET gridview)
【发布时间】:2017-04-25 21:22:09
【问题描述】:

当我尝试从文本框中获取值时,我不断收到此错误(参见图片),我不知道如何解决它。我正在使用以下代码:

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;               
        }

我正在使用结合 ASP.NET 的 javascript,代码如下所示; 更多关于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 代码是正确的。我错过了什么吗?非常感谢您的帮助!

错误:

【问题讨论】:

  • 为什么不简单地为您的网格定义一个编辑模板?我认为你在这里混合asp.net和手工构建的客户端代码过于复杂。例如,请参阅gridview edit mode programatically

标签: javascript c# mysql asp.net gridview


【解决方案1】:

((TextBox)row.FindControl("txtEditTabel")) 的值似乎返回 null 或未定义的值,因此在尝试取消引用 .Text 属性时出现 NullPointerException。

您的用例似乎确实特定于您的 C# 代码,而不是 JavaScript。我最好的猜测是您应该尝试将id 属性添加到您的控件而不是名称。我可能是错的,但根据规范found here on FindControl method 判断,您应该通过 ID 而不是名称访问控件。

编辑: 如果您的输入在表单内,请尝试使用以下语法通过输入的名称属性访问它:Request.Form["txtName"]; 而不是尝试执行 FindControl

【讨论】:

    猜你喜欢
    • 2014-02-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多