在JS中也无法更改ASP复选框边框颜色。

发布于 2025-02-06 07:34:39 字数 2892 浏览 1 评论 0原文

我在ASPX页面中有一个嵌套到Telerik Radgrid中的ASP复选框。

检查此复选框/未选中时,它假设在后面的代码中执行操作。正常工作。但是,我的客户要求它更改颜色两次。单击时首先为红色,然后在措施结束时为绿色。

我有点想知道如何做到这一点,但是在我的测试代码中,它无法正常工作。我将其颜色更改为红色,然后弹出警报,然后将颜色更改为绿色。问题是在警报之前没有更改颜色。

我想这是一个控制刷新问题。如果是窗口表格,我可以使用方法刷新或更新来强制它,但是在Web表单中,我不知道是否可能。

这是我ASP的相关部分:

<telerik:RadAjaxManagerProxy ID="radajaxmanager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="chkSave">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="chkSave" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>

<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowAutomaticDeletes="true" AllowMultiRowSelection="True" >

        <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Caption="List" DataKeyNames="Id">
            <Columns>
                <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Price" HeaderText="Price" UniqueName="Price"></telerik:GridBoundColumn>

                <telerik:GridTemplateColumn HeaderText="Save this"> 
                    <ItemTemplate> 
                        <asp:CheckBox ID="chkSave" runat="server" autoPostBack="true" OnCheckedChanged="chkSave_CheckedChanged" />
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
               
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

这是我的代码:

Protected Sub chkSave_CheckedChanged(sender As Object, e As EventArgs)

    Dim chk As CheckBox = CType(sender, CheckBox)
    Dim status As Boolean = chk.Checked
    Dim item As GridDataItem = CType(chk.NamingContainer, GridDataItem)
    Dim keyvalue As String = item.GetDataKeyValue("Id").ToString()

    chk.BorderStyle = BorderStyle.Solid
    chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000")
    ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "MyScript", "alert('Changed');", True)
    chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#00FF00")    

End Sub

我也尝试在JavaScript中进行操作,但同样行不通:

ASP:

<asp:CheckBox ID="chkSave" runat="server" autoPostBack="true" OnCheckedChanged="chkSave_CheckedChanged" onChanged="colorMe(this);" />  

<javascript>
function colorMe (me) {
me.style.borderColor = "red";
}
</javascript>

创意?

I have an ASP checkbox nested into a Telerik RadGrid in an ASPX page.

When this checkbox is checked/unchecked it suppose to perform an action in code behind. It's working fine. However my client required that it changes the color twice. First to red when it is clicked and then to green when the action is concluded.

I kind of figured how to do this but in my test code it's not working as supposed. I changed its color to red, then popped an alert and then changed the color to green. The problem is that the color is not being changed BEFORE the alert.

I guess it's a control refresh issue. If it was a Windows form I could force it by using the methods Refresh or Update, but in a Web form I don't know if it is even possible.

This is the relevant part of my ASP:

<telerik:RadAjaxManagerProxy ID="radajaxmanager1" runat="server">
        <AjaxSettings>
            <telerik:AjaxSetting AjaxControlID="chkSave">
                <UpdatedControls>
                    <telerik:AjaxUpdatedControl ControlID="chkSave" />
                </UpdatedControls>
            </telerik:AjaxSetting>
        </AjaxSettings>
    </telerik:RadAjaxManagerProxy>

<telerik:RadGrid ID="RadGrid1" runat="server" DataSourceID="SqlDataSource1" AllowAutomaticDeletes="true" AllowMultiRowSelection="True" >

        <MasterTableView AutoGenerateColumns="False" DataSourceID="SqlDataSource1" Caption="List" DataKeyNames="Id">
            <Columns>
                <telerik:GridBoundColumn DataField="Id" HeaderText="Id" UniqueName="Id"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Description" HeaderText="Description" UniqueName="Description"></telerik:GridBoundColumn>
                <telerik:GridBoundColumn DataField="Price" HeaderText="Price" UniqueName="Price"></telerik:GridBoundColumn>

                <telerik:GridTemplateColumn HeaderText="Save this"> 
                    <ItemTemplate> 
                        <asp:CheckBox ID="chkSave" runat="server" autoPostBack="true" OnCheckedChanged="chkSave_CheckedChanged" />
                    </ItemTemplate> 
                </telerik:GridTemplateColumn> 
               
            </Columns>
        </MasterTableView>
    </telerik:RadGrid>

And this is my code behind:

Protected Sub chkSave_CheckedChanged(sender As Object, e As EventArgs)

    Dim chk As CheckBox = CType(sender, CheckBox)
    Dim status As Boolean = chk.Checked
    Dim item As GridDataItem = CType(chk.NamingContainer, GridDataItem)
    Dim keyvalue As String = item.GetDataKeyValue("Id").ToString()

    chk.BorderStyle = BorderStyle.Solid
    chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000")
    ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "MyScript", "alert('Changed');", True)
    chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#00FF00")    

End Sub

I tried doing it in Javascript too but again it didn't work:

ASP:

<asp:CheckBox ID="chkSave" runat="server" autoPostBack="true" OnCheckedChanged="chkSave_CheckedChanged" onChanged="colorMe(this);" />  

<javascript>
function colorMe (me) {
me.style.borderColor = "red";
}
</javascript>

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨落星ぅ辰 2025-02-13 07:34:39

据我

<script type="text/javascript">
   function colorMe (me) {
   me.style.borderColor = "red";
   }
</script>

所知将发送到浏览器,当您使用registerclientscript时,服务器在页面中创建脚本标签,但是它没有立即执行,因为页面仍在服务器中,然后将页面发送到浏览器,浏览器将接收并渲染页面并执行服务器放置的脚本。当您第一次设置边框颜色时,您将红色设置为红色,当您在另一行中设置其他颜色时,您只是用绿色替换了红色的值,因此服务器发送了带有绿色边框的复选框,从未发送红色边框到浏览器。

chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000")
ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "MyScript", "alert('Changed');", True)
chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#00FF00") 

As far as i know the javascript tag doesn't exist... you have to use:

<script type="text/javascript">
   function colorMe (me) {
   me.style.borderColor = "red";
   }
</script>

Other thing i believe is worth mentioning, all of the code below executes in the server, it creates the page that is going to be sent to the browser, when you use registerclientscript, the server creates a script tag in the page, but it's not executed right away, because the page is still in the server, then the page is sent to the browser, the browser will receive and render the page and execute the script that the server put. When you set the border color the first time, you set red, and when you set other color in the other line, you just replaced the value red with green, so the server sent the checkbox with green border, the red border never was sent to the browser.

chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#FF0000")
ScriptManager.RegisterClientScriptBlock(Page, Me.GetType(), "MyScript", "alert('Changed');", True)
chk.BorderColor = System.Drawing.ColorTranslator.FromHtml("#00FF00") 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文