使用表格单元格VB ASP.Net更改texbox的颜色

时间:2015-08-04 16:06:03

标签: html asp.net vb.net

我在表格的单元格中创建了一个文本框,我使用它来从用户那里获取输入并将其显示在网页上。然而,当用户在文本框中单击时,我无法更改文本框的背景,通常我会使用简单的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>

1 个答案:

答案 0 :(得分:1)

首先为聚焦和未聚焦的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)