简体   繁体   中英

datalist selectedindexchanged not firing

Page Load:

 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"

It was working well when i was binding the datalist inside this !IsPostBack but now I don't want to bind my datalist inside the !IsPostBack and I don't want to use ItemCommand as well

But its not working, someone help me...

Whenever the SelectedIndex Change event fires, the Page_Load event is called before the selectedIndex Change event. In your page_load event, your DataList binded again and hence your SelectedIndex event was lost. You have to put your DataList binding code under !IsPostBack eg

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

You have to put in the item template a button or a hyperlink. Look here

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