Click here to Skip to main content
13,799,870 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi All,

how to access aspx textbox control in static method in asp.net

What I have tried:

if (HttpContext.Current != null)
{
Page page = (Page)HttpContext.Current.Handler;
TextBox TextBox1 = (TextBox)page.FindControl("txtVerticalName");
}
Posted 19-Jul-16 1:36am
Updated 6-Dec-18 10:44am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

You can't as your controls only exist as part of a post-back as they are created and managed by the asp.net page liftecycle. If your code is instigated via an ajax call from the client then the only thing available to you is the data the ajax method sends and that is not enough to re-create all of the controls on the page and manage all their events.

If you need to access any control values then they need to be read by the calling js and passed to your method as a parameter;

handler.ashx?name=valueofnamecontrolhere

Likewise if you want to update a control you need to send the updated value in the result, say by returning json like

{name: "John"}

and the calling js needs to get that data and update the relevant control to give it its new value.

If you google calling asp.net handlers from jquery you'll probably find examples of doing all of these things.
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Hi,You can use this sample:

Page page = (Page)HttpContext.Current.Handler;
TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");
TextBox TextBox2 = (TextBox)page.FindControl("TextBox2");
Num1=TextBox1 .text;
Num2=TextBox2 .text;
  Permalink  
v3
Comments
Richard Deeming 7-Dec-18 8:52am
   
That's effectively identical to the code already posted in the question.

It's not clear why the OP was struggling to make that code work. But posting the same code again clearly isn't going to help.

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web06 | 2.8.181214.1 | Last Updated 6 Dec 2018
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100