coder
coder

Reputation: 13248

How to get the String Value to the textbox using javascript with asp.net

I am creating a folder by generating a random string and I need to get that string value to the textbox using javascript.

Here is my javascript Code:

<script type="text/javascript">
 var tempDir = randomString(8);
                document.getElementById("currentDirectory").value = tempDir;
                alert(tempDir);
</script>

This is the textbox where I need to display

<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>

Upvotes: 0

Views: 2141

Answers (3)

Harsh Baid
Harsh Baid

Reputation: 7249

That's It,

document.getElementById('<%= TextBox1.ClientID  %>').value = tempdir;

In asp.net the id of elements changes when rendered in browser so you need to take the.Id from control's ClientID property.

Upvotes: 2

coder
coder

Reputation: 13248

I resolved it by simply declaring the control this way inside the body tag.

('<%= TextBox1.ClientID %>')

Upvotes: 1

jmease
jmease

Reputation: 2535

You can also try JQuery

 $("#ControlID").val(tempDir);

You'll want to check how the control is rendered in the HTML though since it's an ASP.Net control. Sometimes the ID changes a bit and you'll want the command to use that in place of ControlID above.

Upvotes: 0

Related Questions