Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

retrieve datalist item

Status
Not open for further replies.

calvi

Technical User
May 26, 2003
87
0
0
NL
I displayed data from the database in a page with datalist.datasource in aspx.vb and in the aspx page the itemtemplate within the <asp:Datalist>
e.g.:
Name: <asp:Label ForeColor=brown ID="Label1" runat="server" Text='<%# Bind("Name") %>' EnableViewState="False" /><br />
Address: <asp:Label ForeColor=brown ID="Label2" runat="server" Text='<%# Bind("Address") %>' EnableViewState="False" /><br />
<asp:Button ID="btnReserve" runat="server" Text="Go to Reservation" OnClick="btnReservation_Click" /><br />

So now I want to retrieve the "Name" item in another page named Reserve.aspx, when I click on "btnReserve".

How can I solve this?
 
this is about how it would work.
Code:
<asp:Button
   Id="MakeReservation"
   runat="server"
   CausesValidation="false"
   Text="Make a Reservation"
   OnClientClick=<%# string.Format("javascript: window.location('Reservation.aspx?name={0}); return false;", Eval("Name")) %> />
Code:
private string name;
protected override void OnLoad(EventArgs e)
{
   name = Response.QueryString["name"];
}

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
the compiler doesn't recognize the .QueryString
it states: 'System.Web.HttpResponse' does not contain a definition for 'QueryString'
 
Request.QueryString["name"]

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
when I click on the button I get the following: Microsoft jScript runtime error: Object doesn't support this action. I use Visual Web Developer 2005 Express Edition.
 
probably a problem with the javascript. google javascript redirect for the exact functions/syntax

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
It's working. But I want now to combine beside retrieving data from datalist to another page, also to retrieve values from the fields on the current page to another page: Reservation.aspx. I want to combine these on the samE button id: MakeReservation. I add herein the OnClick=MakeReservation_Click. But this is not functioning. This button does not enter in the method MakeReservation_Click where I retrieve the values in the fields with Context.Items["Adres"] = lblAdres.Text; Is there another solution?
 
no it wouldn't. the code i provided above makes a call on the client using js, not to the server.

you have 2 options:
1. use js on the client to avoid a post back
2. use c# on the server (requires a postback) and redirect.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
How do I retrieve the datalist values e.g.: Text='<%# Bind("Name") %> with the use of c# on the server and redirect? in stead of javascript?
 
assign an arbitrary command name to the button along with some form of command arguement (id of data item, or row index, or something)
then define the DataItemCommand event for the datalist within this event use a switch/if statment to determine the name of the command and execute the appropiate procedure.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
I can't solve it yet. In my Seek.aspx I have this datalist:
<asp:DataList ID="DataList1" runat="server" OnItemCommand="Retrieve_Data">
<ItemTemplate>
Name: <asp:Label ForeColor=brown ID="Label1" runat="server" Text='<%# Bind("Name") %>' EnableViewState="False" /><br />
Address: <asp:Label ForeColor=brown ID="Label2" runat="server" Text='<%# Bind("Address") %>' EnableViewState="False" /><br />
City: <asp:Label ForeColor=brown ID="Label3" runat="server" Text='<%# Bind("City") %>' EnableViewState="False" /><br />
Country: <Asp:Label ForeColor=brown ID="Label4" runat="server" Text='<%# Bind("Country") %>' EnableViewState="False" /><br />
<asp:Button ID="btnReserve" runat="server" Text="Go to reserveform" CausesValidation="false" OnClick="btnReserve_Click" CommandName="select" />
</ItemTemplate>
</asp:DataList>

In my Seek.aspx.cs:
protected void btnReserve_Click(object sender, EventArgs e)
{
Context.Items["Arrival"] = lblArrival.Text;
Server.Transfer("Reserve.aspx");
}
protected void Retrieve_Data(object sender, DataListCommandEventArgs e)
{
if (((Button)e.CommandSource).CommandName == "select")
Context.Items["Name"] = ((Label)DataList1.Items[0].FindControl("Label1")).Text;
Context.Items["City"] = ((Label)DataList1.Items[0].FindControl("Label3")).Text;
Context.Items["Country"] = ((Label)DataList1.Items[0].FindControl("Label4")).Text;
}
I don't know if this "Retrieve_Data" is correct.

And in Reserve.aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
string curItem = DropDownList1.SelectedItem.ToString();
string arrival = Context.Items["Arrival"].ToString();
string name = Context.Items["Name"].ToString();
string cIty = Context.Items["City"].ToString();
string country = Context.Items["Country"].ToString();

lblArrival.Text = arrival;
lblName.Text = name;
lblCity.Text = city;
lblCountry.Text = country;
}

Retrieving arrival data is OK, but retrieving data from datalist: Bind("Name"), Bind("City") and Bind("Country") is not successful yet.
Can you help me out?
 
remove the click event on the button in the datalist and just use the commandname. move the code from the click event into the command event.

you don't need to cast the sender within the command event. use e.CommandName and e.CommandArgument to get what you need. if you need more than that use MyDataList.Items[e.ItemIndex].FindControl("control to find");

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
In the button asp:Button ID="btnReserve" I put also CommandArgument='<%# Eval("Name") %>' and within the command event I use the e.CommandArgument. It works just for "Name". But I want also other items like Address and City. How do I solve multiple values?
 
i think the commandeventargs contains an item index property. use this to get the remaining details from the data item. DataListItem item = MyDataList.Items[e.ItemIndex];

another option is to set the command argument as the id of the object in the row and query the domain/database for the information required.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top