简体   繁体   中英

Data List itemCommand function is not working

I've tried to create a datalist with itemcommand function altough it seems that the program doesn't get into the function of the itemcommand for some reason.

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>

Code Behind:

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");

    }
}

I suspect you are having an issue with Page.IsPostBack , so make sure that you bind your DataList when Page is not PostBack in Page_Load as following:

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();
   }
}

See more info about Page.IsPostBack

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM