User overwrite input.text value - c#

Hi I got this problem when trying to make an "edit" page for a profile stored in the database.
To make it simple, I got a textbox where i get a value from the database and apply it to the textbox with textbooks. text = "value";`.
Works great this far.
Then I want the user to be able to edit the text in the textbox and then press save, which should save the edited content of the textbox. But it doesn't, it saves the value i applied with textbox.text.
I get the value from the textbox in the button click function with textbox.text.
What am i doing wrong? - does the input from the user not overwrite the .text value?
TextBoxNavn.Text = "Navn";
protected void ButtonSave_Click(object sender, EventArgs e)
{
using (SqlCommand command = new SqlCommand())
{
command.Connection = connection;
command.CommandType = CommandType.Text;
command.CommandText = "UPDATE Politikere SET Navn = #Navn WHERE ID = #ID";
command.Parameters.AddWithValue("#Navn", TextBoxNavn.Text);
}
}
Between the first line and the update statement, the user has edited the text in the textbox, but it still saves the value from the first line.
If i remove the first line and enter a value in the textbox it saves the entered value. That's why i suspect that the issue is the .text - but i don't know how else to do.enter code here
EDIT
Sorry if i am not clear. but all values are entered and works great. Too great for some.
there's just too much code to point out small snippets for this. But by narrowing down the issue, I have found out that if i don't insert the database value with TextBoxNavn.Text in the page_load, it updates the value in the database with the new value. But when inserting it, user edited value does not get saved, but the value i inserted from the database does
EDIT II
The steps:
i get the data from the database and insert it into TextBoxNavn with TextBoxNavn.Text
The user will go to the page and edit the textbox, writing a new name in the TextBoxNavn
The user press "save" button
The value from the TextBoxNavn is retrieved by TextBoxNavn.Text
The value saved to the database is the value from step 1, but i need it to be the value from step 2

I am assuming that you are using DataBind() inside your page load. If yes please change it to`
if(!Page.IsPostBack)
{
DataBind();
}
`
or else share the complete code where you bind the data to your textbox. If you bind your controls on PageLoad, Bind will be called before the control events and in that case the value you entered will be over ridden by the Databind() function. Pleae refer the following link for the asp.net page life cycle.
http://msdn.microsoft.com/en-us/library/ms178472(v=vs.100).aspx

Related

Losing values after refreshing button asp.net

I have the following project :
It's a page that on Page_Load it fills a TextBox named Email and a TextBox named UserName with a value obtained from asking a database.
Then there is this button, if the email is not null(user is not registered) it will let you register, otherwise it will let you change the email linked to your username.
The thing is, when trying to modify the email, doing an update query, the page preloads, taking the new value placed on Textbox Email the same that is retrieved from the database, making so it will never change.
I've tried to see if it executes the query and it does.
I've tried everything, keeping the variable on a hidden label, creating two different buttons with no luck as when it reloads the code those values are empty again.
I was thinking if I could keep the variable somehow that isn't cookies.
I think You know What is happening.. On every Post back the Page_Load event resetting your Textbox Value
Use IsPostBack to bind the value only on 1st load of page
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//bind dropdown and fill textbox here
TxtName.Text = "Your values";
GetDropdowns();
}
}
I hope this will solve your issue
I totally agree with Kanis XXX, you can use IsPostBack to fill the values only at the start page, and not on other postbacks. In my experience, there are some other advices for your problems:
Using Viewstate, Session state,... to keep your working variable. You can have more detail here: https://kimphuc.wordpress.com/2009/10/18/the-difference-between-viewstate-sessionstate-cookies-and-cache-in-asp-net/
Try to use UpdatePanel, this could be useful in some cases, let you refresh or update data just a part of your page, not the whole page. https://msdn.microsoft.com/en-gb/library/bb398864%28v=vs.100%29.aspx

How can I grab a textfield value in Ext.Net?

I know there's a lot of information on this, but mine doesn't seem to be working out so well. I have a form that's created in C# in a form panel. My textfields are created like below:
Ext.Net.Panel panel = new Ext.Net.Panel();
Field field = null;
field = new TextField();
field.Name = "FieldTitle";
field.ID = "FieldID";
panel.Add(field);
this.searchform.Add(panel);
this.searchform.Add(new Ext.Net.Button()
{
Text = "Search",
Handler = "getID();"
});
When the search button is hit, then a JavaScript function is called which performs a store reload:
Ext.getCmp("#Gridpanel").getStore().reload({});
On the reload, I want to read the form fields and use the text for another part of the code. However, the below code doesn't seem to be working:
if(X.isAjaxRequest){
var ctrl = X.GetCmp<TextField>("FieldID");
string value = ctrl.Value.ToString();
}
I can get inside the 'if' statement, but ctrl comes back as null. Is there something else I need to include to grab the text field data?
EDIT
So I'm realizing that I'll have to pass an array of the search field ID's to a JavaScript function, then send a dataset back to the server side in order to implement the search. Just to throw it out there, is there a way to dynamically create controls (ie; a TextField) in C# and then get the value from those controls after an event is fired (ie; a button click)?
You can try to get the textfield using
Ext.getCmp("#FieldID") or X.getCmp("#FieldID"). FieldID is the client ID for TextField.
Use developer Tools to check the ID for the TextField

Asp.net controls not updating after a postback

I'm writing code to read data from asp controls to update records in a database. I've been debugging the last day and I've tracked it back to something that I ought to have noticed before.
The code first populates the controls with the existing values from the database.
When I click SAVE, it should read the current values from the controls and save with those.
Unfortunately, what it's actually doing is using the values of the controls before a change was made to them. It's not seeing the change to the controls.
Here's a sample:
<asp:TextBox ID="OtherCourseName_5" runat="server"></asp:TextBox>
Here's the corresponding behind code in the btnSave_onClick() function:
int object_number=5;
string other_course_name_string
= "OtherCourseName_" + object_number.ToString().Trim();
TextBox ocn = utilities
.utils
.FindControlRecursive(this.Master, other_course_name_string) as TextBox;
I'm using the FindControlRecursive() I found somewhere on the web. It works, I'm sure, but just in case, I tried to address the control directly as OtherCourseName_5.Text.
Even if I just display the value in OtherCourseName_5.Text, it gives the original value.
I use this same page for both entering new data and for editing data. It works fine when I enter the data. That is, it correctly sees that the TextBox control has changed from empty to having data. It's only when I invoke the edit function on the page (by passing edit=true). I invoke it this way by adding the switch edit=true as a query string (the program correctly reads that switch, gets to the appropriate area of code, prints out all the correct values for everything - except the contents of the controls!).
The page is far too complicated to post the entire thing. I've tried to convey the essential details. It's entirely possible that I've made a simple coding error, but it's seeming more a possibility that I fundamentally misunderstand how pages are processed.
Is there anything known that can make it seem as though the value of a control has not been changed?
Note 1: I thought perhaps I had to go to another field after I entered the data, but I tried that and it's still a problem.
Note 2: I'm using both TextBox and DropDownList controls and have the same problem with both.
Note 3: These controls are on a panel and the page is using a SiteMaster. I haven't had any problem with that and don't think the problem is there, but I'm down to questioning the laws of the physics at this point.
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
//populate controls with data from database
}
}
When you do a postback before the postback handler is evaluated the PageLoad event is raised
so if you don't avoid to rebind your control they will be loaded with the values from the database. And then the postback event will save them to db
Asp.net Page Lifecycle
(source: microsoft.com)

C# SelectedValue in a Listbox

I'm adding data to my listbox using this code :
SqlCommand cmd1 = new SqlCommand("SELECT U_Naam, U_Voornaam, User_ID FROM Login ORDER BY U_Naam ASC", con);
dr = cmd1.ExecuteReader();
int teller = 0;
while (dr.Read())
{
hulp = dr["U_Naam"].ToString() + " " + dr["U_Voornaam"].ToString();
lbNiet.Items.Add(hulp);
lbNiet.Items[teller].Value = dr["User_ID"].ToString();
teller++;
}
When I run the program and select an item in the list I want to get it's value (Using selectedvalue). When I do this it allways gives a nullref (no value). I've read somewhere the selected item is lost after a postback ? How can I solve this ?
thanks !
replace these lines
lbNiet.Items.Add(hulp);
lbNiet.Items[teller].Value = dr["User_ID"].ToString();
with
lbNiet.Items.Add(new ListItem(hulp,dr["User_ID"].ToString();));
I don't think the selected item is lost after PostBack under normal circumstances - my guess is that you are accidently populating the ListBox again after PostBack so that when you make a selection, the system hits the Page_Init / Page_Load (or whatever event you choose to populate the ListBox in) and recreates all the options. Doing this would overwrite the currently selected option.
If that's the case it's easily avoided by check for PostBack first before repopulating the list.
Are you running that code in the Load of your ASP.NET page without checking to see if it is a Postback? You should make sure to wrap your loading code in a if (!IsPostback) {} block if you're doing it in the load of the page.
Databinding your data instead of adding in a loop will help.
Are you using the ViewState for your page?
If yes, check the IsPostBack property of the page, and don't populate your list after postback.
you can use Request.Form to find the posted value
or I think if you use a scriptmanager and an updatepanel it refills the box after a postback

reading text of a text box

I am working on designing a user profile web page in asp.net using c#.
I first load the values of text boxes from database and put them in the text box:
txt_Name.Text = "somestring";
The user can then change the text in the text box to modify their profile.
However when I read txt_Name.Text it shows me the "original" value instead of what the user entered.
More clearly:
First I set the value of a text box to something:
txt_Name.Text = "somestring";
Then the user changes the value of the text box to something else in the gui
Then I read the value of text box:
Response.Write(txt_Name.Text);
In 3 the value is the one from 1 instead of the one from 2
It sounds like you aren't checking the Page.IsPostBack property (http://msdn.microsoft.com/en-us/library/system.web.ui.page.ispostback.aspx) when you are setting the initial textbox value, so it is always being set no matter how the page is invoked.
private void Page_Load()
{
if (!IsPostBack)
{
txt_Name.Text = "somestring";
}
}
Its all in the page life cycle have a look at this page
http://msdn.microsoft.com/en-us/library/ms178472.aspx

Categories

Resources