asp.net 中的数据列表? ItemCommand 事件未触发?

发布于 2024-09-10 13:55:41 字数 2506 浏览 10 评论 0原文

在我的网络应用程序中,我有一个数据列表,我正在绑定一些图像。在 datalist itemcommand 事件中,我编写了一些未触发的代码,我的意思是 itemcommand 事件未触发。你能帮我吗? 这是我的源代码

DataList 控件:

<asp:DataList ID="DLQuickVideos" runat="server"  RepeatColumns ="2" CellPadding="0" CellSpacing="0" OnItemCommand="DLQuickVideos_ItemCommand" >                       
     <ItemTemplate>                                        
         <asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%# "../Trailorvideos/"+ Eval("SnapShot") %>' CommandArgument='<%# Eval("video")+"|"+Eval("videoid") %>' CausesValidation="false"  Width="111px" Height="83px" BorderStyle="double" BorderWidth="4px" BorderColor="#A70202" />                                                                    
     </ItemTemplate>
</asp:DataList> 

事件处理程序:

protected void DLQuickVideos_ItemCommand(object source, DataListCommandEventArgs e)
{        
    try
    {
        string eval = e.CommandArgument.ToString();
        int k = eval.IndexOf("|");
        videoname = eval.Substring(0, k);
        videoid = eval.Substring(k + 1);
        string move = Request.QueryString["movie"].ToString();

        if (Request.Browser.Browser == "IE")
        {
            dvplayer.InnerHtml = "<object id='player' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' name='player' width='500' height='350'>      <param name='movie' value='player-viral.swf' />     <param name='allowfullscreen' value='true' />       <param name='allowscriptaccess' value='always' />       <param name='flashvars' value='file=~/User/Trailorvideos/" + videoname + "&autostart=true' />   <p><a href='http://get.adobe.com/flashplayer'>Get Flash</a> to see this player.</p>         </object>";
        }
        else
        {
            dvplayer.InnerHtml = "<object type='application/x-shockwave-flash' data='player-viral.swf' width='500' height='350'> <param name='movie' value='player-viral.swf' /> <param name='allowfullscreen' value='true' /> <param name='allowscriptaccess' value='always' /> <param name='flashvars' value='file=~/User/Trailorvideos/" + videoname + "&autostart=true' /> <p><a href='http://get.adobe.com/flashplayer'>Get Flash</a> to see this player.</p> </object>";
        }
        GetQuickList(videoid);
    }
    catch (Exception ex)
    {

    }
}

上面的代码 .cs 代码

in my web application i have a datalist in that i am binding some images. in datalist itemcommand event i write code some code which is not firing, i mean the itemcomand event is not firing. can u help me.
this is my source code

DataList control:

<asp:DataList ID="DLQuickVideos" runat="server"  RepeatColumns ="2" CellPadding="0" CellSpacing="0" OnItemCommand="DLQuickVideos_ItemCommand" >                       
     <ItemTemplate>                                        
         <asp:ImageButton ID="imgbtn" runat="server" ImageUrl='<%# "../Trailorvideos/"+ Eval("SnapShot") %>' CommandArgument='<%# Eval("video")+"|"+Eval("videoid") %>' CausesValidation="false"  Width="111px" Height="83px" BorderStyle="double" BorderWidth="4px" BorderColor="#A70202" />                                                                    
     </ItemTemplate>
</asp:DataList> 

Event Handler:

protected void DLQuickVideos_ItemCommand(object source, DataListCommandEventArgs e)
{        
    try
    {
        string eval = e.CommandArgument.ToString();
        int k = eval.IndexOf("|");
        videoname = eval.Substring(0, k);
        videoid = eval.Substring(k + 1);
        string move = Request.QueryString["movie"].ToString();

        if (Request.Browser.Browser == "IE")
        {
            dvplayer.InnerHtml = "<object id='player' classid='clsid:D27CDB6E-AE6D-11cf-96B8-444553540000' name='player' width='500' height='350'>      <param name='movie' value='player-viral.swf' />     <param name='allowfullscreen' value='true' />       <param name='allowscriptaccess' value='always' />       <param name='flashvars' value='file=~/User/Trailorvideos/" + videoname + "&autostart=true' />   <p><a href='http://get.adobe.com/flashplayer'>Get Flash</a> to see this player.</p>         </object>";
        }
        else
        {
            dvplayer.InnerHtml = "<object type='application/x-shockwave-flash' data='player-viral.swf' width='500' height='350'> <param name='movie' value='player-viral.swf' /> <param name='allowfullscreen' value='true' /> <param name='allowscriptaccess' value='always' /> <param name='flashvars' value='file=~/User/Trailorvideos/" + videoname + "&autostart=true' /> <p><a href='http://get.adobe.com/flashplayer'>Get Flash</a> to see this player.</p> </object>";
        }
        GetQuickList(videoid);
    }
    catch (Exception ex)
    {

    }
}

above code .cs code

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

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

发布评论

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

评论(5

温馨耳语 2024-09-17 13:55:41

您已发布这是您的页面加载事件:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) //this IF statement is what prevents re-binding on PostBack 
    { 
        GetQuickList(videoid, moviename); 
    } 
    else 
    { 
        GetQuickList(videoid, moviename) 
    } 
}

这将不起作用。您不得在回发时进行数据绑定。否则,任何挂起的事件处理程序请求都将被取消。

您必须删除此 if 语句的 else 部分。

You have posted that this is your page load event:

protected void Page_Load(object sender, EventArgs e) 
{ 
    if (!IsPostBack) //this IF statement is what prevents re-binding on PostBack 
    { 
        GetQuickList(videoid, moviename); 
    } 
    else 
    { 
        GetQuickList(videoid, moviename) 
    } 
}

This will not work. You must not databind on post back. Otherwise any pending event handler requests are cancelled.

You must remove the else part of this if statement.

噩梦成真你也成魔 2024-09-17 13:55:41

你什么时候绑定DataList?如果 DataList 绑定在 PageLoad 上,但您没有处理 PostBack,那么当 DataList 重新绑定时,所有附加的事件处理程序都将丢失。在这种情况下,永远不会引发 ItemCommand 事件。

确保您的 Page_Load 方法的结构如下:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)  //this IF statement is what prevents re-binding on PostBack
   {
      //Bind DataList
   }
}

上面的代码将确保不会在 PostBack 上重新创建 DataList。

请查看这篇 MSDN 文章覆盖 Page.IsPostBack 属性

When are you binding the DataList? If the DataList is bound on PageLoad but you are not handling for a PostBack then all attached event handlers will be lost as the DataList is re-bound. The ItemCommand event will never be raised in this case.

Make sure your Page_Load method is structured as follows:

protected void Page_Load(object sender, EventArgs e)
{
   if (!IsPostBack)  //this IF statement is what prevents re-binding on PostBack
   {
      //Bind DataList
   }
}

The above code will ensure that the DataList is not re-created on PostBack.

Take a look at this MSDN article that covers the Page.IsPostBack property

你穿错了嫁妆 2024-09-17 13:55:41

向按钮添加命令名称。我想这就是原因。

HTH。

Add a command name to the button. I think that is the reason.

HTH.

携余温的黄昏 2024-09-17 13:55:41

我的猜测是您没有在回发时对 DataList 进行数据绑定。如果控件未进行数据绑定,则不会触发事件。您可以发布执行数据绑定的代码吗?也许在 Page_Load 中?

My guess is that you are not databinding the DataList on a postback. Events are not fired if the control isn't databound. Can you post the code that does your databinding, maybe in Page_Load?

栖竹 2024-09-17 13:55:41

这有效:

标记:

<asp:DataList ID="DLQuickVideos" runat="server"  RepeatColumns ="2" CellPadding="0" CellSpacing="0" OnItemCommand="DLQuickVideos_ItemCommand" >                       
     <ItemTemplate>                                        
         <asp:ImageButton ID="imgbtn" runat="server" ImageUrl='xx' CommandArgument='<%# "test" + "|" + Eval("Test") %>' CausesValidation="false"  Width="111px" Height="83px" BorderStyle="double" BorderWidth="4px" BorderColor="#A70202" />                                                                    
     </ItemTemplate>
</asp:DataList>

代码隐藏:

protected void Page_Load(object sender, EventArgs e)
        {


            {
                if (!IsPostBack)  //this IF statement is what prevents re-binding on PostBack
                {
                    System.Data.DataTable dt = new System.Data.DataTable();
                    dt.Columns.Add(new System.Data.DataColumn("test", typeof(int)));

                    System.Data.DataRow r = dt.NewRow();
                    r["test"] = 1;
                    dt.Rows.Add(r);

                    r = dt.NewRow();
                    r["test"] = 2;
                    dt.Rows.Add(r);

                    this.DLQuickVideos.DataSource = dt;
                    this.DLQuickVideos.DataBind();
                }
            }


        }

        protected void DLQuickVideos_ItemCommand(object source, DataListCommandEventArgs e)
        {

            try
            {

            }
            catch (Exception ex)
            {

            }
        }

但我不明白的是您在DLQuickVideos_ItemCommand中调用GetQuickList。做什么的?但是您最初在哪里填充DataList?

This works:

markup:

<asp:DataList ID="DLQuickVideos" runat="server"  RepeatColumns ="2" CellPadding="0" CellSpacing="0" OnItemCommand="DLQuickVideos_ItemCommand" >                       
     <ItemTemplate>                                        
         <asp:ImageButton ID="imgbtn" runat="server" ImageUrl='xx' CommandArgument='<%# "test" + "|" + Eval("Test") %>' CausesValidation="false"  Width="111px" Height="83px" BorderStyle="double" BorderWidth="4px" BorderColor="#A70202" />                                                                    
     </ItemTemplate>
</asp:DataList>

code behind:

protected void Page_Load(object sender, EventArgs e)
        {


            {
                if (!IsPostBack)  //this IF statement is what prevents re-binding on PostBack
                {
                    System.Data.DataTable dt = new System.Data.DataTable();
                    dt.Columns.Add(new System.Data.DataColumn("test", typeof(int)));

                    System.Data.DataRow r = dt.NewRow();
                    r["test"] = 1;
                    dt.Rows.Add(r);

                    r = dt.NewRow();
                    r["test"] = 2;
                    dt.Rows.Add(r);

                    this.DLQuickVideos.DataSource = dt;
                    this.DLQuickVideos.DataBind();
                }
            }


        }

        protected void DLQuickVideos_ItemCommand(object source, DataListCommandEventArgs e)
        {

            try
            {

            }
            catch (Exception ex)
            {

            }
        }

But what i don't understand is you are calling GetQuickList in DLQuickVideos_ItemCommand. What for? But where do you initially fill the DataList?

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