C#-用FindControl填充文本框
作者:互联网
我有多个文本框,我想用相同的方法以编程方式填充它们.
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="ScriptManager1" runat="server">
</asp:ScriptManager>
<asp:UpdateProgress ID="UpdateProgress1" runat="server" DisplayAfter="500">
<ProgressTemplate>
<div id="FreezeUpdate" class="FreezePaneOn">
<div id="InnerFreezePane" class="InnerFreezePane gradient"><asp:Literal ID="litProgress" runat="server" Text="<%$Resources:LocalizedText, freezeMsg_ProcessingPleaseWait %>"></asp:Literal></style></div>
</div>
</ProgressTemplate>
</asp:UpdateProgress>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<asp:TextBox ID="tbxCollectionAddress" runat="server" TextMode="MultiLine" ReadOnly="True" CssClass="address"></asp:TextBox>
<asp:TextBox ID="tbxReturnAddress" runat="server" TextMode="MultiLine" ReadOnly="True" CssClass="address"></asp:TextBox>
</asp:UpdatePanel>
</asp:Content>
然后是方法…
protected void PopulateAddress(string txtTextbox, Address adrAddress)
{
TextBox txtAddress = (TextBox)FindControl(txtTextbox);
txtAddress.Text = "Hello World";
}
当我像下面这样调用此方法时…
PopulateAddress("tbxReturnAddress", CollectionAddress);
我异常
System.NullReferenceException: Object reference not set to an instance of an object.
…并突出显示txtAddress.Text =“ Hello World”;线.
我想用这种方法来做到这一点,因为我需要在表单上填充三个单独的地址,但是我想尝试通过使用可重复使用的代码来输入地址对象和文本框来完成此操作. id,但是我只是在findcontrol阶段失败了.
我想念什么?
解决方法:
首先,测试你的功能喜欢
TextBox txtAddress = (TextBox)FindControl("tbxReturnAddress");
要么
您可以尝试这样,
TextBox txtAddress = (TextBox)Page.FindControl("tbxReturnAddress");
编辑
如果您的文本框位于UpdatePanel中,
TextBox txtAddress = (TextBox)UpdatePanel1.FindControl("tbxReturnAddress");
标签:findcontrol,asp-net,c 来源: https://codeday.me/bug/20191122/2063623.html