简体   繁体   中英

Associate OnClick of input type with C# method

On entering a text in a Textbox element in asp net, owed to the OnTextChanged attribute, the method TextButton1_TextChanged from code behind is triggered:

<asp:TextBox ID="TextButton1" runat="server" value="" OnTextChanged="TextButton1_TextChanged"> </asp:TextBox>`

What is a way to obtain the same functionality with an input type text like:

<input type="text" id="InputType1" placeholder="Placeholder here">

I tried this with no effect:

<input type="text" id="InputType1" onclick ="TextButton1_TextChanged" >

如果希望它成为等效的OnClick()或使用OnServerChanged="TextButton1_TextChanged TextButton1_TextChanged等效于textbox_change等效,请使用runat="server"OnServerClick="TextButton1_TextChanged" 。有关OnServerChanged的更多详细信息,请链接:( https://msdn.microsoft.com /en-us/library/system.web.ui.htmlcontrols.htmlinputtext.onserverchange(v=vs.110).aspx

The simple answer: use a TextBox . But if you have to use the HtmlInputText -control you could handle the ServerChange -event :

<input type="text" id="InputType1" runat="server" ServerChange="TextButton1_TextChanged" >

codebehind:

protected void Server_Change(object sender, EventArgs e) 
{
   Console.WriteLine("You typed: " + InputType1.Value);
}

But note that you don't have AutoPostBack , so you have to wait until the next postback is triggered, fe from a button.

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