为什么在回发时InnerHtml,InnerText和属性值为空?

时间:2013-03-01 10:13:51

标签: asp.net webforms

我的页面上有一些具有值属性的跨度。

当我尝试从后面的代码中获取这些属性时,我只得到前两个 - 其余的都是null,即使值属性存在且不为空。

Howcome?这是我的标记:

<div class="ExtraContainer">
    <div class="box_slider">
    <form id="Form1" runat="server">
        <div class="block block1">
            <h3>
                Vælg områder</h3>
            <div class="wraper">
                <input id="chkAllAreas" type="radio" name="group1" class="input_radio" runat="server"/>
                <label for="chkAllAreas">
                    Vis alle områder</label>
            </div>
            <div class="wraper">
                <input id="chkCustomAreas" type="radio" name="group1" class="input_radio" runat="server"/>
                <label for="chkCustomAreas">
                    Vælg områder</label>
            </div>
        </div>
        <div class="block block2">
            <h3>
                Vælg Ledighed</h3>
            <div class="wraper">
                <input id="chkShowAll" type="radio" name="group2" class="input_radio" runat="server" />
                <label for="chkShowAll">
                    Vis alle boliger</label>
            </div>
            <div class="wraper">
                <input id="chkIsAvailable" type="radio" name="group2" class="input_radio" runat="server" />
                <label for="chkIsAvailable">
                    Vis kun ledige boliger</label>
            </div>
        </div>
        <div class="block block3">
            <h3>
                Vælg boligtype</h3>
            <div class="wraper go_left">
                <input id="chkAllTHousingTypes" type="checkbox" class="input_check" runat="server" />
                <label for="chkAllTHousingTypes">
                    Vis alle boligtyper</label>
            </div>
            <div class="wraper go_left">
                <input id="chkFamilieboliger" type="checkbox" class="input_check" runat="server" />
                <label for="chkFamilieboliger">
                    Familieboliger</label>
            </div>
            <div class="wraper go_left">
                <input id="chkUngdomsboliger" type="checkbox" class="input_check" runat="server" />
                <label for="chkUngdomsboliger">
                    Ungdoms- &amp; studieboliger</label>
            </div>
            <div class="wraper go_left">
                <input id="chkAeldrebolig" type="checkbox" class="input_check" runat="server" />
                <label for="chkAeldrebolig">
                    Senior- &amp; ældreboliger</label>
            </div>
        </div>
        <div class="clear">
            &nbsp;</div>
        <div class="line">
            &nbsp;</div>
        <div class="block4 go_left">
            <h3>
                Vælg økonomi</h3>
            <div class="slider_info">
                <div class="slider_info_leftright_container">
                    <div class="slider_info_left">
                        <span>Husleje</span>
                    </div>
                    <div class="slider_info_right" style="float: right">
                        <span id="huslejefra" runat="server"></span> <em>til</em> <span id="huslejetil" runat="server"></span><span>kr</span>
                    </div>
                </div>
                <div class="Slider">
                    <div id="husleje-slider-range">
                    </div>
                </div>
            </div>
            <div class="slider_info">
                <div class="slider_info_leftright_container">
                    <div class="slider_info_left">
                        <span>Indskud</span>
                    </div>
                    <div class="slider_info_right" style="float: right">
                        <span id="indskudfra" runat="server"></span> <em>til</em> <span id="indskudtil" runat="server"></span><span>m2</span>
                    </div>
                    <div class="Slider">
                        <div id="indskud-slider-range">
                        </div>
                    </div>
                </div>
            </div>
        </div>
    <div class="block4 go_right">
        <h3>
            Vælg størrelse</h3>
        <div class="slider_info">
            <div class="slider_info_leftright_container">
                <div class="slider_info_left">
                    <span>Antal værelser</span>
                </div>
                <div class="slider_info_right" style="float: right">
                    <span id="rumfra" runat="server">3</span> <em>til</em> <span id="rumtil"
                        runat="server">6</span><span>rum</span>
                </div>
            </div>
            <div class="Slider">
                <div id="rum-slider-range">
                </div>
            </div>
        </div>
        <div class="slider_info">
            <div class="slider_info_leftright_container">
                <div class="slider_info_left">
                    <span>m2</span>
                </div>
                <div class="slider_info_right" style="float: right">
                    <span id="storrelsefra" value="2" runat="server"></span> <em>til</em> <span id="storrelsetil" runat="server"></span><span>m2</span>
                </div>
            </div>
            <div class="Slider">
                <div id="storrelse-slider-range">
                </div>
            </div>
        </div>
    </div>
    <asp:Button ID="searchButton" OnClick="ImgSearch_Click" Text="Søg" runat="server" />
    </form>

    <div class="clear">
        &nbsp;</div>            


