简体   繁体   English

如何单击一次然后切换并确保切换的 CSS 在加载表格后出现?

[英]how to make on click once and then toggle and make sure the CSS of the toggle comes after the table being loaded?

Hi i would like to ask how to draw the table once and then toggle the table.嗨我想问一下如何绘制一次表格然后切换表格。 Besides that, the toggleSetting's css will only load after the table is done drawn/loaded.除此之外,toggleSetting 的 css 只会在表格完成绘制/加载后加载。

PS: i'm quite new to javascript and please be forgiving, thank you and also please ignore my terrible english. PS:我对 javascript 很陌生,请原谅,谢谢,也请忽略我糟糕的英语。

here is my toggle function这是我的拨动开关 function

    function toggleSetting(setting, ctrl) {

        var stt = document.getElementById(setting);
        if (stt.style.display == 'none') {
            stt.style.display = '';
            ctrl.className = "accordion2";
        }
        else {
            stt.style.display = 'none';
            ctrl.className = "accordion";
        }           
    }

here is my code for the javascript function这是我的 javascript function 的代码

        $("#tableAOnce").one("click", (function () {
            
            $.ajax({
                type: "POST",
                url: "AccInfo.ashx",
                success: function (output) {
                    try {
                        output = JSON.parse(output);
                        DrawTable(output);
                        toggleSetting('toggleSBook', this); 
                        //alert(output);

                    }
                    catch (ex) {
                        alert("error");
                        $('#tableA').empty();
                    }
                }
                , complete: function (data) {
                    
                    
                }
            });

the table in the DrawTable function is a table drawn using stringbuilder and append. DrawTable function 中的表格是使用 stringbuilder 和 append 绘制的表格。

function DrawTable(output){
            var general = output;
            var sb = new StringBuilder();
            sb.append("<table>");
            sb.append("<tr>");
            sb.append("<td>");
            sb.append("</td>");
            sb.append("</tr >");
            sb.append("</table>");
            $('#tableA').empty().html(sb.toString());
}

this is my code for the main table这是我的主表代码

        <table>
            <tr>
                <td>
                    <table id="tableAOnce" class="accordion" onclick="toggleSetting('toggleTab',this)" >
                    <tr>
                        <td align="left">Table1</td>
                    </tr>
                    </table>
                </td>
            </tr>
            <tr style="display:none;" id="toggleTab" >
                <td>
                    <table id="tableA"></table>
                </td>
            </tr>
        </table>

I've tried to using the toggleSetting function in complete:function(data) but it wont work.我尝试在 complete:function(data) 中使用 toggleSetting function 但它不起作用。

First, you did seem to leave out the web method directive on your general function.首先,您似乎确实在您的一般 function 上遗漏了 web 方法指令。 And your ajax call should thus include the web method.因此,您的 ajax 调用应包括 web 方法。 eg:例如:

[WebMethod]  
Public static function toggleSetting(setting, ctrl) {

But a ajax call means your code behind will run just fine, but you can't update any control attributes because the web page has not been posted/sent to the server.但是 ajax 调用意味着您的代码可以正常运行,但是您无法更新任何控件属性,因为 web 页面尚未发布/发送到服务器。 So the web page and controls remains sitting on users desktop.因此 web 页面和控件仍然位于用户桌面上。

You can call your server code, but it can't modify controls since the whole web page has not been sent up to the server (it is still sitting in the browser).您可以调用您的服务器代码,但它不能修改控件,因为整个 web 页面尚未发送到服务器(它仍然位于浏览器中)。

Your current code setup thus needs a post back of the page.因此,您当前的代码设置需要回发页面。 Or at the VERY least the part of the page with the controls you are setting.或者至少是您正在设置的控件的页面部分。 This suggest you might as well use a update panel, and your button code is not ajax or JS anymore.这建议您最好使用更新面板,并且您的按钮代码不再是 ajax 或 JS。 So, you could place regular asp button that posts back the pack.因此,您可以放置回发包的常规 asp 按钮。 And update panel means a partial post back - so both the button and table have to be inside of that up-date panel for this to work.更新面板意味着部分回发 - 因此按钮和表格都必须位于该更新面板内才能正常工作。 That means the page and a post back occurs, but ONLY what is inside the update panel goes up to the server.这意味着页面和回发发生,但只有更新面板内的内容才会发送到服务器。

The other way?另一种方法? Don't call the server.不要打电话给服务器。 Do this client side as js, and then you don't need or have to call server side code.将此客户端作为js,然后您不需要或必须调用服务器端代码。 And thus you not need any post back (full or partial with a update panel).因此,您不需要任何回发(全部或部分带有更新面板)。 So, I would consider writing the function in js, and not call the code behind (server side).所以,我会考虑用js写function,而不是调用后面的代码(服务器端)。 However, I unfortunately find js somewhat hard to work with, but 100% client side code would be best.然而,不幸的是,我发现 js 有点难以使用,但最好是 100% 的客户端代码。 Eventually, if you going to run any code behind after a hide/show of the given control (client side), then eventually if ANY code is to run server side and set or work with those controls, then a post-back will be required.最终,如果您要在隐藏/显示给定控件(客户端)之后运行任何代码,那么最终如果任何代码要运行服务器端并设置或使用这些控件,则将需要回发.

And your ajax post needs to include the web method in the url eg:并且您的 ajax 帖子需要在 url 中包含 web 方法,例如:

        $.ajax({
            type: "POST",
            url: "AccInfo.ashx/toggleSetting",
            data: {setting: "some value",
                  ctrl:  "some value"}

note VERY VERY careful in above how in the above json string the names are to be the SAME as the parameters you have for the server side function.注意上面非常非常小心,在上面的 json 字符串中,名称与服务器端 function 的参数相同。

But, without a post back?但是,没有回帖? The settings will occur server side, but any post back from the client side will send up the whole web page in its current state, and the changed values of the controls server side will be over-written.设置将发生在服务器端,但从客户端返回的任何回发都将在其当前 state 中发送整个 web 页面,并且控制服务器端的更改值将被覆盖。 So best to think that you have a copy of the web page sitting client side, and then one sitting server side.所以最好认为你有一个 web 页面的副本坐在客户端,然后是一个坐在服务器端。 Changes on the server side copy of the web page will get over written by any post back. web 页面的服务器端副本上的更改将被任何回发覆盖。 And changes server side will NOT appear until such time that the server side copy of the web page is sent back down to the client.并且更改服务器端不会出现,直到 web 页面的服务器端副本被发送回客户端。

You have: Client side web page.您有:客户端 web 页面。 Change things, controls etc. client side.更改事物,控制等客户端。 post-back.回发。 The web page is sent to server. web 页面被发送到服务器。 Code behind runs (maybe update web page), and then the WHOLE page is sent back down to the browser.代码隐藏运行(可能更新 web 页面),然后整个页面被发送回浏览器。 So, you can't run server side code that updates controls unless te web page is sent up to the server.因此,除非将 web 页面发送到服务器,否则您无法运行更新控件的服务器端代码。 If you break this rule, then you have changes being made client side and server side - but the two pages are out of sync now.如果您违反此规则,那么您将在客户端和服务器端进行更改 - 但是这两个页面现在不同步。

Change the style attributes 100% client side.更改样式属性 100% 客户端。 So in js, you can do the same as your c# server side code with something like this:因此,在 js 中,您可以执行与 c# 服务器端代码相同的操作,如下所示:

function ShowUpload() {
               var JUpload = document.getElementById('<%=UploadFiles.ClientID%>')
               JUpload.style.display = "inline";
               return false;
           }

With jquery, then you can go:使用 jquery,则可以 go:

        function mysetter(ctrl, setting, value) {
            $('#' + ctrl).css(setting, value);
        }

And thus now set any style setting, say like:因此现在设置任何样式设置,例如:

        mysetter('Button1', 'display', 'none');

And, because hide and show is oh so very common?而且,因为隐藏和显示非常普遍?

You can use this:你可以使用这个:

 $('#MyControl').hide();

or或者

 $('#MyControl').hide();

Note that hide() and show() actually sets the style display to "none" or "normal".请注意, hide() 和 show() 实际上将样式显示设置为“无”或“正常”。

So, I would would not do a ajax call to set/hide the controls UNLESS you using a full asp.net button and a full postback.因此,除非您使用完整的 asp.net 按钮和完整的回发,否则我不会调用 ajax 来设置/隐藏控件。 Or put the buttons/controls and the asp.net button inside of a update panel, and you also not get a full page post back.或者将按钮/控件和 asp.net 按钮放在更新面板内,您也不会得到整页的回复。

Given that you just hiding and showing a button or control?鉴于您只是隐藏和显示按钮或控件? I think this can with relative ease be handled with 100% client side browser code.我认为这可以通过 100% 客户端浏览器代码相对轻松地处理。

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

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