【问题标题】:How to store text box value in hidden field如何将文本框值存储在隐藏字段中
【发布时间】:2020-10-28 06:38:17
【问题描述】:

我想将文本框值存储在隐藏字段中。并且需要在按钮单击时填充隐藏值,但问题是按钮和文本框都在 ASPxGridView 内。

ASPxGridView 是分层网格视图,即 (DetailRow)

代码

将值存储到隐藏字段中

 function YourJavaScriptFunction() {
           <%--$("#<%= hdn.ClientID %>").val($('<%# ((ASPxGridView)Container).FindControl("txtCustCode").ClientID %>').val());--%>
            $("#hdn").val($("txtCustCode").val());
        }

隐藏字段

  <asp:HiddenField ID="hdn" runat="server" ClientIDMode="Static" />

网格视图

<dx:ASPxGridView ID="dgDepots" runat="server" ClientInstanceName="grid"
    KeyFieldName="LocationCode" Width="100%" OnDataBinding="dgDepots_DataBinding"
    OnRowDeleting="dgDepots_RowDeleting" OnRowInserting="dgDepots_RowInserting" OnRowUpdating="dgDepots_RowUpdating">
    <Columns>
        <dx:GridViewDataComboBoxColumn FieldName="LocationCode" PropertiesComboBox-ValueField="LocationCode" PropertiesComboBox-TextField="LocationName"
            VisibleIndex="0">
        </dx:GridViewDataComboBoxColumn>
        <dx:GridViewDataComboBoxColumn FieldName="CostCenterCode" PropertiesComboBox-ValueField="CostCenterCode" PropertiesComboBox-TextField="CostCenterName"
            VisibleIndex="1">
        </dx:GridViewDataComboBoxColumn>
        <dx:GridViewDataComboBoxColumn FieldName="GLCode" PropertiesComboBox-ValueField="GLAccountCode" PropertiesComboBox-TextField="GLAccountName"
            VisibleIndex="2">
        </dx:GridViewDataComboBoxColumn>
        <dx:GridViewCommandColumn ShowNewButton="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="3">
        </dx:GridViewCommandColumn>
    </Columns>
  <Templates>
        <DetailRow>
            <dx:ASPxGridView ID="dgCustomers" runat="server" KeyFieldName="CustomerCode"
                OnBeforePerformDataSelect="dgCustomers_BeforePerformDataSelect" OnDataBinding="dgCustomers_DataBinding"
                Width="100%" OnRowDeleting="dgCustomers_RowDeleting" OnRowInserting="dgCustomers_RowInserting"
                OnRowUpdating="dgCustomers_RowUpdating">
                <Columns>
                    <dx:GridViewDataTextColumn FieldName="CustomerCode" VisibleIndex="0">
                        <EditItemTemplate>
                            <asp:TextBox ID="txtCustCode" runat="server" Width="50px" />
                        </EditItemTemplate>
                    </dx:GridViewDataTextColumn>
                    <dx:GridViewDataColumn>
                        <EditItemTemplate>
                            <asp:Button ID="btnSearch" runat="server" Text="Search" OnClientClick="return YourJavaScriptFunction();" OnClick="btnSearch_Click" />
                        </EditItemTemplate>
                    </dx:GridViewDataColumn>
                    <dx:GridViewDataColumn FieldName="PriceGroupCode" VisibleIndex="1">
                        <EditFormSettings Visible="True" />
                    </dx:GridViewDataColumn>
                    <dx:GridViewDataColumn FieldName="ChannelCode" VisibleIndex="2">
                    </dx:GridViewDataColumn>
                    <dx:GridViewDataColumn FieldName="SubChannelCode" VisibleIndex="3">
                    </dx:GridViewDataColumn>
                    <dx:GridViewCommandColumn ShowNewButton="true" ShowEditButton="true" ShowDeleteButton="true" VisibleIndex="4">
                    </dx:GridViewCommandColumn>
                </Columns>

按钮点击后的代码隐藏

protected void btnSearch_Click(object sender, EventArgs e)
 {
      var ss = hdn.Value;  **//Showing Null**
 }

enter image description here 我哪里做错了?

【问题讨论】:

  • 您好,# 附近有 $("txtCustCode").. 小姐。
  • @Swati 我按照你的建议进行了更改 $("#txtCustCode") 但没有更改

标签: jquery asp.net


【解决方案1】:

你快完成了!

但是,

$("txtCustCode") 应该有一个 '#' 在它的 id 中,你错过了这个东西......

这样,

$("#hdn").val($("#txtCustCode").val());

如果上面的方法不行,试试下面的代码,

var hiddenField = document.getElementById("<%=hdn.ClientID%>");
var textFieldValue = document.getElementById("<%=txtCustCode.ClientID%>");

hiddenField.setAttribute("itemid",textFieldValue.value);

并像这样在后面的代码中获取值,

string itemId = hiddenField.Attributes["itemid"];

祝你好运……

【讨论】:

  • 我按照你的建议更改了我的代码但值仍然为空
  • 我认为文本框在网格视图中,它无法找到文本框 ID 可能是我不确定,但是 ..
  • 在调试其加热我的功能但无法从文本框中检索值
  • 按下按钮时该值不会保存在隐藏字段中,因为单击按钮时会发生回发。
  • 如果您想从隐藏字段中获取值,您必须在隐藏字段的属性中设置文本框值或其他内容,而不是存储在隐藏字段值中。例如,您可以在隐藏字段的“itemid”属性中设置值,并在后面的代码中获取值...
【解决方案2】:

我只是在元素选项卡中的浏览器上通过该文本框的完整 ID 检查特定文本框复制从那里然后粘贴相同

"$("#ContentPlaceHolder1_dgDepots_dxdt0_dgCustomers_0_DXEFL_0_editnew_0_0_txtCustCode").val();"

我可以得到价值

感谢大家的帮助

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 1970-01-01
    • 2013-04-06
    相关资源
    最近更新 更多