</div>
    <div class="block5">
        <h3>
            Vælg resultatvisning</h3>
            <div class="buttonswrapper">
        <div class="wraper">
            <input id="id9" type="radio" class="input_radio" />
            <label for="id9">
                Billedevisning</label>
        </div>
        <div class="wraper">
            <input id="id10" type="radio" class="input_radio" />
            <label for="id10">
                Kortvisning</label>
        </div>
        </div>
        <a class="link_img resetBtn" href="#">
            <img src="style/images/select_img.png" alt="" />Nulstil søgningen</a>
    </div>

    <div class="clear">&nbsp;</div>

<input id="HDSelectedAreas" type="hidden" runat="server" class="HSelectedArea" />
</div>

搜索按钮在aspx中如下所示:

<asp:Button ID="searchButton" OnClick="ImgSearch_Click" Text="Søg" runat="server" />

这就是背后的代码:

 protected void ImgSearch_Click(object sender, EventArgs e)
        {

            String cRoomsFrom = rumfra.Attributes["value"];
            String cRoomsTo = rumtil.Attributes["value"];
            String cSizeFrom = storrelsefra.Attributes["value"];
            String cSizeTo = storrelsetil.Attributes["value"];
            String cRentFrom = huslejefra.Attributes["value"];
            String cRentTo = huslejetil.Attributes["value"];
            String cDepositFrom = indskudfra.Attributes["value"];
            String cDepositTo = indskudtil.Attributes["value"];
}

初始化值:

<script type="text/javascript" language="javascript">

        function setupSlider(min, max, step, values, sliderElement, minelement, maxelement) {

            $(sliderElement).slider({
                range: true,
                min: min,
                max: max,
                values: values,
                step: step,
                slide: function (event, ui) {
                    $(minelement).html(ui.values[0]);
                    $(maxelement).html(ui.values[1]);
                    $(minelement).attr('value', ui.values[0]);
                    $(maxelement).attr('value', ui.values[1]);
                    __doPostBack();
                }
            });

            $(minelement).html($(sliderElement).slider("values", 0));
            $(maxelement).html($(sliderElement).slider("values", 1));
            $(minelement).attr('value', $(sliderElement).slider("values", 0));
            $(maxelement).attr('value', $(sliderElement).slider("values", 1));
        }

        function initializeSliders() 
        {
            // størrelse slider
            var values = [10, 80];
            setupSlider(10, 80, 10, values, "#storrelse-slider-range", "#storrelsefra", "#storrelsetil");

            // husleje slider
            values = [1000, 7000];
            setupSlider(1000, 12000, 1000, values, "#husleje-slider-range", "#huslejefra", "#huslejetil");

            /// rum
            values = [3, 6];
            setupSlider(1, 8, 1, values, "#rum-slider-range", "#rumfra", "#rumtil");


            // indskud
            values = [3000, 7000];
            setupSlider(1000, 10000, 1000, values, "#indskud-slider-range", "#indskudfra", "#indskudtil");

            //fix color 
            fixToolTipColor();

        }

        $(document).ready(function () {
            $('.Udvidet').live('click', function () {
                $('.UdvidetSoegning').toggle('slow');
            });
            $(function () {
                initializeSliders();
                $(".resetBtn").live("click", function () {
                    initializeSliders();
                    __doPostBack();
                });

            });
        });
    </script>

我尝试更改为InnerHtmlInnerText,但结果是一样的。有时value属性为null,但InnerHtml或InnerText返回实际值:S

1 个答案:

答案 0 :(得分:2)

在回帖后,只发布input个元素。

所以他们和回帖之前是一样的,他们在回帖之前就有了,现在和现在。所以实际上你没有“得到”这个参数隐藏在代码背后,你只是“设置”它们,并且在viewstate on的服务器控件上,你可以记住这个参数在post back - 但是您无法在客户端更改它们并期望在服务器上读取此更改。

这里的解决方法是。以太网使用页面的视图状态来保存一些值并将它们保留在后面,以太使用输入隐藏的元素将它们放回到帖子后面。