What I am looking for is to dynamically create two textboxes from the C# ASP.NET code (either normal textbox or HTML input text, I don’t mind).
And then I tried (and failed) to define an event that fires when the first textbox loses the focus, so, what is written in the first textbox should be copied to the second textbox.
I tried to use the onblur()
event, but I faced a problem…
Since the body of the function of the onblur event should be written in JavaScript, so my two textboxes that I created in the C# ASP.NET file will not be known by JavaScript code…
Is there a way to write the body of onblur event in default.cs…? Or maybe I should find another way?
Advertisement
Answer
Try following code:
Markup
<asp:PlaceHolder runat="server" ID="textBoxPlaceHolder"></asp:PlaceHolder>
Code-behind
TextBox t1 = new TextBox(); t1.ID = "textBox1"; TextBox t2 = new TextBox(); t2.ID = "textBox2"; textBoxPlaceHolder.Controls.Add(t1); textBoxPlaceHolder.Controls.Add(t2); String javascript = "javascript:document.getElementById('" + t2.ClientID + "').value=this.value;"; t1.Attributes.Add("onblur", javascript);