Click here to Skip to main content
15,897,891 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I want to call c# methode from javascript timer like below.

XML
<script type="text/javascript">

        window.setInterval(DeleteKartItems, 10000);

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


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");
                    }
                }
            }
        }


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;
Posted
Updated 21-Aug-15 21:28pm
v2

I see, you missed adding a
C#
[WebMethod]
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[^]
 
Share this answer
 
v3
Comments
Dawood507 22-Aug-15 3:30am    
Sorry I written '[WebMethod]' but here not mentioned.
yeah I not used ScriptManager .
and I dnt want time i want control value in static methode.
Zubair Alie 22-Aug-15 3:48am    
You can send control value to webmethod from javascript by following..
var controlId = "MyTextBox";
var Controlvalue = document.getElementById(controlId).value;
and then pass that value to your pageMethod....
PageMethods.UIControl(controlId, controlValue);

I hope it will help.
Zubair Alie 22-Aug-15 3:51am    
In your case it will be something like this...
var lableValue = document.getElementById('lblRoomId').value;
and then
PageMethods.DeleteItem(labelValue);
Dawood507 22-Aug-15 4:42am    
not firing the methode if i use parameter.
Zubair Alie 22-Aug-15 5:10am    
have you added the parameter in your method as well?
i.e.
[WebMethod]
public static void DeleteItem(string value)
{
//method body.
}
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.
 
Share this answer
 
Comments
Dawood507 24-Aug-15 4:03am    
update panel is not working for divs.
F-ES Sitecore 24-Aug-15 4:11am    
UpdatePanels do work, so the problem is somewhere in your implementation.
Dawood507 24-Aug-15 4:21am    
I applied this style to div

<style type="text/css">
#upbtnSend {
float:right;
}
body {height:300px}
#divUsers
{
font:normal normal 11px/1.4 Tahoma,Verdana,Sans-Serif;
color:#333;
width:200px; /* Chatbox width */
border:1px solid #344150;
border-bottom:none;
background-color:white;
position:fixed;
right:10px;
bottom:0;
z-index:9999;
-webkit-box-shadow:1px 1px 5px rgba(0,0,0,.2);
-moz-box-shadow:1px 1px 5px rgba(0,0,0,.2);
box-shadow:1px 1px 5px rgba(0,0,0,.2);
}
.chat-box {
font:normal normal 11px/1.4 Tahoma,Verdana,Sans-Serif;
color:#333;
width:200px; /* Chatbox width */
border:1px solid #344150;
border-bottom:none;
background-color:white;
position:fixed;
right:175px;
bottom:0;
z-index:9999;
-webkit-box-shadow:1px 1px 5px rgba(0,0,0,.2);
-moz-box-shadow:1px 1px 5px rgba(0,0,0,.2);
box-shadow:1px 1px 5px rgba(0,0,0,.2);
}

.chat-box > input[type="checkbox"] {
display:block;
margin:0 0;
padding:0 0;
position:absolute;
top:0;
right:0;
left:0;
width:100%;
height:26px;
z-index:4;
cursor:pointer;
opacity:0;
filter:alpha(opacity=0);
}

.chat-box > label {
display:block;
height:24px;
line-height:24px;
background-color:#344150;
color:white;
font-weight:bold;
padding:0 1em 1px;
}

.chat-box > label:before {content:attr(data-collapsed); float:right}

.chat-box .chat-box-content {
padding:10px;
display:none;
}

/* hover state */
.chat-box > input[type="checkbox"]:hover + label {background-color:#404D5A}

/* checked state */
.chat-box > input[type="checkbox"]:checked + label {background-color:#212A35}
.chat-box > input[type="checkbox"]:checked + label:before {content:attr(data-expanded); float:right}
.chat-box > input[type="checkbox"]:checked ~ .chat-box-content {display:block}

</style>

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900