在gridview中显示标签而不是文本框 [英] Display label instead of textbox in gridview
本文介绍了在gridview中显示标签而不是文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
<ItemTemplate>
<asp:Label ID="lblItem" runat="server" Text='<%# Eval("Item") %>'
Width="100px" ></asp:Label>
<asp:TextBox ID="txtItem" runat="server" Text='<%# Eval("Item") %>'
MaxLength="4" visible="false"
onkeydown="CancelEdit(this);"></asp:TextBox>
</ItemTemplate>
'RowCommand代码
'RowCommand Code
Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
Dim _gridView As GridView = DirectCast(sender, GridView)
Select Case e.CommandName
Case ("UpdateCell")
Case ("DeleteRow")
'Valid item number - update row
Dim incLabel As Label = DirectCast(_gridView.Rows(0).FindControl("lblAutoNum"), Label)
Dim counter As Integer = Integer.Parse(incLabel.Text)
'Update data table
Dim dt As DataTable = _sampleData
Dim dr As DataRow = dt.Rows.Find(counter)
dr.BeginEdit()
dr.Delete()
dr.EndEdit()
' Save the updated DataTable
_sampleData = dt
' Clear the selected index to prevent
' another update on the next postback
_gridView.SelectedIndex = -1
' Repopulate the GridView
_gridView.DataSource = dt
_gridView.DataBind()
Case ("CancelEdit")
Dim _rowIndex As Integer = e.CommandSource.Parent.Parent.RowIndex
For i As Integer = _firstEditCellIndex To _gridView.Columns.Count - 1
If (i = 4) Or (i = 5) Then
If _gridView.Rows(_rowIndex).Cells(i).Controls(3).Visible Then
' Get the edit control for the selected cell and make it visible
Dim _editControl As Control = _gridView.Rows(_rowIndex).Cells(i).Controls(3)
_editControl.Visible = False
' Get the display control for the selected cell and make it invisible
Dim _displayControl As Control = _gridView.Rows(_rowIndex).Cells(i).Controls(1)
_displayControl.Visible = True
Exit Sub
End If
End If
Next
Case ("SingleClick")
Dim _rowIndex As Integer = Integer.Parse(e.CommandArgument.ToString())
Dim _columnIndex As Integer = Integer.Parse(Request.Form("__EVENTARGUMENT"))
'Hide image on row change
If (Me.itemImage.Visible) Then
If (_rowIndex <> _gridView.SelectedIndex) Then
Me.itemImage.Visible = False
End If
End If
' Set the Gridview selected index
_gridView.SelectedIndex = _rowIndex
' Bind the Gridview
_gridView.DataSource = _sampleData
_gridView.DataBind()
' Get the display control for the selected cell and make it invisible
Dim _displayControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(1)
_displayControl.Visible = False
' Get the edit control for the selected cell and make it visible
Dim _editControl As Control = _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls(3)
_editControl.Visible = True
' Get the validator control for the selected cell if it exists and make it visible
If _gridView.Rows(_rowIndex).Cells(_columnIndex).Controls.Count > 5 Then
Dim _validatorControl As Control = GridView1.Rows(_rowIndex).Cells(_columnIndex).Controls(5)
_validatorControl.Visible = True
End If
' Clear the attributes from the selected cell to remove the click event
_gridView.Rows(_rowIndex).Cells(_columnIndex).Attributes.Clear()
' Set focus on the selected edit control
ScriptManager.RegisterStartupScript(Me, [GetType](), "SetFocus", "document.getElementById('" & _editControl.ClientID & "').focus();", True)
' If the edit control is a dropdownlist set the
' SelectedValue to the value of the display control
If TypeOf _editControl Is DropDownList AndAlso TypeOf _displayControl Is Label Then
DirectCast(_editControl, DropDownList).SelectedValue = DirectCast(_displayControl, Label).Text
End If
' If the edit control is a textbox then select the text
If (TypeOf _editControl Is TextBox) And
(_editControl.ID = "txtRowHeader") Then
DirectCast(_editControl, TextBox).Attributes.Add("onfocus", "this.select()")
End If
' If the edit control is a checkbox set the
' Checked value to the value of the display control
If TypeOf _editControl Is CheckBox AndAlso TypeOf _displayControl Is Label Then
DirectCast(_editControl, CheckBox).Checked = Boolean.Parse(DirectCast(_displayControl, Label).Text)
End If
'If the edit control is a label set focus to cell
If (TypeOf _editControl Is Label) And _
(Not IsNullOrEmpty(DirectCast(_gridView.Rows(_rowIndex).Cells(4).FindControl("lblItem"), Label).Text)) And _
(_editControl.ID = "lblImage2") Then
DirectCast(_editControl, Label).Attributes.Add("onfocus", "this.select()")
Me.itemImage.Visible = True
Me.itemImage.ImageUrl = "~/Images/RedColorPencil.jpg"
End If
Exit Select
End Select
End Sub
'Render event
Protected Overloads Overrides Sub Render(ByVal writer As HtmlTextWriter)
' The client events for GridView1 were created in GridView1_RowDataBound
For Each r As GridViewRow In GridView1.Rows
If r.RowType = DataControlRowType.DataRow Then
For columnIndex As Integer = _firstEditCellIndex To r.Cells.Count - 1
'Item
If (columnIndex = 4) And (IsNullOrEmpty(DirectCast(r.Cells(4).FindControl("lblItem"), Label).Text)) Then
Page.ClientScript.RegisterForEventValidation(r.UniqueID & "$ctl00", columnIndex.ToString())
End If
'Image and Quantity
If ((columnIndex = 3)) And _
(Not IsNullOrEmpty(DirectCast(r.Cells(4).FindControl("lblItem"), Label).Text)) Then
Page.ClientScript.RegisterForEventValidation(r.UniqueID & "$ctl00", columnIndex.ToString())
End If
Next
End If
Next
MyBase.Render(writer)
End Sub
如果单击单元格,TextBox将变为可见(用户输入项目编号)并使标签不可见。如果用户决定不输入项目编号,如何返回标签?
If I click on the cell, the TextBox is made visible (user enters item number) and Label is made invisible. How can I go back to the Label if the user decides not to enter an item number?
推荐答案
ctl00,columnIndex.ToString())
结束 如果
' 图像和数量
如果((columnIndex = 3 ))和 _
(不 IsNullOrEmpty ( DirectCast (r.Cells( 4 )。FindControl( lblItem),标签).Text))然后
。 ClientScript.RegisterForEventValidation(r.UniqueID&
ctl00", columnIndex.ToString()) End If 'Image and Quantity If ((columnIndex = 3)) And _ (Not IsNullOrEmpty(DirectCast(r.Cells(4).FindControl("lblItem"), Label).Text)) Then Page.ClientScript.RegisterForEventValidation(r.UniqueID & "
ctl00,columnIndex.ToString())
结束 如果
下一步
结束 如果
下一步
MyBase .Render(作家)
结束 Sub
ctl00", columnIndex.ToString()) End If Next End If Next MyBase.Render(writer) End Sub
如果单击单元格,TextBox将变为可见(用户输入项目编号),Label变为不可见。如果用户决定不输入商品编号,如何返回标签?
If I click on the cell, the TextBox is made visible (user enters item number) and Label is made invisible. How can I go back to the Label if the user decides not to enter an item number?
将TextBox
放入EditItemTemplate
和标签
在ItemTemplate
。
参考 - TemplateField.EditItemTemplate属性 [ ^ ]。
Put yourTextBox
insideEditItemTemplate
andLabel
inItemTemplate
.
Refer - TemplateField.EditItemTemplate Property[^].
引用:
获取或设置用于在 TemplateField 对象。
Gets or sets the template for displaying an item in edit mode in a TemplateField object.
这篇关于在gridview中显示标签而不是文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文