【问题标题】:Changing the color of a texbox with a table cell VB ASP.Net使用表格单元格 VBA ASP.Net 更改文本框的颜色
【发布时间】:2015-08-04 16:06:03
【问题描述】:

我在表格的单元格中创建了一个文本框,我正在使用它来获取用户的输入并将其显示在网页上。但是,当用户在文本框内部单击时,我无法更改文本框的背景,通常我会为此使用简单的 CSS,但由于文本框位于表格单元格内,我似乎无法弄清楚如何做到这一点.关于如何解决这个问题的任何想法?

我有以下 VB 代码来生成和填充我的表。

VB

 Protected Sub GenerateTable(noOfRows As Integer)
    Dim table As Table
    Dim row As TableRow
    Dim cell As TableCell
    Dim tb As TextBox
    Dim lbl As Label
    table = Table1
    table.ID = "Table1"

    For i As Integer = 1 To noOfRows Step 1
        row = New TableRow()
        For j As Integer = 0 To 1 Step 1
            cell = New TableCell()
            If j = 1 Then
                tb = New TextBox()
                tb.ID = "TextBoxRow_" & i & "Col_" & j
                cell.Controls.Add(tb)

            ElseIf j = 0 Then
                lbl = New Label()
                lbl.ID = "Label" & i
                lbl.Text = "Volume " & i
                cell.Controls.Add(lbl)
            End If
            row.Cells.Add(cell)
        Next
        table.Rows.Add(row)
    Next
    ViewState("RowsCount") = noOfRows
    Session("RowsCount") = noOfRows
End Sub

HTML

<tr>
    <td colspan="2">
        <asp:Table class="Table1" ID="Table1" runat="server"></asp:Table>

        <br />
            <asp:Button ID="btnAddVolume" class="btnVolumeBreak" runat="server"  Text="Add Volume Break"/>
    </td>
</tr>

【问题讨论】:

    标签: html asp.net vb.net


    【解决方案1】:

    首先为 Focused 和 UnFocused TextBox 添加 CSS 类:

    <style type="text/css">
        .UnFocusedCssClass
        {
            background-color: #FFFFFF;
        }
        .FocusedCssClass
        {
            background-color: #FF0000;
        }
    </style>
    

    接下来将此 Javascript 添加到您的 ASPX/ASCX 标记中:

    <script language="javascript" type="text/javascript">
        function UnFocus(targetControl) {
            targetControl.className = 'UnFocusedCssClass';
        }
    
        function Focus(targetControl) {
            targetControl.className = 'FocusedCssClass';
        }
    </script>
    

    最后,使用 Attributes 属性将 Javascript 连接到您添加到单元格的文本框:

    tb.Attributes.Add("onFocus", "Focus(this);")
    tb.Attributes.Add("onBlur", "UnFocus(this);")
    cell.Controls.Add(tb)
    

    【讨论】:

      猜你喜欢
      • 2013-10-18
      • 1970-01-01
      • 2016-02-11
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 2015-01-24
      相关资源
      最近更新 更多