c# - How to pass a javascript variable to server side method - Stack Overflow

I am facing issue like I am unable to pass the javascript variable to server side I am aware that it is

I am facing issue like I am unable to pass the javascript variable to server side I am aware that it is not achieveable in this scenario so I tried like setting the value to the asp hidden field using jQuery and getting the value of the label in server side but unfortunately I am getting empty value for the hidden field. Help me on how to fix this

CODE

$(document).ready(function(){
  var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){

<%=RenderMethod(DataID) %>  // How to pass javascript variable to server side

}

Hidden Field Approach:

$(document).ready(function(){
     var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){
   $("#<%=hdnDataVal.ClientID %>").val(DataID);

  alert($("#<%=hdnDataVal.ClientID %>").val(DataID));    // Here using javascript I can able to set the value and when I alert the value it is displayed

  <%=RenderMethod(hdnDataVal.Value) %>  // here the hiddenfield value is empty

}

    <asp:HiddenField runat="server" ID="hdnDataVal"  />

I am facing issue like I am unable to pass the javascript variable to server side I am aware that it is not achieveable in this scenario so I tried like setting the value to the asp hidden field using jQuery and getting the value of the label in server side but unfortunately I am getting empty value for the hidden field. Help me on how to fix this

CODE

$(document).ready(function(){
  var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){

<%=RenderMethod(DataID) %>  // How to pass javascript variable to server side

}

Hidden Field Approach:

$(document).ready(function(){
     var DataID = "4325";
    testDataVal(DataID);
});

function testDataVal(DataID){
   $("#<%=hdnDataVal.ClientID %>").val(DataID);

  alert($("#<%=hdnDataVal.ClientID %>").val(DataID));    // Here using javascript I can able to set the value and when I alert the value it is displayed

  <%=RenderMethod(hdnDataVal.Value) %>  // here the hiddenfield value is empty

}

    <asp:HiddenField runat="server" ID="hdnDataVal"  />
Share Improve this question edited Nov 18, 2014 at 7:58 Vignesh asked Nov 18, 2014 at 7:38 VigneshVignesh 1,51812 gold badges31 silver badges60 bronze badges 9
  • 3 You need to either put the variable in a form and submit it, or use an AJAX request. You cannot apply a JS variable to C# code because one is client side and the other server side. – Rory McCrossan Commented Nov 18, 2014 at 7:42
  • @RoryMcCrossan but if I use hiddenfield also it's not working – Vignesh Commented Nov 18, 2014 at 7:43
  • Where are you setting the value of DataID? – Rory McCrossan Commented Nov 18, 2014 at 7:44
  • Actually the Render method is from another library it's not a direct method – Vignesh Commented Nov 18, 2014 at 7:47
  • 1 You get the value but it is empty at the time the page is being generated on the server side. When the page is generated and sent to the client, <%=RenderMethod(hdnDataVal.Value) %> had already output a value with an empty hdnDataVal.Value. – Ali Naci Erdem Commented Nov 18, 2014 at 8:08
 |  Show 4 more ments

3 Answers 3

Reset to default 1

First of all... you should not mix server code and client code the way you're doing it.

It's a poor way to design your code. Try always to separate client and server code. They execute on different moments, places and under different circumstances... having them together will eventually draw you to difficult to debug errors.

I bet that the problem you're experiencing here is due to this way of coding.

You say on your code snippet that

<%=RenderMethod(hdnDataVal.Value) %>  // here the hiddenfield value is empty

When your page is loading and server code is executed the code inside $(document).ready() is not fired yet, as it fires when your whole page finish loading. So, your RenderMethod is firing before you put any value inside the variable.

try using $.ajax();

var url = 'my-url.aspx';
$.ajax({
    url: url,
    type: 'POST',
    data: {'variable_name': my_variable },
    success: function(html)
    { 
      alert('ok');
    },

});

and receiver on the server-side:

string my_variable = Request.Form['variable_name'];

You can use a Page Method to make a server side call from client side. It's propably the easiest way to acplish what you want.

First of all you need to include the Script Manager in your aspx page with Page Methods enabled:

<asp:ScriptManager ID="scrmgr" EnablePageMethods="true" runat="server" /> 

Now you can invoke the server side method and pass it the client side data you want, with sth like this:

<script type="text/javascript">
    $(document).ready(function(){
        var DataID = "4325";
        testDataVal(DataID);
    });

    function testDataVal(DataID) {
        PageMethods.RenderMethod(DataID, OnSuccess, OnError);
    }

    function OnSuccess(result) {
        alert(result);
    }

    function OnError() {
        alert('Some error has ocurred!');
    }
    </script>

OnSuccess function is invoked in case the server-side method has been succesfully called. Otherwise OnError function is invoked.

This is how the Page Method should be declared inside the .aspx.cs file:

[System.Web.Services.WebMethod]
public static string RenderMethod(string dataID)
{
    return "Got here!";
}

If you place a breakpoint inside RenderMethod, then you can verify that client side data (i.e. value "4325") is correctly passed to it.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744119872a4559348.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信