最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Fire Serverside event from javascript - Stack Overflow

programmeradmin2浏览0评论

i have hiddentfield whose value is changing on javascript.

I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript.

I tried with :

__doPostBack('hfLatitude', 'ValueChanged');

But giving me error :

Microsoft JScript runtime error: '__doPostBack' is undefined

Is there any other alternative for this?

Please help me.

i have hiddentfield whose value is changing on javascript.

I just wanted to fire serverside event valuechanged event of hiddenfield when its value changed from javascript.

I tried with :

__doPostBack('hfLatitude', 'ValueChanged');

But giving me error :

Microsoft JScript runtime error: '__doPostBack' is undefined

Is there any other alternative for this?

Please help me.

Share Improve this question asked Nov 1, 2013 at 10:59 C SharperC Sharper 8,64630 gold badges93 silver badges158 bronze badges 2
  • Is there a reason why you are not using hidden field's value changed event : <asp:HiddenField OnValueChanged="EventHandler" /> – Zaki Commented Nov 1, 2013 at 11:06
  • y6es, i am using it, but wants to call it from javascript – C Sharper Commented Nov 1, 2013 at 11:08
Add a ment  | 

3 Answers 3

Reset to default 4

In javascript, changes in value to hidden elements don't automatically fire the "onchange" event. So you have to manually trigger your code that is already executing on postback using "GetPostBackEventReference".

So, with a classic javascript approach, your code should look something like in the example below.

In your aspx/ascx file:

    <asp:HiddenField runat="server" ID="hID" OnValueChanged="hID_ValueChanged" Value="Old Value" />
    <asp:Literal runat="server" ID="litMessage"></asp:Literal>
    <asp:Button runat="server" ID="btnClientChage" Text="Change hidden value" OnClientClick="ChangeValue(); return false;" />

    <script language="javascript" type="text/javascript">

        function ChangeValue()
        {
            document.getElementById("<%=hID.ClientID%>").value = "New Value";
            // you have to add the line below, because the last line of the js code at the bottom doesn't work
            fValueChanged();
        }

        function fValueChanged()
        {
            <%=this.Page.GetPostBackEventReference(hID, "")%>;
        }

        // the line below doesn't work, this is why you need to manually trigger the fValueChanged methiod
        // document.getElementById("<%=hID.ClientID%>").onchange = fValueChanged;

    </script>

In your cs file:

protected void hID_ValueChanged(object sender, EventArgs e)
{
    litMessage.Text = @"Changed to '" + hID.Value + @"'";
}

Quick and Dirty:

Simply put a asp button on form. Set it display:none.

<asp:Button id="xyx" runat="server" style="display:none" OnClick="xyx_Click" />

On its click event call any server side event.

protected void xyx_Click(o,e)
{
   //you server side statements
}

To call its from JS use as below:

<script>

function myserverside_call()
{
var o = document.getElementById('<%=xyx.ClientID%>');
o.click();
}

function anyotherjsfunc()
{
   //some statements
   myserverside_call();
}
</script>

First way is to use HiddenField.ValueChanged Event.

If you want to also watch this varible in Client Side just use this:

    $('#hidden_input').change(function() { 
     alert('value changed');
});

Second way is to assign value to Varible:

$('#hidden_input').val('new_value').trigger('change');
发布评论

评论列表(0)

  1. 暂无评论