datalist selectedindexchanged未触发

时间:2011-11-02 06:49:50

标签: c# asp.net datalist

页面加载:

 protected void Page_Load(object sender, EventArgs e)
  {

       tb.Columns.Add("Id", typeof(int));
        tb.Columns.Add("FileName", typeof(string));
        tb.Columns.Add("FilePath", typeof(string));
        tb.Columns.Add("Index", typeof(int));

        newtb.Columns.Add("Id", typeof(int));
        newtb.Columns.Add("FileName", typeof(string));
        newtb.Columns.Add("FilePath", typeof(string));
        newtb.Columns.Add("Index", typeof(int));

        path = objGetBaseCase.GetAllImagesfromGroup(CaseId);
        for (int i = 0; i < path.Count; i++)
        {
            ArrayList alst = path[i];
            tb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

        }
        msgError.Text = "";

        dlstImage.DataSource = tb;
        DataBind();
        List<ArrayList> t = new List<ArrayList>();
        if (newpath.Count > 0)
        {
            t = newpath;
            newpath = t;
            for (int i = 0; i < newpath.Count; i++)
            {
                ArrayList alst = newpath[i];
                newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

            }
            ViewState["tempimage"] = newpath;
            dlstSelectedImages.DataSource = newtb;
            DataBind();
        }

}

Datalist SelectedIndexChanged:

  protected void dlstSelectedImages_SelectedIndexChanged(object sender, EventArgs e)
  {
    indexId = Convert.ToInt32(dlstSelectedImages.DataKeys[dlstSelectedImages.SelectedIndex]);
  }

的DataList:

 <asp:DataList ID="dlstSelectedImages" runat="server" RepeatDirection="Horizontal"
                                RepeatColumns="5" CellSpacing="8" DataKeyField="Id"  OnSelectedIndexChanged="dlstSelectedImages_SelectedIndexChanged">
         <ItemTemplate>
               <asp:ImageButton ID="Image"  runat="server" ImageUrl='<%#"~/Controls/ShowImage.ashx?FileName=" +DataBinder.Eval(Container.DataItem, "FilePath") %>'
                                        CommandName="Select" OnCommand="ImageSelect_Command" CommandArgument='<%# Eval("Id").ToString() +";"+Eval("FilePath")+";"+Eval("Index")+";"+Eval("FileName") %>' /><br />
                   <asp:Label ID="nlbl" runat="server" Text="Figure"></asp:Label><%# dlstSelectedImages.Items.Count + 1%>
          </ItemTemplate>
  </asp:DataList>

Itried with:View State="Enable"AutoEventWireup="true"

autopostback="true"

当我在这里绑定datalist时它工作得很好!IsPostBack但是现在我不想在!IsPostBack中绑定我的datalist而且我也不想使用ItemCommand

但它不起作用,有人帮助我......

2 个答案:

答案 0 :(得分:2)

每当SelectedIndex更改事件触发时,Page_Load事件都会在selectedIndex更改事件之前调用。在page_load事件中,您的DataList再次绑定,因此您的SelectedIndex事件已丢失。您必须将您的DataList绑定代码放在!IsPostBack例如

protected void Page_Load(object sender, EventArgs e)
{
  if(!IsPostBack)
  {
   newtb.Columns.Add("Id", typeof(int));
   newtb.Columns.Add("FileName", typeof(string));
   newtb.Columns.Add("FilePath", typeof(string));
   newtb.Columns.Add("Index", typeof(int));

   List<ArrayList> t = new List<ArrayList>();
    if (newpath.Count > 0)
    {
        t = newpath;
        newpath = t;
        for (int i = 0; i < newpath.Count; i++)
        {
            ArrayList alst = newpath[i];
            newtb.Rows.Add(Convert.ToInt32(alst[0]), alst[1].ToString(), alst[2].ToString(), i);

        }
        ViewState["tempimage"] = newpath;
        dlstSelectedImages.DataSource = newtb;
        DataBind();
    }
  }
}

答案 1 :(得分:-1)

您必须在项目模板中添加按钮或超链接。 Look here