ASP.net help regarding Javascript and input from textboxes - c#

Here's the basic flow of the webpage I'm working on:
User clicks a button, date /time picker pops up (works).
A time string fills a textbox, say, 4:00 AM (works).
User then has to select one of two radio buttons, which should read the time in the text box, radiobutton1 adds 4 hours, radiobutton2 adds 4.5 (does not work).
The total hours is then displayed in another textbox (haven't gotten to this yet).
Other than the 1st item (which retrieves the dates from a server), most of the work is done by Javascript.
So here's the Javascript I'm working on:
function setHours(setting)
{
var tempVal = document.getElementById('<%=Text6.ClientID%>');
var nHours;
var textHours = new DateTime(tempVal);
alert(tempVal);
textHours = getHours
if (setting==true) {
nHours = 4;
alert("4");
}
else {
nHours = 4.5
alert("4.5");
}
textHours.setHours(textHours.getHours() + nHours);
document.getElementById("text8").value = textHours;
}
This function is called when the user clicks on either one of the two radiobuttons (one passes true, the other passes false). Notice that I have "alert"s scattered throughout, because initially, I couldn't get the entire function to fire. So I commented out everything but the "if statements" and the "alert"s, and lo and behold it works fine.
I'm assuming the problem is somewhere in the var, perhaps, how "tempVal" retrieves the value in text6.
Here's the code for the textbox.
<form id='frmRequestApplicationForm' name='frmRequestApplicationForm' action='#' method='post'>
<input class="textboxdefault" type="text" name="Starttime" id="Text6" value="<%=(requestApplicationForm.StartimeError.ToString().Equals("")?requestApplicationForm.StarttimeDate.ToShortDateString().Equals("1/1/0001")?"":requestApplicationForm.StarttimeDate.ToShortTimeString(): requestApplicationForm.Starttime)%>"/>
</form>
Building the program nets me a "Text6 does not exist". I'm not sure where the code refuses to work.
Replacing "<%=Text6.ClientID%>" with a simple "Text6" makes the entire Javascript function fail.
Could I have some help on this?

Nevermind.
I replaced "document.getElementById('<%=Text6.ClientID%>');" with
document.frmRequestApplicationForm.Text6.value;
Seems to work fine now.

Related

Clicking in Internet Explorer

I have to write simple program for clicking in Internet Explorer, have started, but due to my little experience in programming, stucked. On the first page 11.html I need to click button with text "Sign1", on the next "Sign2"
//11.html
<input type="button" class="button" onclick="document.location='21.html'" value="Sign1">
//21.html
<input type="button" class="button" onclick="document.location='31.html'" value="Sign2">
What is wrong? Second button is not working.
Public Class Form1 Public ie = CreateObject("InternetExplorer.Application")
Private Sub Button2_Click_1(sender As Object, e As EventArgs) Handles Button2.Click
ie.Visible = True
ie.navigate("http://....com/11.html")
Do
Loop Until ie.ReadyState = 4
Actions()
End Sub
Private Sub Actions()
Dim eInputs = ie.Document.GetElementsByTagName("input")
For Each eInput In eInputs
If eInput.GetAttribute("value") = "Sign1" Then
eInput.Click()
Exit For
End If
Next
Threading.Thread.Sleep(5000)
//here ie href is about:blank - WHY???
eInputs = ie.Document.GetElementsByTagName("input")
For Each eInput In eInputs
If eInput.GetAttribute("value") = "Sign2" Then
eInput.Click()
Exit For
End If
Next
End Sub
End Class
I can see two problems here:
Cyrillic alphabet:
If eInput.GetAttribute("value") = "Подписать2" Then
I don't think you can hard-code these characters into the source code,
because they will not be read by the compiler. Hence, it's highly possible that the getAttribute method is returning the correct string but then it's comparing it against a ?????? one so it doesn't find it. Maybe, you might want to get them from an external source (a txt file, a database etc.).
No waiting time between the two buttons:
As you say Second button is not working. (very broad definition: is doing anything? Is raising an error?), I assume the first is. Hence, it might be you found already a way to work-around the alphabetic problem.
So I think the issue is that you don't wait enough time between one click and the other, so the page is not loaded yet when you want to click the second button..
Basically:
1) You click the button 1 in the client (let's say Google Chrome);
2) The button 1 click calls an action in the back-end, so the server will start generate the new HTML document in which you look for button 2;
3)... however, you don't give the server any time for regenerating the page so you already look for the button 2, which probably does not exist yet in your client because the server did not end the regeneration.
So, that's why it doesn't work. If this is the case, add a command to wait some time before to look for the button 2. It's not clear in which language you're developing since you referenced all the .NET languages, but looking at the code I can recognize VB.NET.
Hence, to wait 5 seconds in VB.NET before running the second part of the code:
Threading.Thread.Sleep(5000)
to be put between the first and the second For Loop

Update span from code behind ie

