[英]dynamic changed id of aspx textBox
txtRestaurantSearch.ID = "txtSearch";
TextBox textbox = (TextBox)Page.FindControl("txtSearch");
始终为空。 我如何从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>
您的代码应该可以工作(它在我的机器上可以工作)。 您是否将viewstate设置为true?
txtRestaurantSearch.ID = "txtSearch";
TextBox textbox = (TextBox)Page.FindControl("txtSearch");
textbox.Text = textbox.ID;
输出应该是
txtSearch
以下是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;
这是一个有效的例子。
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
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.