datlist, select item
-
ive never used datalists before and i'd really appreciate some help? What i'd like to do is select an item (eg, hyperlink or button) from a datalist on one webform, which will either: a) transfer to another datalist on another webform, showing more details of that particular record ... or b) open up the details of that record directly underneath. Ive no idea where to start and the more i look it up the more i get confused Chrissy Callen
-
ive never used datalists before and i'd really appreciate some help? What i'd like to do is select an item (eg, hyperlink or button) from a datalist on one webform, which will either: a) transfer to another datalist on another webform, showing more details of that particular record ... or b) open up the details of that record directly underneath. Ive no idea where to start and the more i look it up the more i get confused Chrissy Callen
Hi there Chrissy. As a starting point, you may want to look at the ASP.NET QuickStart Tutorials[^] - the tutorial Introducing Web Forms gives some good examples of working with a Datalist. I think you'd find the other tutorials helpful too, particularly those on data access and databinding. Here's one approach you could take to have a hyperlink in your datalist open a detail record on the same page. The DataList control has an
ItemCommand
event that can be triggered when a button/linkButton in the list is clicked. The button/linkButton can specify aCommandName
andCommandArgument
with details that are passed along with the event. So here's an example of aDataList
that is assigned the proceduremyDataList_ItemCommand
for itsItemCommand
event, with aLinkButton
whoseCommandName
is set to "DetailsLink" (whatever makes sense for us) andCommandArgument
is bound to the value from the database field [Sample1]. The template is also showing the value from field [Sample2].<asp:DataList id="myDataList" runat="server" RepeatColumns="2"
GridLines="Both"
OnItemCommand="myDataList_ItemCommand" >
<ItemTemplate>
<asp:LinkButton id="lbDetails" runat="server"
Text='<%# DataBinder.Eval(Container.DataItem, "Sample1") %>'
CommandName="DetailsLink"
CommandArgument='<%# DataBinder.Eval(Container.DataItem, "Sample1") %>' />
<br />
<%# DataBinder.Eval(Container.DataItem, "Sample2") %>
</ItemTemplate>
</asp:DataList>The event handler
myDataList_ItemCommand
can then inspect theCommandArgument
value to find the id of the detail record that needs to be displayed, and call an procedure to do so:void myDataList_ItemCommand(object o, DataListCommandEventArgs e)
{
// is the command a click in our details link button?
if (e.CommandName == "DetailsLink")
{
//if so, let's run a query to get the details
DisplayDetailRecord(e.CommandArgument.ToString());
}
}The detail record could be displayed using a Panel whose Visible property is set to true by our custom
DisplayDetailRecord
procedure. So here's a full example page in C#. It assumes the sourc