![](/img/trans.png)
[英]How to add text boxes dynamically in asp.net C# and use the textbox values after adding
[英]ASP/C#, adding text to textbox problem
出於某種原因,我無法將文本輸入任何文本框或標簽!
我正在使用母版頁,代碼將在代碼后面查看。 我創建了文本框:
<asp:Textbox ID="whatever" runat="Server">
當我想添加一些文本時,我只需在代碼后面添加代碼,如:
whatever.Text = "myText";
我收到一條錯誤消息:
"System.NullReferenceException:Object reference not set to an instance of an object"
用紅色點亮這一行:whatever.Text =“myText”;
我猜它是因為它說它不在那里,但它怎么能讓我參考文本框?
抱歉,如果答案在網站上,我搜索但沒有發現任何內容。 :)
這是我在Basket.asp中的代碼 - 我已經將文本框更改為標簽,它被稱為bskItems
<asp:Content ID="Content3" ContentPlaceHolderID="ContentPlaceHolder3" runat="server"> <asp:Label ID="bskItems" runat="server"></asp:Label> <div id="cart"> <asp:Button ID="btnCheckout" CssClass="BasketBtnAdd" runat="server" CommandName="checkout" Text="Checkout" /> </div> </asp:Content>
這是我的主頁,我正在使用loginView。 ContentPlaceHolder3是文本框的位置。 我只希望它顯示項目數。
<asp:LoginView ID="loginView" runat="server">
<LoggedInTemplate>
<asp:LoginName ID="loginName" runat="server" FormatString="Hi, {0}!"/>
(<asp:LoginStatus ID="loginStatus" runat="server" />)
<%
if (HttpContext.Current.User.IsInRole("Admin"))
{
%>
<asp:SiteMapDataSource ID="admin" SiteMapProvider="admin" runat="server" ShowStartingNode="false" />
<asp:Menu ID="Menu" runat="server" DataSourceID="admin">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
if (HttpContext.Current.User.IsInRole("Users"))
{
%>
<asp:SiteMapDataSource ID="user" runat="server" SiteMapProvider="user" ShowStartingNode="false" />
<asp:Menu ID="Menu1" runat="server" DataSourceID="user">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
<%
}
%>
<asp:ContentPlaceHolder ID="ContentPlaceHolder2" runat="server"></asp:ContentPlaceHolder>
<asp:ContentPlaceHolder ID="ContentPlaceHolder3" runat="server"></asp:ContentPlaceHolder>
</LoggedInTemplate>
<AnonymousTemplate>
<asp:LoginStatus ID="loginStatus" runat="server" />
<asp:SiteMapDataSource ID="anon" runat="server" SiteMapProvider="anon" ShowStartingNode="false" />
<asp:Menu ID="Menu2" runat="server" DataSourceID="anon">
<StaticItemTemplate>
<%# Eval("Text") %>
</StaticItemTemplate>
</asp:Menu>
</AnonymousTemplate>
</asp:LoginView>
除了其他答案之外,如果您在Page.OnLoad中設置了值,請記住尚未創建母版頁控件。
以下是事件發生順序的完整布局: ASP頁面的完整生命周期
我通常做的是使控件作為我的MasterPage的屬性可見。
在母版頁(AMasterPage.master)上:
public TextBox MyTextBox { get { return this.theTextBoxControl; } }
那么,在使用這個masterPage(APage.aspx)的孩子身上:
((AMasterPage)this.Master).MyTextBox.Text = "myText";
在Content Place Holder文件中從Code-Behind訪問母版頁成員時,我認為您需要這樣做:
this.Master.whatever.Text = "new Text";
從MSDN上查看ASP.NET母版頁上的此鏈接。
您需要獲取對母版頁上文本框的引用,然后設置文本
TextBox tb = Master.Page.FindControl("whatever") as TextBox;
if(tb != null)
{
tb.Text = "myText";
}
將文本框上的ClientIDMode設置為“Static”。 呈現頁面時,它會將TextBox的ID分配給隨機的內容。 通過將ClientIDMode更改為“Static”,您應該能夠引用ID,因為ID將保持不變並且不會更改。
或者嘗試添加OnDataBinding事件處理程序並將“sender”轉換為(TextBox)。 例如:
protected void TextBox_OnDataBinding(object sender, EventArgs e)
{
var txt = (TextBox)sender;
txt.Text = "Something";
}
這應該直接與控件對話。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.