c#如何在静态Web方法中获取asp.net文本框值? [英] c# how to get asp.net textbox value inside static web method?

查看:77
本文介绍了c#如何在静态Web方法中获取asp.net文本框值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从javascript计时器调用c#方法,如下所示。



I want to call c# methode from javascript timer like below.

<script type="text/javascript">

        window.setInterval(DeleteKartItems, 10000);

        function DeleteKartItems() {
            PageMethods.DeleteItem();
            alert("test");
        }
    </script>





c#methodes





c# methodes

[WebMethod]
public static void DeleteItem()
        {


            string query = "[Get_Messages]";
            SqlCommand cmd = new SqlCommand(query);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@roomId", 5);
            GetData(cmd);
        }

        private static void GetData(SqlCommand cmd)
        {
            string strConnString = ConfigurationManager.ConnectionStrings["LinqChatConnectionString"].ConnectionString;
            using (SqlConnection con = new SqlConnection(strConnString))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter())
                {
                    cmd.Connection = con;
                    sda.SelectCommand = cmd;
                    using (DataSet ds = new DataSet())
                    {
                        sda.Fill(ds, "Messages");
                    }
                }
            }
        }





所以如何获得label / TextBox在上面的方法里面的价值?



我试过下面但它的价值是空的..





so how to get label/TextBox value inside above methodes?

I tried like below but its getting null value..

Page page = HttpContext.Current.Handler as Page;
            Label lblRoomId = (Label)page.FindControl("lblRoomId");
            string lbRoomId = lblRoomId.Text;

推荐答案

我明白了,你错过了添加
I see, you missed adding a
[WebMethod]

属性。其次,您可能已经或可能没有在页面上添加ScriptManager。请确认这两件事,休息看起来很不错。



如需了解更多详情,请尝试此链接 [ ^ ]







您可以从我的OneDrive帐户下载所需内容。 试用此示例 [ ^ ]

attribute on your static method in .aspx.cs code file. Secondly you may or may not have added ScriptManager on your page. Please confirm these both things and rest is looking good to go with.

For further details, try this link[^]

Or

You can download a sample of what you required from my OneDrive account. try this sample[^]


您的页面方法无法访问它们被调用的页面,您需要将数据作为参数传递。



http://www.dotnetjalps.com/2012/01/aspnet-page-methods-with- parameters.html [ ^ ]



http://stackoverflow.com/questions/11563638/javascript-get-input-text-value [ ^ ]



同样的页面方法可以' t更新他们从中调用的页面。如果您需要通过ajax读取页面,那么UpdatePanel优于PageMethods。
Your page methods can't access the page they are called from, you need to pass the data as a parameter.

http://www.dotnetjalps.com/2012/01/aspnet-page-methods-with-parameters.html[^]

http://stackoverflow.com/questions/11563638/javascript-get-input-text-value[^]

Similarly your page methods can't update the page they have been called from either. If you need the page read\updated via ajax then an UpdatePanel is better than PageMethods.


这篇关于c#如何在静态Web方法中获取asp.net文本框值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