简体   繁体   English

如何在控件vb.net中将参数从javascript传递到隐藏字段

[英]how can i pass parameters from javascript to hidden field in controls vb.net

i have a control that has two asp:HiddenField 我有一个控件有两个asp:HiddenField

  <asp:HiddenField runat="server" ID="tabTitle" />
  <asp:HiddenField runat="server" ID="tabMenu" />

this control load in a page called Alarms 此控件在名为Alarms的页面中加载

the control on the alarms page look like this alarms页面上的控件如下所示

     <alarm:SubscriptionPanel ID="pnlSubscription" runat="server" />

what iam trying to do is passing value from page alarms to the control hidden fields and there is a function at the control code behind that reads the hidden fields values 我想要做的是将值从页面alarms传递到control hidden fields并且后面的控制代码中有一个函数可以读取隐藏字段值

Question is how can i pass javascript values to hidden field in controls on page load 问题是我如何将javascript值传递给on page load控件中的隐藏字段

thanks in advance 提前致谢

You can use JQuery for it like this 您可以像这样使用JQuery

Example : 示例:

$("input[type=hidden][id='<%=tabTitle.ClientID%>']").val("Hello World"); 
$("input[type=hidden][id='<%=tabMenu.ClientID%>']").val("Hello World"); 

If you are using ASP.NET 4.0 your best bet is to set the ClientIDMode property on those controls to static and then simply use javascript to populate the hidden elements using plain ol' document.getElementById() . 如果您使用的是ASP.NET 4.0,最好的办法是将这些控件上的ClientIDMode属性设置为static,然后使用javascript使用普通的' document.getElementById()填充隐藏的元素。 Something like this: 像这样的东西:

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


//since the id mode in the hidden element is static; 
//you should be able to do this safely:
document.getElementById('tabTitle').value = myvalue;

If you are not on ASP.NET 4.0; 如果您不在ASP.NET 4.0上; jQuery will help here since you can find an element using partial matching as HatSoft showed you in his answer but with a slight difference: jQuery将在这里提供帮助,因为您可以使用部分匹配找到一个元素,因为HatSoft在他的答案中向您展示但有一点点差异:

$("input[type=hidden][id*='tabTitle']").val("Hello World");

Note the id*= part. 注意id*= part。 This gets all input elements whose ids contain the word tabTitle 这将获取其id包含单词tabTitle所有输入元素

Besides the approach commented by @Icarus, you could expose a JavaScript function from your control. 除了@Icarus评论的方法之外,您还可以从控件中公开JavaScript函数。

The problem you would face if you use ClientIDMode=Static in that, you would be restricted to add only one alarm:SubscriptionPanel control to your page 如果您使用ClientIDMode=Static将遇到的问题,您将被限制为仅添加一个alarm:SubscriptionPanel控件到您的页面

If you are planning to use only one control on each page, then the easiest approach is the one commented by @Icarus , however I would consider it as a temporal approach 如果您计划在每个页面上只使用一个控件,那么最简单的方法是由@Icarus评论的方法 ,但我会将其视为一种时间方法

This alternative encapsulates the logic where it really belongs, inside the custom control: 这个替代方案在自定义控件中封装了它真正属于的逻辑:

Output 产量

在此输入图像描述

ASCX ASCX

<div id="<%: this.ClientID %>">
    <asp:HiddenField runat="server" ID="hidden1" Value="one" />
    <asp:HiddenField runat="server" ID="hidden2" />
    <asp:Button Text="Post me" runat="server" OnClick="postme_Click" />
    <asp:Label runat="server" ID="lbl"></asp:Label>
    <script type="text/javascript">
        $(function () {
            var myObj = {
                setHidden1: function (myValue) {
                    $("#<%: this.hidden1.ClientID %>").val(myValue);
                },
                getHidden1: function () {
                    return $("#<%: this.hidden1.ClientID %>").val();
                },
                helloWorld: function () {
                    alert("hellow world");
                }
            };
            $("#<%: this.ClientID %>").data("data", myObj);
        });
    </script>
</div>

ASCX code behind ASCX代码落后

    protected void postme_Click(object sender, EventArgs e)
    {
        this.lbl.Text = "Posted: " + this.hidden1.Value;
    }

ASPX ASPX

    <script type="text/javascript">
        $(function () {
            $("#myPageButton").click(function () {
                $("#<%: this.myControl.ClientID %>").data("data").setHidden1("plop");
                $("#<%: this.myControl2.ClientID %>").data("data").setHidden1("plop2");
            });
        });
    </script>
    <input type="button" id="myPageButton" value="Set Hidden value" />
    <uc1:EncapsulateJavaScriptLogicInUserControl ID="myControl" 
        runat="server" />
    <uc1:EncapsulateJavaScriptLogicInUserControl ID="myControl2" 
        runat="server" />
    <uc1:EncapsulateJavaScriptLogicInUserControl ID="myControl3" 
        runat="server" />

I just found another way, that looks even more object oriented, however, it requires you to use the Microsoft AJAX library. 我刚刚找到另一种方式,看起来更加面向对象,但是,它要求您使用Microsoft AJAX库。

ASCX ASCX

Change: $("#<%: this.ClientID %>").data("data", myObj); 更改: $("#<%: this.ClientID %>").data("data", myObj);

Into: $.extend($get("<%: this.ClientID %>"), myObj); 进入: $.extend($get("<%: this.ClientID %>"), myObj);

ASPX ASPX

Change: 更改:

$("#<%: this.myControl.ClientID %>").data("data").setHidden1("plop");
$("#<%: this.myControl2.ClientID %>").data("data").setHidden1("plop2");

Into: 成:

$get("<%: this.myControl.ClientID %>").setHidden1("plop");
$get("<%: this.myControl2.ClientID %>").setHidden1("plop2");

With this approach you remove the use of the .data jQuery function 使用此方法,您可以删除.data jQuery函数的使用

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

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