datalist selectedindexchanged 未触发

发布于 2024-12-13 00:34:10 字数 2766 浏览 1 评论 0原文

页面加载:

 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>

尝试使用:View State =“Enable”AutoEventWireup =“true”

autopostback="true"

当我在其中绑定数据列表时,它运行良好!IsPostBack 但现在我不想在 !IsPostBack 中绑定我的数据列表,我也不想使用 ItemCommand

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

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...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

巨坚强 2024-12-20 00:34:10

每当触发 SelectedIndex Change 事件时,都会在 selectedIndex Change 事件之前调用 Page_Load 事件。在您的 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();
    }
  }
}

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 e.g.

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();
    }
  }
}
世界如花海般美丽 2024-12-20 00:34:10

您必须在项目模板中放置一个按钮或超链接。
看这里

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文