How to access page controls inside a static web method?
As mentioned by @Tim Schmelter This doesn't answer this question because you can't access page's controls from a webmethod.
Please go through
asp.net access a control from static function
The whole point of [WebMethod]s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable.
Your controls don't exist.
your question is duplicate of How to get controls in static web method
How to access the Asp Controls inside a Static Method?
Try this out!
[System.Web.Services.WebMethod]
public static Array LoadAssetAssignView()
{
string sql = "SELECT Time,Inuse FROM table4";
using (SqlConnection Connection = new SqlConnection((@"Data Source")))
{
using (SqlCommand myCommand = new SqlCommand(sql, Connection))
{
Connection.Open();
using (SqlDataReader myReader = myCommand.ExecuteReader())
{
DataTable dt = new DataTable();
dt.Load(myReader);
Connection.Close();
Page page = (Page)HttpContext.Current.Handler;
TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");
TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
Num1=TextBox1 .text;
Num2=TextBox2 .text;
}
}
}
}
Refer to this.
How to access server controls from page methods?
HTML
<asp:TextBox ID="txtSearch" runat="server"></asp:TextBox>
<input id="btnSearch" type="button" value="Search"
onclick = "GetSearchResult()" />
</div>
JavaScript:
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
<script type = "text/javascript">
function GetSearchResult() {
$.ajax({
type: "POST",
url: "CS.aspx/GetSearchResult",
data: '{search: "' + $("#<%=txtSearch.ClientID%>")[0].value + '" }',
contentType: "application/json; charset=utf-8",
dataType: "json",
success: OnSuccess,
failure: function(response) {
alert(response.d);
}
});
}
function OnSuccess(response) {
alert(response.d);
}
</script>
How to get controls in static web method
You can't.
The whole point of [WebMethod]
s is that they don't run the ASP.Net page lifecycle. This way, they're fast and parallelizable.
Your controls don't exist.
Instead, you should use Javascript (better) or an UpdatePanel (worse).
how to access html control in static function (Asp.net)
you can use AJAX and JQuery for that these 3 links will help you
Accessing runat="server" control id in WebMethod
http://forums.asp.net/t/1580317.aspx?Access+Server+control+in+web+method+
http://www.itorian.com/2012/07/calling-aspnet-c-method-web-method.html
Accessing ASP control from webmethod
I believe ASP Labels get rendered as Spans, you should be able to change it like so:
success: function (data) {
$("#<%=stockLabel.ClientID %>").text(data);
}
As far as accessing page controls from a web method, you aren't allowed to. This post goes into it a lot better than I can: Access ASP.NET control from static [WebMethod] (JS ajax call)
Access ASP.NET control from static [WebMethod] (JS ajax call)
Well, that's not the correct approach. At the web service method level you cannot see anything about the page structure. In this method you can only load your list of items and return it. Where this list is binded to is none of GetListItems' business.
You can manage the display of the Items by implementing a callback function (see http://mattberseth.com/blog/2007/06/aspnet_ajax_invoke_a_static_me.html for example) or by using the UpdatePanel approach.
Related Topics
Questions About Entity Framework Context Lifetime
Equivalent of Tuple (.Net 4) for .Net Framework 3.5
.Net: Unable to Cast Object to Interface It Implements
C# MACro Definitions in Preprocessor
Taking Screenshot of a Webpage Programmatically
Why Gettype Returns System.Int32 Instead of Nullable<Int32>
Can You Access UI Elements from Another Thread? (Get Not Set)
C# Structs: Unassigned Local Variable
Newtonsoft.JSON Assembly Package Version Mismatch
Accuracy of Textrenderer.Measuretext Results
How to Get Current Regional Settings in C#
The 'Await' Operator Can Only Be Used Within an Async Lambda Expression
Convert.Toint32() a String with Commas
Regular Expression to Get the Src of Images in C#
An Integer Array as a Key for Dictionary
How to Implement One "Catch'Em All" Exception Handler with Resume