如何从javascript填充文本框值并执行剩余的代码

时间:2013-03-12 06:38:13

标签: c# javascript asp.net

大家好我已经编写了一个javascript函数,我从代码后面传递3个参数我在文本框上留下了一个文本框,按钮和一个文本框,当光标位于第一个文本内时单击按钮框我想填写脚本中的第二个文本框,我在按钮内调用脚本,如下所示

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

但无法在第二个文本框中显示该值可以帮助我如何在从文本框中失去焦点后立即调用该脚本

2 个答案:

答案 0 :(得分:0)

我不太清楚这里的问题,但从我的假设,你可以附加第一个文本框或按钮的功能

<script type="text/javascript">
    function getEndDate2(noOfWeeks, sEmployeeWeeklyOfDay, DateStart) {
            // My script code
    //then here an ajax call to the insertDB function

  }

<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 + "');");
     REMOVE THE+IS CODE TO next function // Some code to insert the values to database
 }

protected void insertDB() // this should be accessible to ajax call
{
      insert to db function

}

如果你能提供HTML,我可以提供更多帮助。我希望这有帮助

答案 1 :(得分:0)

通过获取两个隐藏字段并将其添加到脚本中,通过函数中的一些更改来调用按钮的函数OnClientClick事件

hdn.Value = "Friday";
hdn1.Value = DateTime.Now.AddDays(3).ToString();
<asp:Button ID="btnClick" OnClientClick="getEndDate2();" runat="server" Text="Pass" OnClick="btnClick_Click" />

<script type="text/javascript">
    function getEndDate2(){
        var noOfweeks = "";
        var sEmployeeWeeklyOfDay = "";
        var DateStart = "";
        noOfweeks = document.getElementById("txtString").value;
        sEmployeeWeeklyOfDay = document.getElementById("hdn").value;
        DateStart = document.getElementById("hdn1").value;
        // Your script remaining code
}
</script>