Or K
Or K

Reputation: 335

How to change input text value with c#

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!!

Upvotes: 0

Views: 2719

Answers (2)

Ian G
Ian G

Reputation: 30234

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>

Upvotes: 0

Ant P
Ant P

Reputation: 25221

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>

Upvotes: 2

Related Questions