0

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?

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.