【问题标题】:HiddenField coming back as blank in the code behindHiddenField 在后面的代码中返回为空白
【发布时间】:2016-10-10 15:53:49
【问题描述】:

我有一个网格视图,该网格的信息由文本框输入。然后我单击保存按钮,此信息将保存到网格中。 对于其中一个文本框,它会显示一个菜单。用户从菜单中选择债权人,债权人 ID 保存在 HiddenField 中。

<td class="tblAddDetail" style="border-right:2px">
   <a style="float:left;width:16px;height:16px;margin-right:0px;left:0px;top:1px" title="Pick from list..." class="iconSearch" id="btnAddDetailCreditor"></a>
   <asp:HiddenField ID="hfCreditorID" runat="server"  />
   <input type="text" id="txtAddEditCreditorCode" class="lookuppopup" onblur="CheckCreditorAccountDetail(this.value)" style="text-transform:uppercase;width:80px" runat="server"/>
 </td> 

当用户从列表中选择债权人时,此函数将运行该函数,该函数将使用名称填充文本框,并使用 id 填充 HiddenField:

function CheckCreditorAccountDetail(AC) {
            //AJAX Save
            if ($.trim(AC).length) {
                PageMethods.GetCreditorAccountCode(AC,
                                            OnCheckCreditorDetail,
                                            null
                                            );
            }
        }

 function OnCheckCreditorDetail(result) {
 $('#<%= hfCreditorID.ClientID %>').val(result.ID);
            $('#<%= txtAddEditCreditorCode.ClientID %>').val(result.AccountCode);
}

它调用WebMethod,在数据库中找到债权人:

[WebMethod]
    public static Creditor GetCreditorAccountCode(string AccountCode)
    {
        try
        {
            Creditor c = new Creditor(AccountCode);
            return c;
        }
        catch (Exception ex)
        {
            return new Creditor();
        }
    }

如果WebMethod 返回一个值,它会填充隐藏字段和文本框。所以result 是债权人,调用result.AccountCode 会给债权人帐户代码等。

但是当我尝试在后面的代码中调用这个 HiddenField 时,它总是空白:

if (!int.TryParse(hfCreditorID.Value, out tmpCredID))
{
    valid = false;
}

问题是当我单击保存按钮时,它会导致回帖,因此我丢失了所有值。不是隐藏字段失去了它的价值,文本框也变回空白

【问题讨论】:

  • this.value 只是一个文本,一个字符串,它不会有ID 字段
  • @Andrei 它从result.ID 找到ID 值,它确实有一个值。这只是一个数字。问题是在后面的代码中调用 hfCreditorID.Value 以空白返回
  • @Andrei 请查看我编辑的问题。我遗漏了一些功能,但希望这能更好地解释问题

标签: javascript c# jquery asp.net gridview


【解决方案1】:

看起来您正在混合/尝试混合使用 javascript 和 C# 在后面的代码中。

您正在发送result(正如 Andrei 已经指出的那样,这是 javascript 中的字符串)并尝试访问不存在的属性。 result.AccountCode?

您的 javascript 函数名称与 CheckCreditorAccountDetailOnCheckCreditorDetail 不匹配。

如果您可以直接从 TextBox 后面的代码中获取值,为什么还要使用 HiddenField? 有关工作示例,请参阅我的 sn-p。

在 .aspx 页面上:

<asp:HiddenField ID="hfCreditorID" runat="server" />
<asp:TextBox ID="txtAddEditCreditorCode" runat="server" onblur="OnCheckCreditorDetail(this.value)"></asp:TextBox>

<script type="text/javascript">
    function OnCheckCreditorDetail(result) {
        $('#<%= hfCreditorID.ClientID %>').val(result);
     }
</script>

在后面的代码中:

    protected void Button1_Click(object sender, EventArgs e)
    {
        txtAddEditCreditorCode.Text = hfCreditorID.Value + " - OK";
        //or just get the values from the TextBox without a HiddenField
        txtAddEditCreditorCode.Text = txtAddEditCreditorCode.Text + " - OK";
    }

【讨论】:

  • 请查看我编辑的问题。我遗漏了一些功能,但希望这能更好地解释问题
  • 我尝试使用文本框而不是隐藏字段但失去了它的价值
  • 尝试一个只有我的 sn-p 的空白页。如果那行得通,那就从那里开始构建。您可能在某处丢失了回发的值。
猜你喜欢
  • 2015-02-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-17
  • 1970-01-01
相关资源
最近更新 更多