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
How to Crop a Polygonal Area from an Image in a Winform Picturebox
Why Would I Bother to Use Task.Configureawait(Continueoncapturedcontext: False);
How to Get Web Page's Content and Save It into the String Variable
C# Generic "Where Constraint" with "Any Generic Type" Definition
Get Current Area Name in View or Controller
Best Way to Find All Factors of a Given Number
Keep Window on Top and Steal Focus in Winforms
How to Clear All Controls on a Form C#
How to Find Reason for Generic Gdi+ Error When Saving an Image
.Net Regex Matching $ with the End of the String and Not of Line, Even with Multiline Enabled
How Does Task<Int> Become an Int
How to Get User Type C# Syntax Highlighting Working Again in VS 2012 Rc
String.Replace() VS. Stringbuilder.Replace()
Namespace and Class with the Same Name