Input text box value is not changing - c#

In my .aspx page I have a 'input' html tag, also an asp button.
<input id="Name" type="text" runat="server" clientidmode="Static" />
<asp:Button Width="100" type="submit" ID="sendOrder" runat="server" OnClick="SubmitForm" Text="Submit" />
On page load, I am filling the value in input tag from code behind, like this:
Name.Value= "X";
But now if I change value of this text box from browser, lets say "Y", and click on Submit button, then I get the old value, but not the new one.
protected void SubmitForm(object sender, EventArgs e)
{
var test= Name.Value; // here I get old value
}
How can I get the altered value?

Make sure you are only setting the value to "X" when its not a postback:
if (!Page.IsPostBack){
Name.Value= "X";
}
Otherwise when clicking the submit button, the Page_Load() event will change the value from "Y" back to "X".

You need to use !IsPostBack on Page_Load shown as below:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Value= "X";
}
}
Suggestion:
We can use use <input></input> control in asp.net but best practice is to use <asp:TextBox></asp:TextBox> control instead.
Here is the sample example:
HTML
<asp:TextBox ID="Name" runat="server"></asp:TextBox>
<asp:Button Width="100" ID="sendOrder" runat="server" OnClick="SubmitForm"
Text="Submit" />
Code-behind:
protected void Page_Load(object sender, EventArgs e)
{
//it's important to use this, otherwise textbox old value overrides again
if (!IsPostBack)
{
Name.Text = "Some Value";
}
}
protected void SubmitForm(object sender, EventArgs e)
{
var test = Name.Text; //now get new value here..
}

Check for IsPostback in Page_Load so you don't overwrite the values that are submitted!

You don't need all the else portion, just do this
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
//code to execute here only when an action is taken by the user
//and not affected by PostBack
}
//these codes should be affected by PostBack
}

Related

"Old" asp:CustomValidator errors show up after clicking the Back button

I have a web form that uses a CustomValidator to check the contents of a textbox. If the textbox's contents are invalid when a "Submit" button is pressed, the error message is displayed properly and the "Submit" operation doesn't happen. When the textbox's contents are corrected and the submit button is pushed, the error disappears and the next page is presented properly. So far so good.
But, if the user then clicks the Back button in browser, the original page is displayed with the error message, even though the textbox still shows the corrected text. Can anyone offer suggestions about what I may be doing wrong?
Code front:
<asp:TextBox runat="server" ID="txtUsername" ValidationGroup="Register" />
<asp:CustomValidator runat="server" ID="valUsername" OnServerValidate="Validate_Username" ValidationGroup="Register" Display="Dynamic" />
<asp:LinkButton runat="server" ID="btnNext" OnClick="btnNext_OnClick" ValidationGroup="Register" CausesValidation="True" />
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
valUsername.ErrorMessage = Resources.Resource.RegisterPageValidateUsername;
}
}
protected void btnNext_OnClick(object sender, EventArgs e)
{
Page.Validate("Register");
if (Page.IsValid)
{
//Do stuff
}
}
protected void Validate_Username(object source, ServerValidateEventArgs args)
{
var user = GetUser(txtUsername.Text);
args.IsValid = user == null;
}

Checking if the value of a textbox is changed or not

I have a textbox filled at the page load. I want to check the value of the textbox is changed or not in the "update" button press. Any solution? TIA.
Well, you could say use client side javascript.
But, you could also do this:
Say this text box:
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button1" runat="server" Text="Done - continue" OnClick="Button1_Click" />
And our code could be this:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// code here to setup and load controls
TextBox1.Text = "Dog";
ViewState["TextBox1"] = "Dog";
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text != (string)ViewState["TextBox1"])
{
// then text box was changed
}
}

Add TAB reference to ASP.NET button

I need to navigate to the certain tab when button is clicked.
The button is
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true" OnClick="ButtonSearch_Click" />
JavaScript
// Javascript to enable link to tab
var url = document.location.toString();
if (url.match('#')) {
$('.nav-tabs a[href="#' + url.split('#')[1] + '"]').tab('show');
}
// Change hash for page-reload
$('.nav-tabs a').on('shown.bs.tab', function (e) {
window.location.hash = e.target.hash;
})
But somehow the method
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Fill Grid code
}
is replaced with
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
// Fill Grid by default
}
}
And finally the grid has no found data and has default data. That's a problem.
I have try to use this
protected void ButtonSearch_Click(object sender, EventArgs e)
{
// Fill Grid code
string url = Request.Url.GetLeftPart(UriPartial.Path) + "#AllWorkOrders";
Response.Redirect(url, true);
}
but there is a
if (!IsPostBack)
{
// Fill Grid by default
}
still ...
I mean I need somehow return user to the correct TAB but Response.Redirect establish some trouble...
Probably there is a way to add #AllWorkOrders part of QueryString to
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true" OnClick="ButtonSearch_Click" />
but I don't know how to do it.
Any clue?
I found solution that was quite easy to implemenet hahahahaa
I completely forgot about PostBackUrl property!
So the code looks like this now
<asp:Button ID="ButtonFind" runat="server" Text="Buscar" CssClass="btn btn-primary" UseSubmitBehavior="true"
PostBackUrl="~/MyPage.aspx#AllWorkOrders" OnClick="ButtonSearch_Click" />

How to I carried out Textbox value from ViewState when I postback?

I want to know that how can I trace out the value of Textbox from ViewState.
As user enters any value into Textbox and click submit button because of postback Textbox value disappears ,
But if I used ViewState in this case , then is there any way to see or display that value from Viewstate?
<html>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" /
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{
TextBox1.Text += "X";
}
In your page load use this.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (ViewState["Values"] == null)
{
ViewState["Values"] = new string();
}
}
TextBox1.Text = ViewState["Values"].ToString();
}
After that use this.
protected void Button1_Click(object sender, EventArgs e)
{
ViewState["Values"] += TextBox1.Text;
}
In the first Page_Load method, You will create a ViewState if its not a postback and its null. After that write the textbox your viewstate, in Button1_Click you will add your new textbox1 to your viewstate.

How to change view-state value dynamically?

I have textbox and button controls in my page. For textbox I have enabled view state, page-load event I am setting text box value “Hello Mr!”. Now I want to change the view state value for text box to “Hello Mr Pradeep!” when post back occurs, how can I do that? And in which all page events I can do that.
<asp:TextBox ID="TextBox1" runat="server" EnableViewState= "true"/>
<br />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
protected void Page_Load(object sender, EventArgs e)
{
TextBox1.Text = "Hello Mr!";
}
Thanks,
Pradeep
As far as i understand your question you can do this using javascript on page load event
Your page is retrieving viewstate between the init of the page and just before the page load event. So the more early attempt you can make to change the view State is in the page load.
Because if you were to modify it before its retrieving your changes would be lost.
protected override void OnInit(EventArgs e)
{
if (IsPostBack)
{
//on postback ViewSate["test"] is null
ViewState["test"] = "Valuepostback";
//Now ViewSate["test"] is Valuepostback
}
base.OnInit(e);
}
protected override void OnLoad(EventArgs e)
{
if (IsPostBack)
{
//on postback ViewState has been reloaded from the page sent and therefore the initial value set in the oninit does not exists anymore
//ViewState["test"] is MyValue
//if you want to cahnge specifically the view state do it here
}
if (!IsPostBack)
ViewState["test"] = "MyValue";
base.OnLoad(e);
}

Categories