How to pass value between pages using link button?

165 views Asked by At

I want to pass the facility_id between pages. However, the link button in the data list does not work. May I know why it does not work and how to make it work?

Thank you in advance.

Here is the code from the .aspx file:

<asp:DataList ID="DataList1" runat="server" DataSourceID="SqlDataSource1" RepeatColumns="2" RepeatDirection="Horizontal" HorizontalAlign="Center" Width="70%" DataKeyField="facility_id" OnItemCommand="DataList1_ItemCommand">
    <ItemTemplate>
        <asp:Label ID="facility_idLabel" runat="server" Text='<%# Eval("facility_id") %>' Visible="False" />
        <table style="width:100%;">
            <tr>
                <td style="height: 53px; width: 500px; text-align: right;">
                    <asp:Label ID="Label2" runat="server" CssClass="ClientFacility2ndTitle" Text='<%# Eval("facility_name") %>'></asp:Label>
                </td>
                <td class="lblLogin" style="height: 53px; width: 220px">
                    <asp:LinkButton ID="LinkButton1" runat="server" BorderStyle="None" CssClass="Button" CommandName="BookNow">Book Now</asp:LinkButton>
                </td>
            </tr>
        </table>
    </ItemTemplate>
    </asp:DataList>
</div>

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebConfigcs %>" SelectCommand="SELECT [facility_id], [facility_name] FROM [facility]"></asp:SqlDataSource>  

Meanwhile for the code in aspx.cs:

public partial class ClientFacility : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
        }
         
        protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if(e.CommandName == "BookNow")
            {
                MessageBox.Show(e.CommandArgument.ToString()); 
                Response.Redirect("EditFacility.aspx?facility_id=" + e.CommandArgument.ToString()); 
            }
        }
         
    }
1

There are 1 answers

0
Albert D. Kallal On

you have a CommandName for the link button, but no commandArugment?

so:

<asp:LinkButton ID="LinkButton1" runat="server" 
   BorderStyle="None" CssClass="Button" 
   CommandName="BookNow"
   CommandArgument = '<%# Eval("facility_id") %>'
       >Book Now</asp:LinkButton>
           

So to pick up e.CommandArugment, you need to set that in the link button.