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
Related
I have a WebForm ASP label and button. I am setting the label's value on page load. For example, the label text on page load is 2 items selected. This comes from the database. Then if the user changes the selection then it counts the selected values by jQuery and sets the text as 5 items selected.
When I click on the submit button to save changes, again it resets to 2 items selected. I didn't use an update panel. I don't know what is going on here. Can anyone please explain this scenario?
$("#lblCount").text($('#grdProducts').find('input#chkSelect:checked').length + ' Complementary Products added');
C# on page load:
lblCount.Text = ComplementaryproductCount.ToString() + " Complementary Products added";
I do not understand why the label text is changed on button click. I couldn't find anything while debugging too.
Thanks
When you set lblCount.Text in your code, that value is set into the ViewState of the page... that means when your page is posted back to the server (to handle an event, etc) ASP.Net knows what lblCount.Text was originally and can re-render the HTML with the same value.
As part of that post-back to the server, the browser will send back that ViewState along with any input control values (things like textboxes, dropdowns, hidden field).
What it does NOT do is post-back any changes you might have made to the elements on the page via things like jQuery (other than input controls I mentioned above).
The result is that although you've changed the element on the screen, the server knows absolutely nothing about that change, and it will re-send the original HTML for the label back to the browser.
Your only option is to do something as suggested by #John in his comment... you need to store the fact the element has changed in an input, and then use that.
For instance...
<asp:Label runat="server" id="lblCount" />
<asp:HiddenField runat="server" id="hdnCount" />
function updateCount(newCount) {
$("#<%=lblCount.ClientID%>").text("Count: " + newCount.toString());
$("#<%=hdnCount.ClientID%>").val(newCount.toString());
}
Then in your code-behind you can have...
if (!Page.IsPostBack)
{
var count = 1;
lblCount.Text = String.Format("Count: {0}", count);
hdnCount.Value = count.ToString();
}
else
{
lblCount.Text = String.Format("Count: {0}", hdnCount.Value);
}
I have an asp.net web form which on one page has a multi line textbox. When the user types in this field and continues the details are displayed on a confirmation page and displays as the user entered
Example
This is the first line
This is the second line
But when I click my edit button (which directs me back to my page) my textbox displays as
This is the first line<br /><br />This is the second line
I want it to keep its styling but don't know how to do this. The details are stored in the session.
Code behind
protected void Step07SubmitButton_Click(object sender, EventArgs e)
{
Session["Step07OtherDetailsField"] = Step07OtherDetailsField.Text.Replace("\r\n", "<br />");
Response.Redirect("/Quotation/pg3.aspx");
}
I tried the following in my Page_Load
Step07OtherDetailsField.Text.Replace("<br />", "\r\n");
and also
Step07OtherDetailsField.Text.Replace("<br />", Environment.NewLine);
But for some reason when I debug it, it says that my .text is empty but the previous wording is actually still displayed in the field and the <br /> is also still displayed.
You don't need to replace anything. You just store value in session and display it to any textbox, it will maintain \r and \t.
If you want to display result in span then display result to textbox with multiline then apply css for no border,etc.
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
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
What I'm trying to achieve/plan, is whereby a page loads with a set of inputs, e.g. TextBox, Radio List etc. Taking TextBox as an example, there is a button for the user to "Add" another textbox to the page (in the same group), e.g. Member1, Member2, Member3 etc etc.
Two questions:
I could add these with Javascript, however the resultant "save" on postback would not get these inputs? If so, how?
The form needs to work without Javascript as well, so postback to dad another control is fine, however if I click the "add" button again, it will only ever add one control.
protected void btnAdd_OnClick(object sender, EventArgs e)
{
holder.Controls.Add(new TextBox { ID = "txtControl1" });
}
You can access the dynamic controls in the postback by name, using the following syntax "Page.Request.Form[""].ToString();". This uses the "name" attribute, not the "id" attribute.
I guess you can have one of these scenarios :
1- to save any new control ( textbox ) data in a session variable .. for example save name and value in which when any post back you can draw then again from this variable
2- If the only post back done from Add button you can do its function without doing post back as in this example http://msdn.microsoft.com/en-us/library/ms178210.aspx