asp.net文本框。文本和jquery.val()

时间:2021-01-03 03:15:51

I'm reading the contents of a an asp.net TextBox on a button click event in the codebehind of a webpage.

我正在阅读一个asp.net文本框的内容,在一个网页的代码后面的按钮点击事件。

This works fine if I type something into the box I can read whats in there via TextBox.Text.

如果我把东西输入框中,我可以通过TextBox.Text读取其中的内容。

However, if I copy into the input textbox using jquery, setting the contents using val(), I can see the text appear in the box but when the click event fires and I try to read the contents of the textbox it is always blank. There's only every anything in there if I type it in myself.

但是,如果我使用jquery复制到输入文本框中,使用val()来设置内容,我可以看到文本出现在框中,但是当单击事件触发时,我尝试读取文本框的内容,它始终是空白的。如果我自己输入的话,那里只有所有的东西。

The relevant bits of code are: - The input box

代码的相关部分是:-输入框。

<asp:TextBox runat="server" ID="deliveryAddress3" CssClass="required radius disabled sugestedAddressTargetCity bcity2" />

Javascript

Javascript

var bfields = ['.baddress', '.bcity', '.bcountry', '.bpostcode'];
    var dfields = ['.baddress2', '.bcity2', '.bcountry2', '.bpostcode2'];
for (var i in dfields) {
                    $(dfields[i]).prev('label').hide();
                    $(dfields[i]).val($(bfields[i]).val());
                    $(dfields[i]).attr('disabled', 'disabled');
                    $(dfields[i]).addClass('disabled');
                    $(dfields[i]).attr('disabled', 'disabled');

Code in button click method of codebehind: -

代码的按钮点击方法的代码:-。

customer.DelTown = deliveryAddress3.Text;

Whats going on here is that the customer can copy their address from one set of boxes to another. If they do this (by clicking a button) the ext shows up in the boxes but in the code behind is blank. However, if I type something in to those boxes it is available in the code behind.

这里的情况是,客户可以将他们的地址从一组框复制到另一组。如果他们这样做(点击一个按钮),ext会出现在方框中,但是后面的代码是空的。但是,如果我键入一些东西到那些框中,它就可以在后面的代码中找到。

1 个解决方案

#1


2  

I started a new ASP.net application and it works if you set the text box's value attribute using jQuery attr().

我启动了一个新的ASP.net应用程序,如果您使用jQuery attr()设置文本框的值属性,它就会起作用。

The script updates the value inside the textbox and the code behind is able to read it too.

脚本更新文本框内的值,后面的代码也可以读取。

Designer HTML (added the below to the default.aspx):

设计器HTML(添加到default.aspx):

<asp:TextBox runat="server" ID="MytextBox"></asp:TextBox>
<asp:Button runat="server" ID="MyButton" Text="Click Me"/>

Rendered HTML output:

呈现的HTML输出:

<input name="ctl00$MainContent$MytextBox" type="text" value="NewValue" id="MainContent_MytextBox">
<input type="submit" name="ctl00$MainContent$MyButton" value="Click Me" id="MainContent_MyButton">

Script:

脚本:

$("#MainContent_MytextBox").attr("value", "NewValue");

Code Behind from default.aspx.cs:

从default.aspx.cs背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    // Place Debugger here:
    if (MytextBox.Text == "NewValue")
    {
        // hurray
    }
}

#1


2  

I started a new ASP.net application and it works if you set the text box's value attribute using jQuery attr().

我启动了一个新的ASP.net应用程序,如果您使用jQuery attr()设置文本框的值属性,它就会起作用。

The script updates the value inside the textbox and the code behind is able to read it too.

脚本更新文本框内的值,后面的代码也可以读取。

Designer HTML (added the below to the default.aspx):

设计器HTML(添加到default.aspx):

<asp:TextBox runat="server" ID="MytextBox"></asp:TextBox>
<asp:Button runat="server" ID="MyButton" Text="Click Me"/>

Rendered HTML output:

呈现的HTML输出:

<input name="ctl00$MainContent$MytextBox" type="text" value="NewValue" id="MainContent_MytextBox">
<input type="submit" name="ctl00$MainContent$MyButton" value="Click Me" id="MainContent_MyButton">

Script:

脚本:

$("#MainContent_MytextBox").attr("value", "NewValue");

Code Behind from default.aspx.cs:

从default.aspx.cs背后的代码:

protected void Page_Load(object sender, EventArgs e)
{
    // Place Debugger here:
    if (MytextBox.Text == "NewValue")
    {
        // hurray
    }
}