Showing posts with label series. Show all posts
Showing posts with label series. Show all posts

Saturday, March 24, 2012

Pass variables to header control (.asxc web user control)?

How can I pass variables to a header control?

Generically, I have a series of pages that all use the same header and footer controls. I want each page to have it's own values for a few variables, and the header page needs to recognize those values and change some html based on those values. The html to be changed is just class names on tds, I'm assuming for it to work that maybe I need to change that to something .net can access easier.

Specifically, I tried just making the variables in the pages's .aspx.vb files and writing them out in the header's .aspx file (right where they'd go in the control) but it's errors and tells me I need to declare the variables in the header control.Try this (in C#)

In your code behind for the Header control:


private string _YourVariable = "";

public string YourVariable
{
set
{
_YourVariable = value.ToLower();
}
}

and in the HTML of the page containing the control:


<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere"></uc1:YourHeader >

Hope this helps,
Nic
Thank you! I will work on it. I'm using vb, so I'll let you know how it goes.

Question: does the header control line allow multiple variables or just one?

Ie,


<uc1:YourHeader id="YourHeader1" runat="server" YourVariable="TheValueOfYourVariableHere" YourVar1="SomeValueY" YourVar2="SomeValueX" YourVar3="SomeValueZ" ></uc1:YourHeader

As many as you want.
Good to hear I can have several variables there, thanks.

Ok, I've had no luck creating equivalent code in vb, yet it looks so straight forward... make a local variable, set it to "", then give the local variable the value of the 1st one... does that sound about right?

:?
Update: Success!
Used this link (http://www.kamalpatel.net/ConvertCSharp2VB.aspx), followed your suggestions, and it works! Now I understand better how to approach this in the future.

Thanks for your help. :)

Pass variables to database

I have a bunch of code written by another developer. It is all in java script. The user walks through a series of instructions then answers a few questions. The java script tracks the answers, I believe they are using a cookie. At the end of the quiz they receive a certificate of completion that says Congrats (username) you got (##) out of (##) correct, etc. Each (somethinghere) being a variable. The javascript just does a document.write(something.parent);
I want to take what is in the variable and pass it to my SQL Database. I have written code in asp.net that does this but the code uses a text box and you have to put somthing in the text box then click Add and it will pass the variables on to my database. How do I do this with this javascript stuff?

Passing values from javascript to the server-side ASP code can be done by placing the value in a hidden text box, or any control which has a text property.

<SCRIPT Language="JavaScript">
document.Form1.TextBoxName.value = javascript variable
</SCRIPT>
You can retrieve the value on the server-side usingRequest.Form("TextBoxName")when the form is posted back.

hope this helps,
sivilian


Not sure where the final value you need is being displayed. You said a document.write?
Then, you may also want to put that var in a hidden html field, then, in your code behind, find the value...

string myVar = (HtmlInputHidden)(this.FindControl("myHiddenFieldName")).Value;


Hope it helps,
Zath


It seems like you want this to automagically happen, without using a text box and clicking a button.
I am assuming that you are not overly knowledgeable with web based development. So you need to know that, in general, to get info into the database the user has to do an action and post the data back to the server. This would be usually done with a form and a button. as Zath points out you can use a hidden control instead of a textbox. Your javascript that does the document.write could then call document.forms[0].submit() to do the post back to the server. This will refresh the page.
Alternatively, you could look at something fancy like AJAX or Javascript RPC (basically the same things). This is pretty advanced stuff.


Here is an example of my asp page:

<%@.ImportNamespace="System.Data.SqlClient" %>
<%@.ImportNamespace="System.Data" %>
<%@.PageLanguage="VB"Debug="TRUE" %>
<%
Dim conMMTMPAs SqlConnection
Dim strInsertAsString
Dim cmdInsertAs SqlCommand

conMMTMP =New SqlConnection("server=servername;UID=ID;PWD=password;database=DBname")
strInsert ="Insert Into SuperTrain (User,Score) Values ('" & Request.Querystring("User") &"','" & Request.Querystring("Score") &"')"
cmdInsert =New SqlCommand( strInsert, conMMTMP )
conMMTMP.Open()
cmdInsert.ExecuteNonQuery()
conMMTMP.Close()
%>
so usually i would pass something say to the url:http://www.mysite.com/page.aspx?User=Tester&Score=100
bam i have a new record in my database. I also have a page written like this:

<%@.ImportNamespace="System.Data.OleDb" %>
<%@.ImportNamespace="System.Data" %>
<%@.PageLanguage="VB"Debug="true" %>
<ScriptRunat="Server">

Sub Button_Click( sAsObject, eAs EventArgs )
Dim conAuthorsAs OleDbConnection
Dim strInsertAsString
Dim cmdInsertAs OleDbCommand
Dim strFirstNameAsString
conAuthors =New OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=" & Server.Mappath("database.mdb"))
strInsert ="Insert Into tblTest ( au_lname ) Values ( @.FirstName )"
cmdInsert =New OleDbCommand( strInsert, conAuthors )
cmdInsert.Parameters.Add("@.Firstname", txtProductName.Text)
conAuthors.Open()
cmdInsert.ExecuteNonQuery()
conAuthors.Close()
EndSub

</Script>
<html>
<head><title>Insert Parameters</title></head>
<body>
<formRunat="Server">
<h2>Add new Name</h2>
<b>First Name:</b>
<br>
<asp:TextBoxID="txtProductname"Runat="Server"/>
<p>
<asp:ButtonText="Add!"OnClick="Button_Click"Runat="Server"/>
</form>
</body>
</html>
now... I have a page not written by me that is in javascript and I need to pass the variables to an asp page or something so I can get them into my database:
here is part of the page, how do i get this javasrcipt variables into my pages?

<p><b><i><font face="Arial,Helvetica"><font size=+4> <!--RG_TITLE0--><SCRIPT LANGUAGE="JavaScript"><!--

if (parent.pass == 1)

{

document.write(parent.master_title);

}

else

{

document.write(parent.comp_title);

}

//-->

</SCRIPT><!--RG_TITLE1--> </font></font></i></b></center>

<p><br>

<center><table WIDTH="70%" >

<tr>

<td><img height=339

SRC="ribbon.png" height=399 width=159></td>

<td><i><font face="Arial,Helvetica">This certifies that</font></i>

<br>

<p><b><u><font face="Arial,Helvetica"><font size=+2> <!--RG_STUDENT_ID0--><SCRIPT LANGUAGE="JavaScript"><!--

document.write(parent.student_id);

//--></SCRIPT><!--RG_STUDENT_ID1--> </font></font></u></b>

<br>

<p><i><font face="Arial,Helvetica"> <!--RG_ACTION0--><SCRIPT LANGUAGE="JavaScript"><!--

var pass=parent.checkScores();

if (pass == 1){document.write(parent.master_act);

}

else

{document.write(parent.comp_act);

}

//--></SCRIPT><!--RG_ACTION1--> <b> <!--RG_COURSE_NAME_OR_TITLE0-->