Text Property Confusion - asp.net

I am not sure what is happening, maybe someone can clarify.
The scenario is simple, I have a form that I am submitting to update a DB.
So on Page_Load, I set each field to the current value in the current object.
Example:
txtFirstName.Text = empInfo.FirstName // FirstName = Jane
txtLastName.Text = empInfo.LastName
// Etc
Now at runtime it may be edited by the user, typical textbox stuff.
When I run my button click to update it will always return the assigned Text value previously and not the new user-edited value.
Let's say the user edits the field:
First Name: [ Joe ]
If I were to print txtFirstName.Text, it is STILL Jane
Note: This does not happen if the Text property is never set, in that case, it works as expected

Sounds like the code that assigns your txtFirstName.Text control/property is running again after post back and overwriting the new value. Make sure that you initialization code is wrapped in a check for (!IsPostBack) to ensure it is only run the first time the page is accessed an not with every post back (update) to the page.
Post your Page_Load code or where you do the initialization and we can probably confirm this is the issue.

Related

Cannot add new correlated record to new created record

When I create new record in Google AppMaker and then try to add correlated record to this new one I get this warning in console:
com.google.apps.appmaker.client.datasource.AbstractModelDataSource
WARNING: Could not find element with key RecordKey{key=private$7,
model key=...
Both datasources are set to:
Manual save mode
Automatically load data
The problem doesn't appear when I refresh the page or try to add correlated record to other existing record.
Anybody knows what could be a reason for this error?
App Maker doesn't allow to have unsaved changes on both ends of relation, most likely it is a reason, why you recieve error message in the first case. But in theory it should work once you save one of the relation ends (first save one of the records and then link them and save again):
var countryDs = app.datasources.Country;
var capitalDs = app.datasources.Capital;
countryDs.createItem();
countryDs.item.Name = 'United States';
countryDs.saveChanges(function() {
capitalDs.createItem();
capitalDs.item.Name = 'Washington, D.C.';
capitalDs.item.Country = countryDs.item;
capitalDs.saveChanges();
});
OK, I fixed it.
I have two forms. First to create item. Second to edit data. In the first form page need to be set to:
On Detach: Clear Changes To Datasource.
Datasources need to be set to autosave.

Checking whether a Session exists, if not creating a new session in VB.Net

I'm new to this so I'm having a difficult time figuring this thing.
I have a ASP.Net application which has two controls, 'lblSessionState' which has a value - "There is no session"
'btnSessionState' which has a value - "Start Session"
What i want to know is how to check whether a Session exists. For example, how do I check whether there is a Session called "MySession" and if that exists how to assign its value to 'lblSessionState' as text value.
If "MySession" does not exist how do I create it and assign a value.
Also I want to know when a button is clicked how to detect the post back and clear the session.
Thank you so much in advance.....
HYG
if(Session["MySession"]==null)
Session["MySession"] = "Your value";
lblSessionState.Text = Session["MySession"].ToString();
check the following link for more details http://www.w3schools.com/asp/asp_sessions.asp

Send an ok/cancel confirm with database data? ASP.Net VB

Can someone help me with this:
I want to send a confirm message to the user if a button is clicked. But the problem is that I want server data on the message.
For Example:
|¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯|
| You are going to delete |
| 4 messages, are you sure? |
| |
| |OK| |Cancel| |
|___________________________|
The number 4 is brought from database with a Stored Procedure.
If the user clicks OK it should do some other code, if cancel is clicked, it should just return...
This is for a website on ASP.net using Visual Basic.
Please help!
Update:
After #T.S. answer I came up with this:
'Method for receiving the number...
'messages = numberReceived
Page.ClientScript.RegisterStartupScript(Me.GetType, "Confirmation", "<script> if (confirm('You will delete " & messages.ToString & " messages, are you sure?')) { ConfirmOnClean() }; </script>", False)
And the javascript function ConfirmOnClean() gets called correctly. What can I do to call a method on the codebehind after the user clicked OK?
Update:
I Finally solved it by Just adding a hidden field on the form:
<asp:HiddenField ID="cleanStatus" Value="dontClean" runat="server" />
And then on the Confirm on clean function I just did:
function ConfirmOnClean() {
$('#<%=cleanStatus.ClientId %>').val('clean');
__doPostBack('<%=btnClean %>','');
}
And on the Page_Load method on my codebehind I added:
If cleanStatus.Value.Equals("clean") Then
OnConfirm() 'Method for calling the stored procedure for deleting the messages.
End If
I hope this helps someone!
Your question is lacking concrete code. So here is one option:
Assume you have some Id. You call stored proc
Dim message as String
Using cmd as new SqlCommand(...
cmd.ExecuteNoQuery()
' retrieve your parameters here
message = cmd.Parameters(0).Value
End Using
Now code returns to the user with the values you retrieved. You can register script block to show the message immediately on the page http://msdn.microsoft.com/en-us/library/z9h4dk8y%28v=vs.110%29.aspx
When user User clicks Ok, you can, for example, set a hidden field with a value and invoke postback. YOur aspx page will read the value and invoke DB-call to delete values.
You need to provide more details to get more concrete solution here

What is the most efficient way of filling in details on a web form?

Take a standard web page with lots of text fields, drop downs etc.
What is the most efficient way in webdriver to fill out the values and then verify if the values have been entered correctly.
You only have to test that the values are entered correctly if you have some javascript validation or other magic happening at your input fields. You don't want to test that webdriver/selenium works correctly.
There are various ways, depending if you want to use webdriver or selenium. Here is a potpourri of the stuff I'm using.
Assert.assertEquals("input field must be empty", "", selenium.getValue("name=model.query"));
driver.findElement(By.name("model.query")).sendKeys("Testinput");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("Testinput", selenium.getValue("name=model.query"));
With webdriver only:
WebElement inputElement = driver.findElement(By.id("input_field_1"));
inputElement.clear();
inputElement.sendKeys("12");
//here you have to wait for javascript to finish. E.g wait for a css Class or id to appear
Assert.assertEquals("12", inputElement.getAttribute("value"));
Hopefully, the results of filling out your form are visible to the user in some manner. So you could think along these BDD-esque lines:
When I create a new movie
Then I should see my movie page
That is, your "new movie" steps would do the field entry & submit. And your "Then" would assert that the movie shows up with your entered data.
element = driver.find_element(:id, "movie_title")
element.send_keys 'The Good, the Bad, the Ugly'
# etc.
driver.find_element(:id, "submit").click
I'm just dabbling in this now, but this is what I came up with so far. It certainly seems more verbose than something like Capybara:
fill_in 'movie_title', :with => 'The Good, the Bad, the Ugly'
Hope this helps.

Is this a KnockoutJS bug or am I doing multiple bindings wrong?

I've been working my way through the KnockoutJS documentation and tried to modify example 3 of the "Writeable computed observables" section in this page.
The example basically shows a textbox and displays a message if the user enters a non-numeric value to the textbox. I tried to modify the code so that the textbox has a pink background when the message appears.
The problem is when you enter a invalid value the textbox turns pink as expected but the value you entered is replaced with what was originally there. I have no idea why this behavior is occurring since everything worked fine before I added the style binding to get the pink background. Try removing the style binding and notice how the behavior changes when you enter an invalid value.
What's going on?
The code is below or try out this jsfiddle.
<p>
Enter a numeric value:
<input data-bind="value: attemptedValue
,style: {backgroundColor: lastInputWasValid() ?
'transparent' :
'pink' }"/>
</p>
<div data-bind="visible: !lastInputWasValid()">That's not a number!</div>
function MyViewModel() {
this.acceptedNumericValue = ko.observable(123);
this.lastInputWasValid = ko.observable(true);
this.attemptedValue = ko.computed({
read: this.acceptedNumericValue,
write: function (value) {
if (isNaN(value))
this.lastInputWasValid(false);
else {
this.lastInputWasValid(true);
this.acceptedNumericValue(value); // Write to underlying storage
}
},
owner: this
});
}
ko.applyBindings(new MyViewModel());
EDIT: Here's another fiddle with the style binding removed. Try appending the letter 'a' and taking focus out of the textbox. Notice how the letter 'a' stays there. Try that with the original fiddle textbox and notice how it is removed. The only change between the two fiddles is the presence of the style binding.
If the value is NAN than it is never written to the model, therefore the input will be updated to the existing value of the model when the onblur event is fired.
this.acceptedNumericValue(value); // Write to underlying storage
Is the code that updates when the value is numerical. You can see that it is not in the else block.
So I sent an email to the KnockoutJS user group and got a reply in about 7 hours (not too shabby).
Sadly, Google Groups confuses me and I have no idea how to reply to the fellow who cleared up my question to tell him to come on over here and post his answer so I guess I'll do it for him. All credit goes to John Earles of the KO user group.
It make sense to me.
In your example without the style, Knockout does not have to re-render
your input (only the error), so the value stays the same.
In your example with the style, Knockout does have to re-render your
input (to add the style), so BOTH bindings execute and it reads the
value - which is the last accepted value.
Here is a version that saves the attempted value into one of two
observables, and reads from the appropriate one based on
lastInputWasValid:
http://jsfiddle.net/jearles/VSWfr/

Resources