-2

I am attempting to populate a html page by passing in values using QueryString and my values are passing in the QueryString but my limited to NO knowledge of JS is preventing me from being able to deduce why the textbox on the page isn't populating with the passed value.

This is my HTML showing the JS Function

<%@ Page Title="" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true"
CodeBehind="Test.aspx.cs" Inherits="TestProject.Pages.Test" %>
<asp:Content ID="ContentHeaderID" ContentPlaceHolderID="MainContent" runat="Server">
<div class="BackgroundOfWhite">
    <font class="BB">Select Instructor:</font>
    <asp:DropDownList runat="server" ID="dropdown1" 
AutoPostBack="true" CssClass="DropDownLists" ></asp:DropDownList>    
    &nbsp;&nbsp;&nbsp;&nbsp;     
    <asp:Button runat="server" ID="btnOpenPage" 
CssClass="Buttons" Text="Open Page With Params" OnClick="btnLoadPage_Click" />
    <div class="White"></div>        
</div>
<script type="text/javascript">
    document.getElementById('InstructorName').value = Instructor;
</script>



This is my C# info here

protected void btnLoadPage_Click(object sender, EventArgs e)
{
    string openthis = "http://whiskeyinthewatertestofsendingdata.html";
    string Instructor = "Tyler Moore";
    Response.Redirect(openthis+"?"+Instructor);
}

I feel that the issue is I am not actually calling the JS function to populate the textbox on the hmtl page, but how would I do such?

EDIT: This is the html behind the textbox

<input id="InstructorName" name="InstructorName" maxlength="255" style="width: 240px;">



EDIT 2
I see this 1st few lines of HTML of the page...does this mean on the page load they force the fields to have a null value (which of course would mean their is no way to achieve what I am after)

<head>
<script type="text/javascript">
 var forcefieldstonull = 
 {
 "InstructorName":null,
  "InstructorClass":null,
  "InstructorBuilding":null,
  "InstructorRoomNum":null
};

Rashid Abib
  • 189
  • 1
  • 4
  • 13
  • It's not the same answer exactly so I won't report you, but have a look at http://stackoverflow.com/questions/1045845/how-to-call-a-javascript-function-from-php – online Thomas Oct 15 '15 at 14:50
  • `"http://whiskeyinthewatertestofsendingdata.html";` hopefully that isn't the real URL that you are trying to use... – Ron Beyer Oct 15 '15 at 14:51
  • @RonBeyer - no it is not the real URL just threw a garbage URL in their for the ? purpose – Rashid Abib Oct 15 '15 at 14:55
  • @user2754599 When you flag as a duplicate, you are not reporting the user (which makes it sound like the user has done something very bad). Instead, you are flagging *the question*. – mason Oct 15 '15 at 15:09
  • In response to Edit 2: not necessarily. There may be some code afterwards that checks the URL. You'll have to look and see. – mason Oct 15 '15 at 16:06

2 Answers2

2

Try this:

protected void btnLoadPage_Click(object sender, EventArgs e)
{
    string openthis = "http://whiskeyinthewatertestofsendingdata.html";
    string Instructor = "Tyler Moore";
    Response.Redirect(openthis+"?Instructor="+Instructor);
}

and then, on your page, change your javascript function to do it like this:

<script type="text/javascript">
    document.getElementById('InstructorName').value = '<%=Request.QueryString["Instructor"]%>';
</script>
  • That is not populating the textbox on the page with the value of Instructor – Rashid Abib Oct 15 '15 at 15:05
  • Well, where's your '' ?? – Caio César S. Leonardi Oct 15 '15 at 15:07
  • @mason that would be the case if he was using an element, but it's just a simple that has no runat="server" – Caio César S. Leonardi Oct 15 '15 at 15:28
  • 1
    @RashidAbib go for the basics: before the ''document.getElementById'', do a simple javascript alert. Include this line: `alert('Instructor: ' + '<%=Request.QueryString["Instructor"]%>');` – Caio César S. Leonardi Oct 15 '15 at 15:33
  • the alert is fired on the page that launches btn_pageLoad() never on the openthis page. – Rashid Abib Oct 15 '15 at 15:42
  • @RashidAbib The JavaScript code needs to go on the receiving page, the page the user gets redirected to. – mason Oct 15 '15 at 15:44
  • @RashidAbib Then you won't be able to accomplish what you want to do. Unless that page is already set up to receive parameters via the URL somehow or has some other mechanism of prepopulating fields, you can't do that from a redirect. Why can't you edit? Is the page on a 3rd party site? – mason Oct 15 '15 at 15:46
  • @Mason - yes it is a 3rd party site. Was attempting to auto-populate data from our database into the 3rd party site as opposed to it having to manually be entered one by one – Rashid Abib Oct 15 '15 at 15:51
  • @RashidAbib In that case, you'll have to inspect their HTML source code to see if they might have something on the client side that can pull values out of the URL. Or check and see if they have an API you can use. And lastly, you may be able to contact the website operators to see if something exists or if they could add something for you. – mason Oct 15 '15 at 15:53
  • @Mason - what in the page HTML would be an indicator that they are set-up to pull values from the URL? – Rashid Abib Oct 15 '15 at 15:55
  • @RashidAbib It would be in the JavaScript. You'd see it inspecting the URL, there might be some checks on `window.location` or `$.url` or something along those lines. – mason Oct 15 '15 at 15:57
1

You have to wait for the page to be loaded completely before you can change it's elements. Though the javascript is at the bottom it comes to my mind that it might be executed before the InstructorName div is rendered.

You should surround it with window.onload() to make sure that it is executed after the page is fully loaded. https://developer.mozilla.org/de/docs/Web/API/GlobalEventHandlers/onload

Additionally what you can do is simply check the Browsers console if the script gives you an error.

Danmoreng
  • 2,367
  • 1
  • 19
  • 32
  • I used the window.onload tip in the link you provided and still no luck. The textbox is not being populated. How would I check the Browser Console? – Rashid Abib Oct 15 '15 at 15:05
  • 2
    Use Firefox Chrome or even Internet Explorer and press F12. – Danmoreng Oct 15 '15 at 15:06
  • I let the page load, and hit f12 and the console window is empty – Rashid Abib Oct 15 '15 at 15:09
  • Are you sure your server side passes the value of "Instructor " (I'm guessing its a variable in C#?) in the html the client gets? What I mean is, if you check the source code of the webpage and scroll down to the script part - is the value you want in the textbox actually there? – Danmoreng Oct 15 '15 at 15:13
  • If I step through my code, Instructor has a value, and it is displayed in the QueryString in the URL of the page also. – Rashid Abib Oct 15 '15 at 15:15
  • When you answered I was editing the last sentence in my last comment: What I mean is, if you check the source code (rightclick somewhere on the page -> show source code) of the webpage and scroll down to the script part - is the value you want in the textbox actually there? – Danmoreng Oct 15 '15 at 15:18
  • No - I do not see my value mentioned anywhere when I click on View Page Source. I see the field name InstructorName but not the value I am attempting to fill the textfield with. – Rashid Abib Oct 15 '15 at 15:30
  • @RashidAbib When you look at the page HTML and you don't see the value filled, what is the URL? – mason Oct 15 '15 at 15:36
  • The URL shows the passed value http://whiskeyinthewatertestofsendingdata.html?Instructor=Tyler%20Moore – Rashid Abib Oct 15 '15 at 15:44