将hiddenfield控件从服务器端传递给javascript [英] Pass hiddenfield control to javascript from server side

查看:55
本文介绍了将hiddenfield控件从服务器端传递给javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



非常高优先级的任务,请帮忙。

任何人都可以帮我解决以下问题。

我的页面上有gridview,它包含Linkbutton。



点击Linkbutton我正在调用服务器端代码,我想调用Javascript函数。

要Javascript功能我要发送三个参数,ID,名称和第三个参数是Hiddnefield Control。



通过编写下面的代码我我只能传递ID和名称但不能通过隐藏的野外控制。



如果我尝试传递隐藏字段,那么我的javascript不会调用。



任何人都可以帮助我,高优先级



以下是代码。



需要你宝贵的帮助。

我已经尝试了很多东西,但是没达到标准。



我尝试过:



Hi ,
Its very High Priority task, please help.
Can any one help me on the following.
I have gridview on my page,and it contains Linkbutton.

On clicking of Linkbutton i am calling server side code from where i want to call Javascript Function.
To Javascript function i want to send three parameters,ID,Name and third parameter is Hiddnefield Control.

By writing below code i am able to pass only ID and Name but not able to pass Hidden Field Control.

If i am trying pass Hidden field as well then my javascript does not call.

Can any one please help me on this,Its on high Priority.

Below is code.

Need your precious help.
I have tried so many things but it was not up to the mark.

What I have tried:

<asp:GridView ID="MyGrid" runat="server" AutoGenerateColumns="false" OnRowCommand="MyGrid_RowCommand">
        <columns>
            <asp:TemplateField HeaderText="Application ID">
                <itemtemplate>
                    <asp:Label ID="lblApplicationID" runat="server" Text='<%# Bind("application_id") %>'>
                    <asp:HiddenField ID="hdnApplicationID" runat="server" Value='<%# Bind("application_id") %>'>
                    
                
            
            <asp:TemplateField HeaderText="Application Type">
                <itemtemplate>
                    <asp:Label ID="lblApplicantType" runat="server" Text='<%# Bind("applicant_type") %>'>
                    <asp:HiddenField ID="hdnApplicantType" runat="server" Value='<%# Bind("applicant_type") %>'>
                    
                
            
            <asp:TemplateField HeaderText="Application Name">
                <itemtemplate>
                    <asp:Label ID="lblApplicatName" runat="server" Text='<%# Bind("applicant_name") %>'>
                    <asp:HiddenField ID="hdnApplicantName" runat="server" Value='<%# Bind("applicant_name") %>'>
                    
                
            
            <asp:TemplateField HeaderText="Document Type">
                <itemtemplate>
                    <asp:Label ID="lblDocumentType" runat="server" Text='<%# Bind("DOCUMENT_TYPE") %>'>
                    <asp:HiddenField ID="hdnDocumentType" runat="server" Value='<%# Bind("DOCUMENT_TYPE") %>'>
                    
                
            
            <asp:TemplateField HeaderText="Download">
                <itemtemplate>
                    <asp:LinkButton ID="lnkDownload" runat="server" Text="Download" CommandName="DownloadAddressProof">
                    <asp:HiddenField ID="hdnDocument" runat="server" Value="C:\Users\Prasanna\Desktop\SALARY SLIP JAN\GS POINT\ABHILASHA SALARY SLIP JAN-2017" />
                
            
            <asp:TemplateField HeaderText="Approve">
                <itemtemplate>
                    <asp:LinkButton ID="lnkApprove" runat="server" Text="Approve" Enabled="false">
                
            
            <asp:TemplateField HeaderText="Reject">
                <itemtemplate>
                    <asp:LinkButton ID="lnkReject" runat="server" Text="Reject" Enabled="false" CommandName="RejectAddressProof">



服务器端代码:


Server Side Code:

protected void MyGrid_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            GridViewRow gvr = (GridViewRow)((Control)e.CommandSource).NamingContainer;
            int rowIndex = gvr.RowIndex;
            

            if (e.CommandName == "RejectAddressProof")
            {

                string applicationID = "";
                string applicantName = "";
                applicationID = ((HiddenField)gvr.FindControl("hdnApplicationID")).Value;
                applicantName = ((HiddenField)gvr.FindControl("hdnApplicantName")).Value;

                //Session["applicationID"] = applicationID;
                //Session["applicantName"] = applicantName;

                //string url = "Popup.aspx?applicationID=" + applicationID + "&applicantName=" + applicantName;
                //string s = "window.open('" + url + "', 'popup_window', 'width=300,height=100,left=100,top=100,resizable=yes');";
                //ClientScript.RegisterStartupScript(this.GetType(), "script", s, true);
                HiddenField hdnValue = ((HiddenField)gvr.FindControl("hdnApplicantType"));


                //Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction('" + applicationID + "','" + applicantName + "'" + hdnValue + ")", true);
                Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction('" + applicationID + "','" + applicantName + "'," + hdnValue + "')", true);
            }
        }

推荐答案

Google搜索有很多例子:asp.net隐藏字段 [ ^ ]
Google Search has plenty of examples for you: asp.net hidden field[^]


传递隐藏字段值

To pass the hidden field value
HiddenField hdnValue = ((HiddenField)gvr.FindControl("hdnApplicantType"));
     string strValue = hdnValue.Value;





要传递隐藏字段控制,只需传递控件的ID



To pass the Hidden Field Control, just pass the id of the control

HiddenField hdnValue = ((HiddenField)gvr.FindControl("hdnApplicantType"));
           string hdnId = hdnValue.ClientID;

           Page.ClientScript.RegisterStartupScript(this.GetType(), "CallMyFunction", "MyFunction('" + applicationID + "','" + applicantName + "','" + hdnId + "')", true);



并选择javascript函数中的控件为




and select the control in javascript function as

function CallMyFunction (applicationID,applicantName, hdnId )
        {
            var hiddenControl = document.getElementById(hdnId);
            .
            .
            .
        }





注意:你不能直接从c#



Note:You cannot pass the control object directly from c#

发送控制对象

这篇关于将hiddenfield控件从服务器端传递给javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