3

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

3 に答える 3

7

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);
}
于 2012-07-11T15:22:16.213 に答える
1

Button1_Clickイベントの最後にClientScriptを登録し、
OnClientClick = "javascript:Highlit()"を削除する必要があります

protected void Button1_Click(object sender, EventArgs e)
{
    //Do stuff
    ScriptManager.RegisterStartupScript(this, this.GetType(), "ANYNAME", "javascript:Highlit();", true);
}
于 2012-07-11T15:30:46.000 に答える
0

属性を削除しOnClientClick、呼び出しをスタートアップスクリプトとして追加して、ページが読み込まれた後に実行されるようにします。

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

補足:OnClientClick属性を使用する場合、コードは。で始まらないようにする必要がありますjavascript:hrefこれは、リンクの属性にスクリプトを配置する場合にのみ使用されます。

于 2012-07-11T15:25:52.960 に答える