简体   繁体   English

回发期间如何在客户端设置标签样式

[英]How to keep Label style set in client-side during Postback

Asp.net 4.0 webform web application... I'm trying to wrap my head around this scenario and wanted to throw this out there to obtain an explanation. Asp.net 4.0 Webform Web应用程序...我试图绕过这种情况,并想把它扔在那里以获得解释。

On my page, I have an HTML table. 在我的页面上,我有一个HTML表。 In one of the cells, I have a checkbox that performs some functionality on the server-side when clicked. 在其中一个单元格中,我有一个复选框,单击该复选框可在服务器端执行某些功能。 This has been in place. 这已经到位。 What I am trying to do is to perform manipulation of controls (styling, disabling, etc.) when this checkbox is clicked and then continue with the server-side call. 我想做的是单击此复选框后执行对控件的操作(样式,禁用等),然后继续进行服务器端调用。

Here's a snipet: 这是一个片段:

<table>
        <tr>
            <td style="width:10%; color: #FFFFFF;">                    
            </td>
            <td style="width:15%; color: #FFFFFF;">         
            </td>
            <td class="TdAvailable" id="TdTeam" style="width:15%; color: #FFFFFF;" align="left">
                Team
            </td>
            <td class="TdAvailable" id="TdPrimaryContact"  style="width:15%; color: #FFFFFF;" align="left">
                Primary Contact
            </td>
            <td style="width:15%; color: #FFFFFF;" align="left">
                Status
            </td>
            <td style="width:15%; color: #FFFFFF;" align="left">
                Type
            </td>
        </tr>
    <tr>
        <td>
            <asp:CheckBox ID="chkShowFavorites" runat="server" Text="Show Favorites" 
                ForeColor="White" OnCheckedChanged="FavCheckChange" onclick="HighlightControls()"
                AutoPostBack="True" />                 
        </td>
     </tr>

...
...
...
</table>

javascript javascript

 <script type = "text/javascript">
     function HighlightControls() {
         document.getElementById("MainContent_TdTeam").style.color = "red";
     }
 </script>

This is going to be expanded to include more controls than this one but I'm just trying to see what my options are before developing too much. 这将被扩展为包括比此控件更多的控件,但是我只是想在开发过多控件之前先看看我的选择是什么。

During debugging I see the label turn red but then returns to white after postback. 在调试过程中,我看到标签变成红色,但回发后又恢复为白色。

How can I perform my styling in the client-side and keep the settings after postback? 如何在客户端执行样式并在回发后保留设置?

Any changes to your DOM elements (css classes, styles) will be lost between postbacks unless you find a way to persist it. 在回发之间,对DOM元素(css类,样式)所做的任何更改都将丢失,除非您找到持久化它的方法。 You could use a hidden field to store the elementID + the new class name or style so when you post back, your code can interrogate the hidden field and perform the necessary changes. 您可以使用隐藏字段来存储elementID +新的类名或样式,以便在回发时,您的代码可以查询隐藏字段并执行必要的更改。

The only thing that I can think of would be to interrogate the control state on the server side when the page is posted back and apply the same logic that you are applying client side. 我唯一想到的就是在页面回发时询问服务器端的控制状态,并应用与应用客户端相同的逻辑。 So if you have a checkbox that when checked changed a cell to be red, you will need to write some server side code to check the value of that check box and then change the color of the cell. 因此,如果您有一个复选框,当选中该复选框时会将一个单元格更改为红色,则需要编写一些服务器端代码来检查该复选框的值,然后更改该单元格的颜色。

Its either that or could you replace your postback with an ajax call and only update the parts of the form that need to be changed? 它是或者您可以用ajax调用替换回发邮件,而仅更新需要更改的表单部分?

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

相关问题 回发后如何从客户端保留值 - How to preserve the values from client-side after postback 在UpdatePanel中ASP.NET AJAX回发期间更新客户端javascript数组变量的最简单方法? - Simplest way to update a client-side javascript array variable during a ASP.NET AJAX postback in an UpdatePanel? 如何使客户端脚本在ASP.NET回发上执行? (来自UpdatePanel) - How do you get client-side script to execute on an ASP.NET postback? (from an UpdatePanel) 如何强制用户至少选中一个复选框,然后从客户端JavaScript回发? - How can I force the user to have at least one selected checkbox, and postback from client-side javascript? 没有回发的文本框上的ASP.NET客户端验证 - ASP.NET Client-side validation on textbox without postback 在客户端代码中执行回发后,RedirectUrl信息丢失 - RedirectUrl info lost after doing postback in client-side code 从客户端处理程序返回false不会阻止回发 - Returning false from Client-Side Handler does not Prevent Postback JavaScript:获取导致回发的控件名称[客户端] - JavaScript: Get the control name which caused postback [client-side] 客户端验证prvent通过JavaScript eval()手动回发 - Client-Side validation prvent Manually Postback by JavaScript eval() 客户端JS和ASP.NET回发 - Client-side JS and ASP.NET Postback
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM