Showing posts with label index. Show all posts
Showing posts with label index. Show all posts

Saturday, March 24, 2012

Pass Variable through URL

Hi, I'm new to programming I want to know how to pass variable through URL
for example
http://localhost/index.html?username=check
then when the index page finish loading it can show up:
your name is checkWelcome to the ASP.NET forums, iwinmac.

Let me know whether you're learning C# or VB.NET, and I'll write an example in that language.

By the way, you wouldn't really want to put usernames into the URL like that. If you're planning to introduce a login system, you should look at theSecurity QuickStart tutorial.
I like to learn in VB.NET. I put username into the URL just for example. once I know how to get the variable from the URL I can put more stuff in to it.
use the following code in order to retreive the username into a string:

dim username as string = request.querystring("username")

If "http://localhost/index.html?username=check" was entered into a browser than the string would return "check"

As SomeNewKid2 said, The QuickStart Tutorials are always a great place for information.
Here is the code for your firstpage.aspx, where you ask the user for his or her username:

<%@. Page Language="vb" %>
<script runat="server"
Sub SubmitButton_Click(ByVal sender As Object, ByVal e As EventArgs)

' get the user's name from the textbox
Dim Username As String
Username = UsernameTextbox.Text

' we need to "encode" the username
' so that "Charlie Brown" becomes "Charlie+Brown"
Dim UsernameEncoded As String
UsernameEncoded = HttpUtility.UrlEncode( Username )

' now, we can move off to the next page
Response.Redirect( "secondpage.aspx?name=" & UsernameEncoded )

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
Please enter your username:
<asp:Textbox id="UsernameTextbox" runat="server" />
<asp:Button id="SubmitButton" text="Submit" onclick="SubmitButton_Click" runat="server" />
</form>
</body>
</html>




Here is the code for secondpage.aspx, where you receive the username and display it:
<%@. Page Language="vb" %>
<script runat="server"
Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)

' get the user's name from the querystring
Dim UsernameEncoded As String
UsernameEncoded = Request.Params( "name" )

' we need to "decode" the username
' so that "Charlie+Brown" becomes "Charlie Brown"
Dim Username As String
Username = HttpUtility.UrlDecode( UsernameEncoded )

' now, we can display the username
WelcomeMessage.Text = "Welcome " & Username

End Sub

</script>
<html>
<head>
</head>
<body>
<form runat="server">
<asp:Label id="WelcomeMessage" runat="server" />
</form>
</body>
</html>




This is very simple. You would want to add a "RequiredFieldValidator" to ensure that the user actually enters a username.

However, I'll leave that as part of your learning.

I hope this gets you started. Any questions, please ask.

Pass variable into a user control

Please help, I've been working on this for hours and can't seem to find an answer online.

I have a main page (index.aspx) that uses a usercontrol (DropDownSearch.ascx). Inside the usercontrol are 3 dropdownlists with a code behind file that fills them with data from a query. The query is based on a variable which is sent by the main (index.aspx) file. Inside the code behind for the main page (index.aspx) I try to send the variable over to the codebehind of the user control, but it keeps failing.

Here is the user control info in index.aspx:
<%@dotnet.itags.org.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%>
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch
Here is the code for the index.aspx.cs:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MainPage
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected SearchMenus DropDownSearch;

private void Page_Load(object sender, System.EventArgs e)
{

}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
}

}
}
}

Here is the user control code behind (dropdownsearch.ascx.cs):
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace Enthone
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public string SqlCat1, SqlCat2;

private void Page_Load(object sender, System.EventArgs e)
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=Enthone");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

ThanksHow is it failing? Do you get an exception? (If so, what is the complete exception text?) Incorrect results? What's going wrong?

Don
When I go to build it from vs.net I get this error in the index.aspx.cs file, "c:\inetpub\wwwroot\MyApp\index.aspx.cs(25): The type or namespace name 'SearchMenus' could not be found (are you missing a using directive or an assembly reference?)
"
Okay, that means that it can't find the SearchMenus namespace. Where is that located? In another project? Is it open in your VS.NET solution file? You'll need to add a reference to it in Solution Explorer, by right-clicking on References in the project with Index.aspx and adding a reference to the project or DLL.

There are lots of ways to configure these things in VS.NET though, so explain where SearchMenus is located and we should be able to figure out what's wrong.

Don
O.K. I got the whole thing working, but would definitly appreciatea look at my code to make sure I'm handling it the right way...

User Control Info on index.aspx:


<%@.Register TagPrefix="MyControl" Tagname="DropDownSearch" src="http://pics.10026.com/?src=dropdownsearch.ascx"%
<MyControl:DropDownSearch ID="SearchMenus" Runat="server"></MyControl:DropDownSearch>

Code Behind of index.aspx:


using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for index.
/// </summary>
public class index : System.Web.UI.Page
{
protected System.Web.UI.WebControls.RadioButton Functional;
protected System.Web.UI.WebControls.RadioButton Pwb;
protected System.Web.UI.WebControls.RadioButton Decorative;
protected System.Web.UI.WebControls.RadioButton Microelectronics;
public string SqlCat1, SqlCat2;
protected MyNamespace.dropdownsearch SearchMenus;

private void InitializeComponent()
{

}

private void Page_Load(object sender, System.EventArgs e)
{
}

protected void CategorySelected(object sender, System.EventArgs e)
{

if (Functional.Checked)
{
SearchMenus.SqlCat1 = "1";
SearchMenus.SqlCat2 = "1";
SearchMenus.DbCall();
}

if (Decorative.Checked)
{
SearchMenus.SqlCat1 = "2";
SearchMenus.SqlCat2 = "2";
SearchMenus.DbCall();
}

if (Pwb.Checked)
{
SearchMenus.SqlCat1 = "3";
SearchMenus.SqlCat2 = "3";
SearchMenus.DbCall();
}

if (Microelectronics.Checked)
{
SearchMenus.SqlCat1 = "4";
SearchMenus.SqlCat2 = "4";
SearchMenus.DbCall();
}

}
}
}


Code behind of User Control:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace MyNamespace
{
/// <summary>
/// Summary description for tier.
/// </summary>
public class dropdownsearch : System.Web.UI.UserControl
{
SqlConnection SearchConnection;
protected DataSet MyDataSet = new DataSet();
public DropDownList TradeNames, Applications, Processes;
public Label MyLabel;
private string _SqlCat1;
private string _SqlCat2;

public string SqlCat1
{

get
{
return _SqlCat1;
}
set
{
_SqlCat1 = value;
}
}

public string SqlCat2
{

get
{
return _SqlCat2;
}
set
{
_SqlCat2 = value;
}
}

private void Page_Init(object sender, System.EventArgs e){

}

private void Page_Load(object sender, System.EventArgs e)
{
}

public void DbCall()
{
SearchConnection = new SqlConnection("Server=localhost;uid=sa;password=;database=db");
string MySqlStatement = "sp_DropDownsSql " +SqlCat1+", "+SqlCat2;
SqlDataAdapter MyAdapter = new SqlDataAdapter(MySqlStatement, SearchConnection);
MyAdapter.Fill(MyDataSet);

TradeNames.DataSource = MyDataSet.Tables["Table"];
TradeNames.DataBind();

Applications.DataSource = MyDataSet.Tables["Table1"];
Applications.DataBind();

Processes.DataSource = MyDataSet.Tables["Table2"];
Processes.DataBind();

SearchConnection.Close();
}

override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}

private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}

}
}

Thanks