0

I have an aspx file, that contain a form. In the form there is input type text. How can I change it value via the c# code?

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        int num = 5;
        if (num > 6)
            mytextbox.value="big";
        else
            mytextbox.value="small";
    }
</script>
<asp:Content ID="Content1" ContentPlaceHolderID="HeadContent" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="Server">
    <form method="get" id = "myform">
    <input id="mytextbox" type="text" name="mtb" />
    </form>
</asp:Content>

Thank you!!

2 Answers 2

2

You need to add runat="server" to the input and form in order to be able to assign a value to it directly in your codebehind:

<form method="get" runat="server" id="myform">
    <input id="mytextbox" runat="server" type="text" name="mtb" />
</form>
Sign up to request clarification or add additional context in comments.

Comments

0

Unless you specically wanted the to use <input>

You could use

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

and

 <script runat="server">
     protected void Page_Load(object sender, EventArgs e)
     {
         int num = 5;
         if (num > 6)
             mytextbox.Text ="big";
         else
             mytextbox.Text ="small";
     } 
</script>

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.