如何使用链接按钮在页面之间传递值?

问题描述

我想在页面之间传递 factory_id。但是,数据列表中的链接按钮不起作用。 我可以知道为什么它不起作用以及如何使它起作用吗?

提前致谢。

这是 .aspx 文件中的代码

<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>  

同时对于 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()); 
            }
        }
         
    }

解决方法

您有链接按钮的 CommandName,但没有 commandArugment?

所以:

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

所以要选择 e.CommandArugment,您需要在链接按钮中设置它。