简体   繁体   English

如何在C#代码中获取JavaScript变量值

[英]How to get JavaScript variable value in C# code behind

I have a gridview with a column with customised button. 我有一个带有自定义按钮列的gridview。 When I click the button, user contact is asked for through JavaScript and then the contact is passed on to rowCommand event of gridview for some operation. 当我单击按钮时,将通过JavaScript请求用户联系人,然后将该联系人传递到gridview的rowCommand事件以进行某些操作。

I am using Hiddenfield as a gridview column but there is something going wrong in setting and getting the value. 我将Hiddenfield用作gridview列,但是在设置和获取值时出了点问题。

JavaScript code: 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 code: 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# code behind: 后面的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
            }
        }
    }

Hi it should work like this: you can assign some value to the hidden variable as 嗨,它应该像这样工作:您可以为隐藏变量分配一些值,如下所示:

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

in code behind you can read this field variable: 在后面的代码中,您可以阅读以下字段变量:

string variable = hdnfldVariable.Value;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM