Data List itemCommand function is not working

95 views Asked by At

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

    }
}
1

There are 1 answers

0
Selim Yildiz On BEST ANSWER

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