So if the user selects a value from a dropDownList and clicks the button, the ID is passed to the code behind(which is where I want it)
E.g.
<asp:DropDownList ID="DropDownList" runat="server" AppendDataBoundItems="true" DataTextField="Company_Name" DataValueField="id">
<asp:ListItem Text="---Select---" Value="0" />
</asp:DropDownList>
So dataValueField will pass the ‘id’ of the selected record in the DD.
However On the same page I am using a repeater to display records that have been previously chosen from the drop down, Beside each record i have a ‘Change Prices’ button which I want to perform a task when clicked, all works fine however all I need is the same ‘id’.
So is this done in a similar way? E.g
<asp:Repeater ID="repeaterShowName" runat="server">
<HeaderTemplate>
<tr>
<th>
<asp:Label ID="SubConName" Text="Current SubContractors" runat="server"></asp:Label>
</th>
</tr>
</HeaderTemplate>
<ItemTemplate>
<tr>
<td>
<asp:Label ID="SubCon" Text='<%# Bind ("Company_Name") %>' runat="server"></asp:Label>
</td>
<td>
<asp:LinkButton ID="AddNewBOQLink" runat="server" OnClick="EditPricesForSubContractor" CssClass="bottomhyperlink">Change Prices</asp:LinkButton>
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
//So Do I add in DataValueField=”id” inside the link button, so when the user selects the hyperlink beside the name, it will automatically have the ‘id?
You should use the
CommandArgumentproperty of the LinkButton to pass the id to the method that handles the click.So your
LinkButtonwill now look like this:And you want to add the following property to your Repeater:
The method that will handle the command event will look like this
Also, note that I removed the
OnClickproperty from the LinkButton. It is not needed as you are handling the click via the repeater’sOnItemCommandmethod.In addition, you may want to consider adding a
CommandNamepropery to the LinkButton. This is used to identify which command you wish to execute. Currently, you only have a single command: Change Prices. But in the future you may want to add an additional button with a different command.In order to do this, add the following property to the LinkButton:
And modify the
repeater_Commandmethod to handle theCommandName. Like so: