txtRestaurantSearch.ID = "txtSearch";
TextBox textbox = (TextBox)Page.FindControl("txtSearch");
始终为null。如何从aspx textBox的动态更改id中获取值
整个代码:
<%@ Master Language="C#" MasterPageFile="~/MasterPages/HeaderFooter.Master" AutoEventWireup="true"
CodeBehind="Search.master.cs" Inherits="FoodOrder.MasterPages.Search" %>
<asp:Content ID="Content1" ContentPlaceHolderID="cplMPHead" runat="server">
<asp:ContentPlaceHolder ID="cplMPHead" runat="server">
</asp:ContentPlaceHolder>
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="cplMPBody" runat="server">
<asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
</asp:ScriptManagerProxy>
<div class="search">
<ul class="tabs">
<li ID="liRestaurnat" runat="server">
<asp:LinkButton ID="hlRestaurant"
runat="server" CssClass="active" Text="Poišči restavracijo"
ToolTip="Poišči po imenu restavracije" NavigateUrl="~/Default.aspx"
onclick="hlRestaurant_Click" ></asp:LinkButton>
</li>
<li ID="liAddress" runat="server" class="second">
<asp:LinkButton ID="hlAddress"
runat="server" Text="Iskanje po krajih" ToolTip="Poišči po imenu kraja"
NavigateUrl="~/Default.aspx" onclick="hlAddress_Click"></asp:LinkButton>
</li>
</ul>
<div class="search_place">
<ul>
<li>
<fieldset>
<asp:TextBox ID="txtRestaurantSearch" CssClass="input_big" runat="server" Text=""
enableviewstate="true" ontextchanged="txtSearch_TextChanged" />
</fieldset>
</li>
答案 0 :(得分:0)
你的代码应该可以工作(它在我的机器上工作)。你有没有将viewstate设置为true?
txtRestaurantSearch.ID = "txtSearch";
TextBox textbox = (TextBox)Page.FindControl("txtSearch");
textbox.Text = textbox.ID;
输出应该是
txtSearch
答案 1 :(得分:0)
以下是C#代码。
// Get a reference to the master page
MasterPage mp =(MasterPage) FindControl("ctl00");
// Get a reference to the ContentPlaceHolder
ContentPlaceHolder MainContent = (ContentPlaceHolder) mp.FindControl("cplMPBody");
// Reference the TextBox controls
TextBox textbox = (TextBox)MainContent.FindControl("txtRestaurantSearch");
textbox.ID = "txtSearch";
string str=textbox.Text;
答案 2 :(得分:0)
这是一个有效的例子。
protected void Button1_Click(object sender, EventArgs e)
{
MasterPage mp = (MasterPage)FindControl("ctl00");
ContentPlaceHolder cp = (ContentPlaceHolder)mp.FindControl("ContentPlaceHolder1");
TextBox tb = (TextBox)cp.FindControl("TextBox1");
tb.ID = "txtSearch";
tb.Text = tb.ID+tb.Text;
}
在文本框中输入一些值后,您可以访问其Text
属性。