ASP.NET - Web Methods

Asked By prateek
03-Nov-11 01:22 AM
Hi

I want to get server side control in static web methods in c# using page.findcontrol("textbox")


Page page = (Page)HttpContext.Current.Handler;
Panel ltr = (Panel)page.FindControl("panelID");

but it return null in ltr object....how can i access server side control in web method...


thanks


  James H replied to prateek
03-Nov-11 02:12 AM
If you want to Load the server Control like this you need to do like this
A sample on Loading the User Control
[WebMethod]
   public static string Result(string controlName)
   {
     return RenderControl(controlName);
   }

   public static string RenderControl(string controlName)
   {
     Page page = new Page();
     UserControl userControl = (UserControl)page.LoadControl(controlName);
     userControl.EnableViewState = false;
     HtmlForm form = new HtmlForm();
     form.Controls.Add(userControl);
     page.Controls.Add(form);

     StringWriter textWriter = new StringWriter();
     HttpContext.Current.Server.Execute(page, textWriter, false);
     return textWriter.ToString();
   }
  Anoop S replied to prateek
03-Nov-11 02:26 AM
The FindControl("controlID") method is commonly used to programmatically reference a Web control. However, FindControl does not penetrate through naming containers. Consequently, you cannot directly use the Page.FindControl method to reference controls within a GridView or other naming container.

protected void SubmitButton_Click(object sender, EventArgs e)
{
TextBox AgeTextBox = Page.FindControl("Age") as TextBox;
}
  Reena Jain replied to prateek
03-Nov-11 02:31 AM
hi,

The ASP.NET Web Parts control set enables you to use existing Web server controls as Web Parts controls in order to achieve maximum code reuse and to gain the benefits of Web Parts personalization. User controls are one type of server control you can use as Web Parts controls. This topic demonstrates how to treat an existing user control as a Web Parts control.

for more information check this
http://msdn.microsoft.com/en-us/library/w9b5ett0.aspx

check these also
http://msdn.microsoft.com/en-us/library/yhzc935f.aspx
http://webproject.scottgu.com/CSharp/UserControls/UserControls.aspx
  dipa ahuja replied to prateek
03-Nov-11 02:55 AM
Untitled document
Step 1: Every server-side method that is called from the client-side, must be declared as "static", and also has to be decorated with the [System.Web.Services.WebMethod] tag.
 
[System.Web.Services.WebMethod()]
public static string Message()
{
   return "Hello from the server-side World!";
}
 
 
Step 2: Modifying the ScriptManager The "EnablePageMethods" attribute has to be added on the ScriptManager tag.
 
<asp:ScriptManager ID="ScriptManager1" runat="server" EnablePageMethods="true" />    

 
Step 3:Adding a simple HTML button
 
<input onclick="GetMessage()" type="submit" value="Get Message" />

 
Step 4. Adding the JavaScript code
 
function GetMessage() {
   PageMethods.Message(OnGetMessageSuccess, OnGetMessageFailure);
}
 
The "OnGetMessageSuccess" is the name of the JavaScript function that will be called if the request is successful. Whereas the "OnGetMessageFailure" will be called if an exception is thrown.
 
function OnGetMessageSuccess(result, userContext, methodName) {
   alert(result);
}
function OnGetMessageFailure(error, userContext, methodName) {
   alert(error.get_message());
}
  prateek replied to dipa ahuja
