【问题标题】:How to get JavaScript variable value in C# code behind如何在后面的 C# 代码中获取 JavaScript 变量值
【发布时间】:2019-08-23 19:34:21
【问题描述】:

我有一个带有自定义按钮的列的网格视图。 当我点击按钮时,会通过 JavaScript 询问用户联系方式,然后将联系方式传递给 gridview 的 rowCommand 事件以进行某些操作。

我将 Hiddenfield 用作 gridview 列,但在设置和获取值时出现问题。

JavaScript 代码:

   <script type="text/javascript">
    function validateContact()
        {
        var contact = prompt("Please enter your contact number.");
        if (isEmpty(contact) && !isNumber(contact) && contact.length != 10)
            alert("Invalid contact.");
            return;
        else
            document.getElementById("hiddenCustomerContact.ClientID").value = contact;
    }
</script>

asp 代码:

    <asp:GridView ID="gridSearchWorker" runat="server" AutoGenerateColumns="false"               OnRowCommand="gridSearchWorker_OnRowCommand" OnRowDataBound="gridSearchWorker_RowDataBound">
        <Columns>
        <asp:BoundField DataField="WORKER" HeaderText="WORKER" SortExpression="WORKER">
            <ItemStyle Width="12%" />
        </asp:BoundField>
        <asp:TemplateField HeaderText = "" ItemStyle-Width="5%">
            <ItemTemplate>
                <asp:Button ID="btnGetContact" runat="server" Text="Get Contact" CommandName="GetContact" OnClientClick="validateContact();" />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:BoundField DataField="CONTACT" HeaderText="CONTACT" SortExpression="CONTACT">
            <ItemStyle Width="12%" />
        </asp:BoundField>
        <asp:TemplateField HeaderText = "" ItemStyle-Width="5%">
            <ItemTemplate>
                <asp:HiddenField ID="hiddenCustomerContact" runat="server"/>
            </ItemTemplate>
            <ItemStyle Width="0.0001%" />
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

后面的c#代码:

    protected void gridSearchWorker_OnRowCommand(object sender, GridViewCommandEventArgs e)
    {

        if (e.CommandName == "GetContact")
        {
            OleDbConnection conn = new OleDbConnection(cs);

            //ask for contact and check if any feedback pending
            HiddenField hiddenCustomerContact = 
                e.Row.FindControl("hiddenCustomerContact") as HiddenField; //getting error in this line


            string score = hiddenCustomerContact.Value;

            int n = Int32.TryParse(score, out val);
            string customerContact = "";

            //check if any feedback pending

            int pendingCount;

            if (pendingCount > 0)
            {
                //ask to give feedback

            }

            else
            {
                //show worker contact
            }
        }
    }

【问题讨论】:

  • 你用的是getElementById("hiddenCustomerContact.ClientID"),不应该是getElementById("hiddenCustomerContact")吗?
  • 好的...我会尝试删除 ClinetID。但错误在后面的代码中。
  • 您遇到了什么错误?
  • 我认为您需要获得该行。也许this 有帮助?
  • @haldo 应该是getElementById("&lt;%= hiddenCustomerContact.ClientID %&gt;") 来获取webforms中的元素ID。

标签: javascript c#


【解决方案1】:

嗨,它应该像这样工作: 您可以为隐藏变量分配一些值

<script type="text/javascript">
        var somefunction = function () {
            var hdnfldVariable = document.getElementById('hdnfldVariable');
            hdnfldVariable.value = 'value from javascript';
        }
    </script>

您可以在后面的代码中读取此字段变量:

string variable = hdnfldVariable.Value;

【讨论】:

    猜你喜欢
    • 2021-01-29
    • 2013-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多