简体   繁体   中英

dynamic changed id of aspx textBox

txtRestaurantSearch.ID = "txtSearch";    
TextBox textbox = (TextBox)Page.FindControl("txtSearch");

which is always null. How can i get value from dynamic changed id of aspx textBox

whole code:

<%@ 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>   

your code should work (it is working on my machine). Have you set viewstate to true ?

txtRestaurantSearch.ID = "txtSearch";
TextBox textbox = (TextBox)Page.FindControl("txtSearch");
textbox.Text = textbox.ID;


OUTPUT should be

txtSearch

Following is C# code.

// 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;

This is a working example.

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;
}


after putting some value in textbox you can access its Text property.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM