无法从代码后面访问按钮 [英] Cant access button from behind code
问题描述
这背后的代码
受保护的 Sub gvData_RowCommand( ByVal sender 作为 对象 , ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs)句柄 gvData.RowCommand
' lblError.Text =
Dim oButton As Button = fvData.FindControl( BtnRevisi)
' < span class =code-comment> Dim oTextbox As TextBox = DirectCast(Me.fvData.FindControl(txtRevisi),TextBox)
lblError.Visible = 发lse
如果 e.CommandName = 选择 然后
fvData.ChangeMode(FormViewMode.Edit)
fvData.Caption = 编辑数据
oButton.Visible = 错误
结束 如果
如果 e.CommandName = Revisi 然后
结束 如果
结束 Sub
这是在HTML上
< pre > < asp:FormView ID = fvData runat = server DataSourceID = sdsForm
DefaultMode = 编辑 宽度 = 100% DataKeyNames = ID >
< EditItemTemplate >
< 表 cellpadding = 0 cellspacing = 0 width = 100% >
< tr >
< td colspan = 2 < span class =code-keyword>>
< hr / >
< span class =code-keyword>< / td >
< / tr >
< tr >
< td colspan = 2 >
< / td >
< / tr >
< tr >
< span class =code-keyword>< td colspan = < span class =code-keyword> 2 class = FooterStyle >
< uc1:BtnUpdate ID = BtnUpdate1 runat = server / >
< uc2:BtnDelete ID = < span class =code-keyword> BtnDelete1 runat = server / >
< uc3:BtnCancel ID = BtnCancel1 runat = server / >
< < span class =code-leadattribute> asp:Button ID = btnRevisi < span class =code-attribute> runat = server OnClick = btnSave_Click 样式 = font-size:8pt;游标:指针;背景位置:左; background-image:url(../ images / Save.gif); background-repeat:no-repeat; text-align:right; 文字 = Revisi 工具提示 = 保存 ValidationGroup = 定价 宽度 = 55px / >
< / td >
< / tr >
< tr >
< td colspan = 2 >
< ; hr / < span class =code-keyword>>
< / td >
< / tr >
< / table >
< / EditItemTemplate >
本质上我只是想从btnRevisi中获取id属性,因为btnRevisi的位置在formview中,所以btnRevisi的id可以不被称呼。运行后显示错误是btnRevisi没有任何对象引用没有设置为对象的实例。
我尝试了什么:
我已经使用DirectCast声明,结果是相同的(对象引用没有设置为对象的实例)。像这样代码Dim oButton As Button = fvData.FindControl(btnRevisi)
oButton.Visible = False在本节中它的错误
您要查找的按钮位于EditItemTemplate
。
在FormView
更改为编辑模式并绑定到其数据源之前,不会创建该模板中的控件。
这样的东西应该有效:
如果 e.CommandName = 选择 然后
fvData.Caption = 编辑数据
fvData.ChangeMode(FormViewMode.Edit)
fvData.DataBind()
Dim oButton As Button = fvData .FindControl(<温泉n class =code-string> btnRevisi)
oButton.Visible = False
结束 如果
但是,如果您所做的只是设置按钮的可见性,您可以在标记中执行此操作:
< asp:按钮 ID = btnRevisi runat = server 可见 = False ...
this behind code
Protected Sub gvData_RowCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles gvData.RowCommand
'lblError.Text = ""
Dim oButton As Button = fvData.FindControl("BtnRevisi")
'Dim oTextbox As TextBox = DirectCast(Me.fvData.FindControl("txtRevisi"), TextBox)
lblError.Visible = False
If e.CommandName = "Select" Then
fvData.ChangeMode(FormViewMode.Edit)
fvData.Caption = "Edit Data"
oButton.Visible = False
End If
If e.CommandName = "Revisi" Then
End If
End Sub
this is on html
<pre><asp:FormView ID="fvData" runat="server" DataSourceID="sdsForm"
DefaultMode="Edit" Width="100%" DataKeyNames="ID">
<EditItemTemplate>
<table cellpadding=0 cellspacing=0 width="100%">
<tr>
<td colspan="2">
<hr />
</td>
</tr>
<tr>
<td colspan="2">
</td>
</tr>
<tr>
<td colspan="2" class="FooterStyle">
<uc1:BtnUpdate ID="BtnUpdate1" runat="server" />
<uc2:BtnDelete ID="BtnDelete1" runat="server" />
<uc3:BtnCancel ID="BtnCancel1" runat="server" />
<asp:Button ID="btnRevisi" runat="server" OnClick="btnSave_Click" Style="font-size: 8pt; cursor: pointer; background-position: left; background-image: url(../images/Save.gif); background-repeat: no-repeat; text-align: right;" Text="Revisi" ToolTip="Save" ValidationGroup="Pricing" Width="55px" />
</td>
</tr>
<tr>
<td colspan="2">
<hr />
</td>
</tr>
</table>
</EditItemTemplate>
in essence I just want to grab the id property from the btnRevisi, because the location of the btnRevisi is in the formview, so the id of the btnRevisi can not be called. and display error after the run is btnRevisi it is nothing Object reference not set to an instance of an object.
What I have tried:
I have declare by using DirectCast, the result is the same still (Object reference not set to an instance of an object). like this the code Dim oButton As Button = fvData.FindControl("btnRevisi")
oButton.Visible = False in this section its an error
The button you're trying to find is in theEditItemTemplate
.
The controls in that template aren't created until theFormView
is changed to "edit" mode and bound to its data source.
Something like this should work:
If e.CommandName = "Select" Then fvData.Caption = "Edit Data" fvData.ChangeMode(FormViewMode.Edit) fvData.DataBind() Dim oButton As Button = fvData.FindControl("btnRevisi") oButton.Visible = False End If
However, if all you're doing is setting the visibility of the button, you can do that in the markup:
<asp:Button ID="btnRevisi" runat="server" Visible="False" ...
这篇关于无法从代码后面访问按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!