04-Nov-11 01:48 AM
[System.Web.Services.WebMethod()]
public static string Message()
{
   //I want to access sever control in this function like as
lable1.text = textbox.text;


//how can i access server control in this function

Page page = (Page)HttpContext.Current.Handler;
((Lable)page.findcontrol("lable1")).text = ((Textbox)page.findcontrol("lable1")).text;


//page.findcontrol return null and page.controls.count return 0
}
Create New Account
help
html in code-behind This is an article about rendering a control in code-behind page in the html content of the page. This kind of requirements are usually needed while creating a CMS. Please have a look while creating a web site where we have to render a user control in our page. There are more than one methods available to use the user control in our page. Here in this article I am going to explain these methods. Please have a look in html text. I was working on CMS project, where the requirement was a html page whose contents were going to be added from admin section. And now the new requirement for the html page was a contact us section in the html page which would be created by dynamic controls depending on the page. For example, I had a Event Page. So the admin had created a dynamic form
content-disposition" , "attachment;filename = Employee.xls" ); Response.Charset = "" ; Response.ContentType = "application / ms-excel" ; System.IO. StringWriter oStringWriter = new System.IO. StringWriter (); System.Web.UI. HtmlTextWriter oHtmlTextWriter = new System.Web.UI. HtmlTextWriter (oStringWriter); grdvResult.AllowPaging = false ; grdvResult grdvResult.AllowSorting = false ; grdvResult.RenderControl(oHtmlTextWriter); Response.Write(oStringWriter.ToString()); Response.End(); } / / / also in the page write the following lines too public override void VerifyRenderingInServerForm( Control control) { / * Confirms that an HtmlForm excel file using System.IO; using System.Web.UI.HtmlControls; protected void Button2_Click( object sender, EventArgs e) { Response.Clear(); Response.AddHeader( "content-disposition" , "attachment;filename = e1.xls" ); Response.Charset = "" ; Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application / vnd.xls" ; System.IO.StringWriter stringWrite = new System.IO StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); StringWriter swriter = new StringWriter(); HtmlTextWriter hwriter = new HtmlTextWriter(swriter); HtmlForm frm = new HtmlForm(); this .GridView1.Parent.Controls.Add(frm
Call a page method using jquery. Hi, I need to call a web method on server using jquery Can anyone give me solution to this. Its urgent. Thanks. HI YOU CAN CALL THE PAGE METHOD LIKE THIS .PLEASE SEE THIS EXAMPLE HERE I AM LOADING THE USERCONTROL USING JQUERY BY CALLING PAGEMETHOD <%@ Page Language = "C#" AutoEventWireup = "true" CodeFile = "LoadControlDynamically.aspx.cs" Inherits = "LoadControlDynamically" %> <!DOCTYPE html PUBLIC "- / / W3C / / DTD using System.Web.UI.HtmlControls; using System.IO; public partial class LoadControlDynamically : System.Web.UI.Page { protected void Page_Load( object sender, EventArgs e) { } [WebMethod] public static string Result( string controlName) { return RenderControl(controlName); } public static string RenderControl( string controlName) { try { Page page = new Page(); UserControl userControl = (UserControl)page.LoadControl(controlName); userControl.EnableViewState = false ; HtmlForm form = new HtmlForm
System.Web.UI.HtmlControls; using System.Data.SqlClient; public partial class ExportGridView : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) { GridView1.DataSource = BindData(); GridView1.DataBind(); } } private string ConnectionString { get { return @"Server = localhost;Database = Northwind DataSet ds = new DataSet(); ad.Fill(ds, "Categories"); return ds; } protected void ExportToExcel_Click(object sender, EventArgs e) { Response.Clear(); Response.AddHeader("content-disposition", "attachment; filename = FileName.xls"); Response.Charset = ""; / / If you the line below / / Response.Cache.SetCacheability(HttpCacheability.NoCache); Response.ContentType = "application / vnd.xls"; System.IO.StringWriter stringWrite = new System.IO.StringWriter(); System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite); GridView1.RenderControl(htmlWrite); Response.Write(stringWrite.ToString Parameters[0] = true ; objMainApp.GetType().InvokeMember( "Visible" , BindingFlags.SetProperty, null , objMainApp, Parameters); objMainApp.GetType().InvokeMember( "UserControl" , BindingFlags.SetProperty, null , objMainApp, Parameters); } catch (Exception theException) { MessageBox.Show(theException.Message, "Error" ); } } try this and let me know hi, here is