开发者

listview OnItemCommand dosen't fire up

when i press either linkbutton in the listviews it dosen't fire up at all

<div>
   <%
      String[] d1 = { "1", "2", "3" };
      String[] d2 = { "4", "5", "6", "7" };
      ListView1.DataSource = d1;
      ListView1.DataBind();
      ListView2.DataSource = d2;
      ListView2.DataBind();
   %>
   <asp:ListView ID="ListView1" runat="server" OnItemCommand="lv_command">
      <LayoutTemplate>
          <ul>
              <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
          </ul>
      </LayoutTemplate>
      <ItemTemplate>
          <asp:LinkButton ID="LinkButton1" runat="server">LinkButton</asp:LinkButton>
      </ItemTemplate>
   </asp:ListView>
   <asp:ListView ID="ListView2" runat="server" OnItemCommand="lv_command">
      <LayoutTemplate>
          <ul>
              <asp:PlaceHolder ID="itemPlaceholder" runat="server" />
          </ul>
      </LayoutTemplate>
      <ItemTemplate>
          <asp:LinkButton ID="LinkButton2" runat="server">LinkButton</asp:LinkButton>
      </ItemTemplate>
   </asp:ListView>
</div>

protected void lv_command(object send开发者_StackOverflow中文版er, ListViewCommandEventArgs e)
{
  int i = 0;
}


Set the CommandName property of each of the LinkButtons, for instance:

 <asp:LinkButton ID="LinkButton1" runat="server" CommandName="MyCommand">LinkButton</asp:LinkButton>

Thus when the ItemCommand event is raised you can detect whether it is fired from a link button as follows:

     protected void lv_command(object sender, ListViewCommandEventArgs e)
    {
  if(e.CommandName == "MyCommand")
  {
    //do something
  }
}

Also it is more performance-wise to bind the listview on initial load only and bind it again from certain event handlers when needed:

    protected void Page_Load(object sender, EventArgs e)
{
   if(!Page.IsPostBack)
   {
    String[] d1 = { "1", "2", "3" };
    String[] d2 = { "4", "5", "6", "7" };
    ListView1.DataSource = d1;
    ListView1.DataBind();
    ListView2.DataSource = d2;
    ListView2.DataBind();
   }
}


Move the logic that performs data binding into the code-behind:

protected void Page_Load(object sender, EventArgs e)
{
    String[] d1 = { "1", "2", "3" };
    String[] d2 = { "4", "5", "6", "7" };
    ListView1.DataSource = d1;
    ListView1.DataBind();
    ListView2.DataSource = d2;
    ListView2.DataBind();
}

protected void lv_command(object sender, ListViewCommandEventArgs e)
{
  int i = 0;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