I am using the following back-end c# code to see if I need to update the text inside a span.
c#:
if (status1.InnerHtml != temp1)
{
status1.InnerHtml = temp1;
status1.Update();
}
html:
<span runat="server" id="status1">Status 1</span>
This works fine in chrome and firefox, but it has issues in ie.
Visually, this is what happens (only in ie):
first time:
second time:
Possible Source of Error
I noticed that status1.InnerHtml always returns Status 1; as in it never changes. This leads me to believe that this is why it is creating a second element.
This means that I need to find a way to get the the current value of the span, using something besides InnerHtml (runat="server" was supposed to solve this issue).
Looking at the code in ie, on initial load, it is displayed properly. However, the second time I execute the code,
it turns
<ext.net.direct.update id="status1"/>
<span id="status1">
Text - Transfer completed
</ext.net.direct.update/>
(Note: the closing span tag was removed)
into
<span id="el_status1_container">
<span id="status1">
Text - Transfer completed
<span id="status1">
Text - Transfer completed
</ext.net.direct.update/>
(Note: <ext.net.direct.update id="status1"/> gets removed from the code, an element with a duplicate ID is inserted)
Any pointers would be greatly appreciated! Thank you
The solution I came up with was to make a button that when clicked loads the statuses. Using javascript, I clicked the button when it is generated with this code found from this post.
function initialload() {
if (document.getElementById('Label1')) {
document.getElementById("Label1").click();
} else {
setTimeout(initialload, 15);
}
This works because it turns out ie was ignoring the initial span (weird, but hey, it's ie) and using the second one to update it.
This is a workaround, so if anyone has a better answer, please share!

Get dynamically created element by ID in C# [duplicate]

I have stumbled across a problem with my asp.net form.
Within my form the end user chooses a number of textboxes to be dynamically created, this all works fine with the following code:
protected void txtAmountSubmit_Click(object sender, EventArgs e)
{
int amountOfTasks;
int.TryParse(txtAmountOfTasks.Text, out amountOfTasks);
for (int i = 0; i < amountOfTasks; i++)
{
TextBox txtAddItem = new TextBox();
txtAddItem.ID = "txtAddItem" + i;
txtAddItem.TextMode = TextBoxMode.MultiLine;
questionNine.Controls.Add(txtAddItem);
txtList.Add(txtAddItem.ID);
}
}
However this has also caused a small problem for me, later on in my form on the submit button click, I send the results to the specified person it needs to go to (using smtp email). Again this part is fine, until I am trying to retrieve the text from these dynamically created textboxes.
What I have tried
I have tried using this msdn access server controls ID method however this was not working.
I tried to add these new textboxes to a list, however I was unsure on how to update these textboxes when they have text in them. Therefore my results were returning null because of this.
I have also looked at other questions on SO such as this however they are usually for WPF or winforms, rather than my problem with asp.net (this usually isn't an issue, but I don't need to get the text from every textbox control in my page, just the ones that were dynamically created).
I have also tried changing how I call the code that I hoped would have worked:
string textboxesText = string.Join("\n", txtList.Select(x => x).ToArray());
and then in my concatenated string (email body) I would call:
textboxesText
The problem
As they are dynamically created I am finding it difficult to call them by their id for example: txtExampleID.Text, also as I have to increment the ID's by one each time (so they don't override each other) it has made things a little bit more difficult for me.
I am not asking for a code solution, I would prefer pointers in the right direction as I am still learning.
So to sum it all up: I need to get the text from my dynamically created textboxes to add it to my email body.
The issue is these text boxes need recreated in the Load event of the page, every single time, so that both events and values can be hooked back up and retrieved.
I think the most straight forward approach, in your case, would be to extend idea #1 that you had already tried. Build a List of these controls with enough information to recreate them in Load, but you need to store that List in either ViewState or Session.
ViewState["DynamicControls"] = list;
or
Session["DynamicControls"] = list;
I would use ViewState if you can because it gets destroyed when the user leaves the page.

ASP.Net UpdatePanel, want to have html-tag as a parameter

This is my problem, I have one textbox, one button and one label. Everything is inside an updatepanel. Let's say I want to test if a valid html-tag is entered in the textbox when I press the button. In the code-behind I have a method for that.
Everythings works just fine, except when I actually enter a html-tag in my textbox. If I test < html>, it works. But not if I test .
So my question is, doesn't the updatepanel accept html-tags as parameters? Because my breakpoint for the button_click doesn't triggers.
Sorry for my bad english.. :)
Since you're in an UpdatePanel you aren't seeing the real error here...if you just remove the UpdatePanel and do a full postback you will.
Since you're entering an HTML tags, ASP.Net is stopping the postback because you're hitting Event Validation, which prevents symbols like < and > from being entered to prevent scripts attacks on your site.
See here for troubleshooting this and learn about your options. The short version is that by default you cannot enter HTML tags in the field, more specifically, you can't enter < and >

Disable selection box without removing value from post

In my current asp.net-mvc project one of my pages allows the user to select a value in a dropdown box after wich a post request is made that updates several values.
To make sure the delay from the postback doesn't confuse the user into selecting another value (and thus creating another post, creating another delay etc) I set the select's disabled attribute to true.
Disabled inputs aren't submitted to the post call however.
How can I make it visually clear to the user that work is in progress and make it imposible to select a new value without removing the input from the post?
Yes, this annoys me too.
Basically what you need to do is hide the old button and replace it with a disabled one so it looks the same to the user. That way it's still submitted but can't be doubly submitted.
Actually I've found what seems to be a duplicate of this at Problem with disabling submit buttons on form submit.
From your answer, I gather you are already using jQuery. In that case why don't you get the value of the select box, disable it, then post the value yourself?
Bonus : BlockUI is a nice jQuery plugin to, well, block the UI.
None of the answers I found in Cletus' post was entirely what I was looking for.
Here is what I came up with. It's not 100% reusable, but it does what I need and feel free to improve/edit.
$('#productMixSelectorForm').change(function() { $(this).ChangeSelection() });
jQuery.fn.ChangeSelection = function() {
var html = $('<div class="hidden">');
$(this).find('select, input').each(function() {
if ($(this).hasClass('hidden') == false) {
//Clone the original one into the hidden div
html.append($(this).clone());
//Disable the original (visible) one and make it's name unique again
$(this).attr("disabled", true);
var name = $(this).attr("name");
$(this).attr("name", name + "disabledDummy");
}
});
//Add the collection of clones to the form so they get submitted
$(this).append(html);
$(this).submit();
}

Categories