在asp按钮控件上运行C#和Javascript函数

Running a C# and a Javascript function on an asp button control?

本文关键字:Javascript 函数 运行 asp 按钮 控件      更新时间:2023-09-26

我可以让Javascript和C#函数都正常工作。

但是,我的Javascript函数在C#之前运行。

我如何让它在C#函数之后运行??

这是我的代码:

<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
<asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
<asp:View ID="View1" runat="server">
<div id="div2" style="height:70px; width:auto; text-align:center;">
<p><b>This is A View!!!</b></p>
  <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
</div>
<div id="div1">
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" 
        OnClientClick="javascript:Highlit()" />
</div>
</asp:View>
</asp:MultiView>
<script type="text/javascript">
function Highlit() 
{
 $("#div2").effect("highlight", {}, 10000);
}
</script>
 </ContentTemplate>
 </asp:UpdatePanel>

代码隐藏:

namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Changed";
    }
   }
}

以下是反映答案变化的代码:

代码隐藏

namespace jQuery_Highlight.jQuery_Highlight
{
public partial class jQuery_HighlightUserControl : UserControl
{
    protected void Page_Load(object sender, EventArgs e)
    {
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        Label1.Text = "Changed";
        ScriptManager.RegisterStartupScript(this, this.GetType(), "TEST", "Highlit();", true);
    }
   }
   }


 <script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.8/jquery-ui.min.js"></script>
 <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
 <ContentTemplate>
 <asp:MultiView ID="MultiView1" runat="server" ActiveViewIndex="0">
 <asp:View ID="View1" runat="server">
 <div id="div2" style="height:70px; width:auto; text-align:center;">
 <p><b>This is A View!!!</b></p>
 <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
 </div>
 <div id="div1">
  <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
 </div>
 </asp:View>
 </asp:MultiView>
 </ContentTemplate>
 </asp:UpdatePanel>
<script type="text/javascript">
function Highlit() {
    $("#div2").effect("highlight", { color: "#9499FC" }, 10000);
}
</script>

让javascript在之后运行的唯一方法是在Button1_Click事件中添加脚本引用。

标准回发的示例代码:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Changed";
    Page.ClientScript.RegisterStartupScript(this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}

正如其他人所指出的,请确保删除您的OnClientClick事件。另外,考虑将"突出显示"脚本移到更新面板之外。

此外,由于您处于更新面板中,因此需要使用以下示例代码进行部分回邮:

protected void Button1_Click(object sender, EventArgs e)
{
    Label1.Text = "Changed";
    ScriptManager.RegisterStartupScript(this, this.GetType(), "PostButton1_ClickScript", "Highlit();", true);
}

您必须在Button1_Click事件结束时注册ClientScript
并删除OnClientClick="javascript:Highlet()"

protected void Button1_Click(object sender, EventArgs e)
{
    //Do stuff
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ANYNAME", "javascript:Highlit();", true);
}

删除OnClientClick属性,并将调用添加为启动脚本,以便在页面加载后运行:

protected void Button1_Click(object sender, EventArgs e) {
  Label1.Text = "Changed";
  Page.ClientScript.RegisterStartupScript(this.GetType(), "start", "Highlit();", true);
}

旁注:使用OnClientClick属性时,代码不应以javascript:开头。仅当您将脚本放在链接的href属性中时才使用。