Click here to Skip to main content
15,890,186 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

XML
<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
Posted
Comments
sri senthil kumar 12-Mar-13 5:47am    
To my understanding, what you want is to fill the second textbox by calling the getEndDate2? if thats the case then

btnClick.Attributes.Add("onClick", "getEndDate2('" + noOfweeks + "','" + sEmployeeWeeklyOfDay + "','" + DateStart + "');");

Add the above code in page load and remove those button events because in the code you written will bind client event in first click of the button only and on second click the client script might get called. if you want text to populate after losing focus straight a way you can use onblur event of textbox.
demouser743 12-Mar-13 5:49am    
In the page load I can not get this `int noOfweeks = Convert.ToInt16(txtString.Text);` then how can I
AshishChaudha 12-Mar-13 7:27am    
put all the values in hidden field.

1 solution

why don't u call it on OnClientClick of button because u can able to do same thing from javascript also...
for example
C#
function getEndDate2()
    {
    var noOfweeks = document.getElementById('txtString').innerHTML ;
    var sEmployeeWeeklyOfDay = "Friday";
    var DateStart = new Date();
    DateStart.setDate(DateStart.getDate()+3);
    document.getElementById('TextBox1').value = DateStart.getDate();
    // My script code
    }

ASP.NET
<asp:button id="btnClick" runat="server" text="Pass" onclientclick="getEndDate2()" xmlns:asp="#unknown" />
 
Share this answer
 
v3
Comments
demouser743 12-Mar-13 8:43am    
Thanks for the idea :)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900