Get Text/Value from Textbox After Value/Text Changed Server Side

Get text/value from textbox after value/text changed server side

In every postback, you are always getting the old value from your database. The solution is check if the page is being rendered for the first time (!IsPostBack) then set your MainFormTemplate's DataSource else if is being loaded in response to a postback (IsPostBack) get the txt_Name's value like this:

HtmlInputText twt;

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
using (DB_MikaDataContext data = new DB_MikaDataContext())
{
MainFormTemplate.DataSource = data.File_Projects.Where(x => x.Num_Tik.Equals("12")).ToList();
MainFormTemplate.DataBind();
}
}
else
{
twt = MainFormTemplate.FindControl("txt_Name") as HtmlInputText;
}
}

protected void btn_Update_OnClick(object sender, EventArgs e)
{
string text = twt.Value; // You will get the new value
}

Get TextBox text after text is changed by Javascript

The problem is in Enabled="false". Values of disabled inputs are not included in the POST data submitted by the browser to the server. You can try making the TextBox read-only.

If you have to keep it disabled, add a HiddenField control to the form and set its value in javascript the same way you set it for the TextBox. Then, in server-side code transfer the value from the HiddenField to the TextBox.

Get Set values of TextBox Control from server side

Wrap it in a NOT is Postback

  protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
TextBox1.Text = "123";
}
}

or remove it completely:

protected void Page_Load(object sender, EventArgs e)
{
//not here
}

<asp:TextBox ID="TextBox1" Text="123" runat="server"></asp:TextBox>

call serverside script upon textbox value change

You should use GetPostBackEventReference and create a js function like this:

function Textbox1_OnChange() {
<%=Page.GetPostBackEventReference(Textbox1, "TextChanged")%>;
}

Then you will be able to call this from jQuery after you change the text:

 $('#Textbox1').val("TreeView Selected Value");  // your code  
Textbox1_OnChange(); // new line

This will automatically postback the page and invoke your Textbox1_TextChanged server-side event.



Related Topics



Leave a reply



Submit