数据列表 itemCommand 功能不起作用

Data List itemCommand function is not working

我已经尝试创建一个带有 itemcommand 函数的数据列表,尽管由于某种原因该程序似乎没有进入 itemcommand 的函数。

aspx

<asp:DataList ID="DataList1" runat="server" OnItemCommand="DataList1_ItemCommand" DataKeyField="jobID">
    <ItemTemplate>
       <div class="jobContainer">
          <div class="jobDetails">
             <span class="jobName"><%# Eval("jobName") %></span><br /> 
             <hr class="style13">
             <a class="Details">    <b>Requirments: </b><span ><%# Eval("jobRequirments") %> WPM</span>   </a> 
             <a class="Details">  <b>Salary: </b><span ><%# Eval("jobSalary")%> Shekel per hour</span>  </a>
              
             <a class="Details">   <b>City: </b><span ><%# Eval("jobCity")%></span> 
              <asp:Button ID="Button1" runat="server" CommandName="Details" Text="Show Details" />


        </div>
      </div>
    </ItemTemplate>
</asp:DataList>

隐藏代码:

protected void Page_Load(object sender, EventArgs e)
{
    // getjobs returns a dataset
    Service.Service a = new Service.Service();
    DataList1.DataSource = a.getjobs();
    DataList1.DataBind();
}

protected void DataList1_ItemCommand(object source, DataListCommandEventArgs e)
{
    if (e.CommandName == "Details")
    {
        Response.Redirect("Home.aspx");

    }
}

我怀疑您在使用 Page.IsPostBack 时遇到问题,因此请确保当 Page 不是 PostBack in Page_Load 时绑定您的 DataList,如下所示:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack) 
   {
        //Bind your DataList
        Service.Service a = new Service.Service();
        DataList1.DataSource = a.getjobs();
        DataList1.DataBind();
   }
}

查看有关 Page.IsPostBack

的更多信息