Click here to Skip to main content
15,913,486 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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
Updated 6-Dec-18 9:44am

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.
 
Share this answer
 
v2
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;
 
Share this answer
 
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)



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