简体   繁体   English

如何在Jquery Asp.net中查找UserControls控件值?

[英]How to Find UserControls Control value in Jquery Asp.net?

I am using jQuery and ASP.net 我正在使用jQuery和ASP.net

I have a User Control and wanted to Set the values of this User control to the database, for that I need the user control's value. 我有一个用户控件,并想将此用户控件的值设置到数据库中,因为我需要用户控件的值。 In the same way, I want to show the data from the database in the user control, for that I need to Get the values by jquery. 同样,我想在用户控件中显示数据库中的数据,为此,我需要通过jquery获取值。

In my user control I have 4 TextBoxes and 2 Buttons (SET/UPDATE) 在我的用户控件中,我有4个文本框和2个按钮(SET / UPDATE)

AutoCompleteSearch_New is ascx user control AutoCompleteSearch_New是ascx用户控件

Here is my tried code: 这是我尝试的代码:

var ID = $('#<%= ((HiddenField)AutoCompleteSearch_New.FindControl("hdnvalue")).ClientID %>').val();

But I dont wan't to use hidden fields. 但是我不想使用隐藏字段。

Can I directly find the control's value without using hidden fields? 我可以不使用隐藏字段直接找到控件的值吗?

It is similar to use value from a webform. 它类似于使用Web表单中的值。

Here is the code for the same. 这是相同的代码。

 Var TextBoxValue = $('#YourTextBoxID').val();

Inspect element and get the Textbox ID and replace it with YourTextBoxID . 检查元素并获取文本框ID并将其替换为YourTextBoxID

Or 要么

Var TextBoxValue = $('#<%= YourTextBoxID.ClientID').val();

where YourTextBoxID is your asp:Textbox ID. 其中, YourTextBoxID是您的asp:Textbox ID。

When ever you load user control in your aspx page, jquery consider it as a whole page which is combined of user control and rest of the aspx controls exist in the form. 每当您在aspx页面中加载用户控件时,jquery就会将其视为由用户控件和其余aspx控件组成的整个页面。 So you can directly get textbox value in your jquery 这样您就可以在jquery中直接获取textbox的值

you can do that by adding ClientIDMode="Static" to the Control 您可以通过向控件添加ClientIDMode =“ Static”来实现

and then use 然后使用

var txtvalue=  $("#TextBoxId").val();

Provided storing the values in Session is not an option, other options I see are : 如果在Session中存储值不是一个选项,我看到的其他选项是:

  • javascript variable declaration in the user control markup 用户控件标记中的javascript变量声明
  • client-side decryption of the view state 客户端对视图状态的解密
  • custom data attributes on html elements html元素上的自定义数据属性

I'm afraid HiddenField is your best option. 恐怕HiddenField是您最好的选择。 That's somewhat the same idea behind ViewState. 这与ViewState背后的想法有点相同。

If you don't want the client to tamper with the value, you may go for a combination of the value, and a hash of the value concatenated to a secret key ( value+separator+Hash(value+secretkey) ) 如果您不希望客户端篡改该值,则可以使用该值的组合,以及该值的散列连接到密钥(value + separator + Hash(value + secretkey))

If you don't want the client to access the value, you may rely on encrypting it in the HiddenField 如果您不希望客户端访问该值,则可以依靠在HiddenField中对其进行加密

hey u can directly find control of textbox or hiddenfield without mention its type 嘿,您可以直接找到textbox或hiddenfield的控件,而无需提及其类型

var value=$('AutoCompleteSearch_New_hdnvalue').val(); var value = $('AutoCompleteSearch_New_hdnvalue')。val();

here AutoCompleteSearch_New is the name of user control u load on page and 这里的AutoCompleteSearch_New是您在页面上加载的用户控件的名称,

hdnvalue is the id of hidden field in user control AutoCompleteSearch_New hdnvalue是用户控件AutoCompleteSearch_New中隐藏字段的ID。

enjoy.. :-) 请享用.. :-)

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

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