Changing the datasource.Id of a grid as its page is opening

  • Thread starter Thread starter Monty
  • Start date Start date
M

Monty

Hi there,
From one page, I have created a dropdown list of employees. When an employee
is selected, I would like another page to open, based upon the employee.
Here is my question...
How do I change the datasource.id of a grid on another page as it is
opening?
Thank you, Mark
 
You just need to pass the selected value on a query string to the second
page and pick it up there.

Here's the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]


Here's pg1.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Redirect("pg2.aspx?id=" & _
DropDownList1.SelectedValue.ToString)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="0">George</asp:listitem>
<asp:listitem value="1">John</asp:listitem>
<asp:listitem value="2">Mary</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" />&nbsp;</div>
</form>
</body>
</html>

Here's pg2.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim strID As String
If Not IsNothing(Request("id")) Then
strID = Request("id")
Label1.Text = strID
dg.DataSourceID = strID
End If

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="Label1" runat="server"></asp:label><br />
<br />
<asp:datagrid id="dg" runat="server" ></asp:datagrid></div>
</form>
</body>
</html>
 
Ken, thank you. It works well.
If you could, could you expand on the ?id in your code as follows...
"Response.Redirect("pg2.aspx?id=" & _"
Thanks again, Mark

Ken Cox - Microsoft MVP said:
You just need to pass the selected value on a query string to the second
page and pick it up there.

Here's the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]


Here's pg1.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Redirect("pg2.aspx?id=" & _
DropDownList1.SelectedValue.ToString)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="0">George</asp:listitem>
<asp:listitem value="1">John</asp:listitem>
<asp:listitem value="2">Mary</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" />&nbsp;</div>
</form>
</body>
</html>

Here's pg2.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim strID As String
If Not IsNothing(Request("id")) Then
strID = Request("id")
Label1.Text = strID
dg.DataSourceID = strID
End If

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="Label1" runat="server"></asp:label><br />
<br />
<asp:datagrid id="dg" runat="server" ></asp:datagrid></div>
</form>
</body>
</html>


Monty said:
Hi there,
From one page, I have created a dropdown list of employees. When an
employee is selected, I would like another page to open, based upon the
employee. Here is my question...
How do I change the datasource.id of a grid on another page as it is
opening?
Thank you, Mark
 
Thanks, it worked fine.
Can you explain the ?id in your code?
"Response.Redirect("pg2.aspx?id=" & _"
Thanks again, Mark

Ken Cox - Microsoft MVP said:
You just need to pass the selected value on a query string to the second
page and pick it up there.

Here's the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]


Here's pg1.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Redirect("pg2.aspx?id=" & _
DropDownList1.SelectedValue.ToString)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="0">George</asp:listitem>
<asp:listitem value="1">John</asp:listitem>
<asp:listitem value="2">Mary</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" />&nbsp;</div>
</form>
</body>
</html>

Here's pg2.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim strID As String
If Not IsNothing(Request("id")) Then
strID = Request("id")
Label1.Text = strID
dg.DataSourceID = strID
End If

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="Label1" runat="server"></asp:label><br />
<br />
<asp:datagrid id="dg" runat="server" ></asp:datagrid></div>
</form>
</body>
</html>


Monty said:
Hi there,
From one page, I have created a dropdown list of employees. When an
employee is selected, I would like another page to open, based upon the
employee. Here is my question...
How do I change the datasource.id of a grid on another page as it is
opening?
Thank you, Mark
 
Hi Mark,

The part starting with the question mark (?) is called a querystring. It
simulates the Get technique in HTTP, requesting a resource by providing
additional data.

The format is a name/value pair separated by an equals (-) sign as in

id=444

You can read lots more here by searching for request querystring on the MSDN
site.

Ken


Monty said:
Ken, thank you. It works well.
If you could, could you expand on the ?id in your code as follows...
"Response.Redirect("pg2.aspx?id=" & _"
Thanks again, Mark

Ken Cox - Microsoft MVP said:
You just need to pass the selected value on a query string to the second
page and pick it up there.

Here's the idea. Let us know if it helps?

Ken
Microsoft MVP [ASP.NET]


Here's pg1.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Button1_Click _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Response.Redirect("pg2.aspx?id=" & _
DropDownList1.SelectedValue.ToString)

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:dropdownlist id="DropDownList1" runat="server">
<asp:listitem value="0">George</asp:listitem>
<asp:listitem value="1">John</asp:listitem>
<asp:listitem value="2">Mary</asp:listitem>
</asp:dropdownlist><br />
<br />
<asp:button id="Button1" runat="server" onclick="Button1_Click"
text="Button" />&nbsp;</div>
</form>
</body>
</html>

Here's pg2.aspx

<%@ Page Language="VB" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<script runat="server">

Protected Sub Page_Load _
(ByVal sender As Object, _
ByVal e As System.EventArgs)
Dim strID As String
If Not IsNothing(Request("id")) Then
strID = Request("id")
Label1.Text = strID
dg.DataSourceID = strID
End If

End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:label id="Label1" runat="server"></asp:label><br />
<br />
<asp:datagrid id="dg" runat="server" ></asp:datagrid></div>
</form>
</body>
</html>


Monty said:
Hi there,
From one page, I have created a dropdown list of employees. When an
employee is selected, I would like another page to open, based upon the
employee. Here is my question...
How do I change the datasource.id of a grid on another page as it is
opening?
Thank you, Mark
 
Back
Top