vb.net 被 javascript 更改时回发丢失的值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19438119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 15:31:41  来源:igfitidea点击:

Values lost on postback when changed by javascript

javascriptjqueryhtmlasp.netvb.net

提问by brad

I'm changing the ImageUrl of ImageButtons via Javascript when the user mouses over. On submit I want to inspect the ImageUrl property. However, the changed property is not reflected in the code behind. In fact, I'm also updating a span tag via javascript and its changed value is also not reflected on post back. These are all static html elements. I'm not creating any of these dynamically and am not doing any manipulation to them in the code behind.

当用户鼠标悬停时,我正在通过 Javascript 更改 ImageButtons 的 ImageUrl。提交时,我想检查 ImageUrl 属性。但是,更改后的属性并未反映在后面的代码中。事实上,我也在通过 javascript 更新 span 标签,其更改的值也不会反映在回发中。这些都是静态的 html 元素。我没有动态创建任何这些,也没有在后面的代码中对它们进行任何操作。

HTML:

HTML:

            <div style="margin: 20px 0 0 0; float: left;">
        <div class="divStars1" style="width: 130px; float: left;">
            <input type="image" name="ctl00$MainContent$ax1" id="MainContent_ax1" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$ax2" id="MainContent_ax2" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$ax3" id="MainContent_ax3" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$ax4" id="MainContent_ax4" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$ax5" id="MainContent_ax5" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />

        </div>
        <div style="margin-top: 3px; width: 600px; float: left;">
            <span>axs</span>
        </div>
    </div>
    <div style="margin: 20px 0 0 0; float: left;">
        <div class="divStars2" style="width: 130px; float: left;">
            <input type="image" name="ctl00$MainContent$bx1" id="MainContent_bx1" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$bx2" id="MainContent_bx2" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$bx3" id="MainContent_bx3" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$bx4" id="MainContent_bx4" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
            <input type="image" name="ctl00$MainContent$bx5" id="MainContent_bx5" class="stars" onmouseover="<code to flip image>" src="style/EmptyStar.png" style="height:20px;width:20px;" />
        </div>
        <div style="margin-top: 3px; width: 600px; float: left;">
            <span>bx blah blah</span>
        </div>
    </div>

Javascript:

Javascript:

        $(document).on("mouseover", "input.stars", function () {
        var score = 0;
        $("input.stars").each(function (index, element) {
            // element == this
            if ($(element).attr("src") == "style/FilledStar.png") {
                score = score + 1;
            };
        });
        score = score / 5;
        var n = score.toFixed(2);
        $("#MainContent_lblScore").text(n);
    });

btnSubmit_Click code:

btnSubmit_Click 代码:

        For Each ctrl As Control In Me.divSkill1.Controls
        If (TypeOf ctrl Is ImageButton) Then
            Dim imgCtl As ImageButton = TryCast(ctrl, ImageButton)
            Dim x As String = imgCtl.ImageUrl 'HERE'S THE PROBLEM... x always equals original static value!
        End If

    Next

回答by Sandcar

The server does not know anything about changes on the client side for this type of controls. Possibly you will have to store the values ??that have been changed by javascript in hidden field or hidden textbox and then collect this value on the server side.

服务器对此类控件的客户端更改一无所知。可能您必须将已被 javascript 更改的值存储在隐藏字段或隐藏文本框中,然后在服务器端收集此值。

Hope this helps

希望这可以帮助

Edit:

编辑:

If you only want to get the value of var n , you could do something like this:

如果您只想获取 var n 的值,您可以执行以下操作:

// aspx;

// aspx;

<asp:HiddenField ID="HidScore" runat="server" />

/// javascript

/// javascript

$(document).on("mouseover", "input.stars", function () {
    var hid=$("#<%= HidScore.ClientID %>");


        var score = 0;
        $("input.stars").each(function (index, element) {
            // element == this
            if ($(element).attr("src") == "style/FilledStar.png") {
                score = score + 1;
            };
        });
        score = score / 5;
        var n = score.toFixed(2);
        $("#MainContent_lblScore").text(n);

    hid.val(n);
    });

and then in vb.net just grab the value : HidScore.value

然后在 vb.net 中获取值:HidScore.value

回答by andleer

Client side changes to server side controls will generally not be preserved or posted back to the server.

客户端对服务器端控件的更改通常不会被保留或回发到服务器。

Consider creating a hidden field <asp:HiddenField />control and have your JavaScript also updates that value. HiddenFields/ <input type="hidden" />will be returned to the server on a PostBack. Your code will then need to inspect the HiddenFieldfor possible changes or updates and route those changes back to the ImageButtonImageUrlproperty.

考虑创建一个隐藏字段<asp:HiddenField />控件并让您的 JavaScript 也更新该值。HiddenFields/<input type="hidden" />将在 PostBack 上返回到服务器。然后,您的代码需要检查HiddenField可能的更改或更新,并将这些更改路由回ImageButtonImageUrl属性。