Fill TextBox value using JavaScript and execute the remaining code in ASP.Net

Last Reply on Mar 12, 2013 06:17 AM By Amit

Posted on Mar 12, 2013 04:13 AM

Hi all I have written a javascript function where I pass 3 arguments from code behind I am having a text box, button and a text box on my form on leaving text box or clicking the button when the cursor is inside the first text box I would like to fill the second text box from the script I call the script inside the button as follows

<script type="text/javascript">
    function getEndDate2(noOfWeeks, sEmployeeWeeklyOfDay, DateStart) {
            // My script code
  }
 
<asp:TextBox ID="txtString" runat="server"></asp:TextBox>
<br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
 
 <asp:Button ID="btnClick" runat="server" Text="Pass" OnClick="btnClick_Click" />
 
protected void btnClick_Click(object sender, EventArgs e)
{
    int noOfweeks = Convert.ToInt16(txtString.Text);;
    string sEmployeeWeeklyOfDay = "Friday";
    string DateStart = DateTime.Now.AddDays(3).ToString();
    btnClick.Attributes.Add("onClick", "getEndDate2('" + noOfweeks + "','" + sEmployeeWeeklyOfDay + "','" + DateStart + "');");
     // Some code to insert the values to database
 }

But unable to display the value in the second text box can some one help me how can I call that script Immediately after losing focus on from the text box or any method to fill the text box first

You are viewing reply posted by: Amit on Mar 12, 2013 06:17 AM.
Posted on Mar 12, 2013 06:17 AM

I guess you want to use value which send to TextBox1 on button click and also want to display it immediately.You can't do this in that way instead of this use onclientclick function on button click and change javascript slightely 

 <script  type="text/javascript">
    function getEndDate2() {
    var noOfweeks = document.getElementById('txtString').value; //assigning value of txtString
    var sEmployeeWeeklyOfDay = "Friday"
    var DateStart = '<%=DateTime.Now.AddDays(3).ToString() %>';    
    var TextBox1 = document.getElementById('TextBox1'); 
    //you can place value to textbox1 in that way and use this value in code behind if required
    TextBox1.value=noOfweeks + "--" + sEmployeeWeeklyOfDay  + "--" +  DateStart;
  }
  </script>

Source Code :

 <div>
    <asp:TextBox ID="txtString" runat="server"></asp:TextBox><br />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>  
 <asp:Button ID="btnClick" runat="server" OnClientClick ="getEndDate2();" Text="Pass"  />
    </div>

 

Code Behing (in vb.net)

if you want to use value of textbox

 Protected Sub btnClick_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnClick.Click

        Dim textvalue As String = TextBox1.Text

    End Sub