Click here to Skip to main content
13,863,412 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

What I have tried:

if (HttpContext.Current != null)
Page page = (Page)HttpContext.Current.Handler;
TextBox TextBox1 = (TextBox)page.FindControl("txtVerticalName");
Updated 6-Dec-18 10:44am
Rate this: bad
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 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;


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 handlers from jquery you'll probably find examples of doing all of these things.
Rate this: bad
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;
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
Web01 | 2.8.190214.1 | Last Updated 6 Dec 2018
Copyright © CodeProject, 1999-2019
All Rights Reserved.
Layout: fixed | fluid

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