0

In asp.net website, inside .aspx i have following code

<script type="text/javascript">
    function callme(option) {
        document.getElementById("t1").value = option;
    }
</script>
<div id="content" runat="server"></div>

<input type="text" id="t1" />

On code behind file inside Page_Load:

content.InnerHtml = MyClassObject.MyMethod(...);

Inside MyClass:

public String MyMethod(...)
{
   ... //some code
   String str1 ="<select id=\"s1\" onchange=\"callme(this.value)\">" +
                    "  <option value=\"1\">One</option>"+
                    "  <option value=\"2\">Two</option>" +
                    "  <option value=\"3\">Three</option>" +                          
                    "</select>";
   ... // some code
   return str1;

Whenever i select any option from dropdownlist it reflects its value inside the textbox t1. But at page load the textbox remains empty. I cannot use default value as the values of the dropdownlist are changing at runtime. How can I add first value of dropdownlist to textbox t1 on page load?

2
  • what do you mean "the values of the dropdownlist are changing at runtime"? and how do they change? Commented Sep 7, 2012 at 10:26
  • this method "MyMethod()" connects to database and fetches value from there. Its a generic method. Thats why according to parameters passed values building the String str1 changes. Commented Sep 7, 2012 at 10:32

2 Answers 2

1

In your onload function in javascript you get the currently selected value in a drowdownlist as follows:

var s1 = document.getElementById("s1");
var selectedVal = s1.options[s1.selectedIndex].value;
Sign up to request clarification or add additional context in comments.

Comments

1
<script type="text/javascript">
function BuyG2() {
    alert('Hai Jquery');
}
</script> //jquery in sourse
protected void Page_Load(object sender, EventArgs e)
{
    ScriptManager.RegisterStartupScript(this.Page, this.GetType(), "tmp",
        "<script type='text/javascript'>BuyG2();</script>", false);
} //ASP Button Click Events

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.