我试图创建一个带有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_Load
中的PostBack
时绑定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
的更多信息