Datalist in asp.net? ItemCommand event not firing?
Asked Answered
P

5

5

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

Placida answered 20/7, 2010 at 9:36 Comment(3)
Are you binding the event handler to the control? Can you post the relevant code (both code behind and aspx/ascx)?Jesuit
source code.......... <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") %Placida
i place this datalist in a table which is in divPlacida
N
7

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.

Niple answered 29/7, 2010 at 13:47 Comment(2)
so we not bind when IsPostback is true. Other wise events or not firedPlacida
I mean when we bind data in IsPostback is true then events will not fired am i right Mr. Philip Smith.Placida
T
5

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

Tobiastobie answered 24/7, 2010 at 15:30 Comment(2)
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) } }Placida
Are you able to hit breakpoints placed in DLQuickVideos_ItemCommand?Tobiastobie
M
1

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

HTH.

Moult answered 20/7, 2010 at 10:20 Comment(2)
ya i tried that one just now it is not entering into that itemcommand event. i write response.write("hai") it is not showing that message Mr.BrainPlacida
Temporarily, try using a Button, and set try it with UseSubmitBehavior="false".Moult
I
1

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?

Incarcerate answered 20/7, 2010 at 14:35 Comment(1)
this is my binding code protected void GetQuickList(string vidid) { string squery123 = "select * from Trailor_videos where movie='"+movie+"' and Trailor_videos.videoid!=" + vidid; da = new SqlDataAdapter(squery123, con); ds = new DataSet(); da.Fill(ds, "temp"); DLQuickVideos.DataSource = ds.Tables["temp"]; DLQuickVideos.DataBind(); }Placida
A
1

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?

Alodie answered 25/7, 2010 at 4:16 Comment(5)
yes in page load i am calling the GetQuickList(videoid, moviename)Placida
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) } }Placida
Remove the GetQuickList(videoid); from your DLQuickVideos_ItemCommand !Alodie
Can you paste the html that is rendered in your browser for the DataList?Alodie
This code.....<table id="DLQuickVideos" cellspacing="0" cellpadding="0" border="0" style="border-collapse:collapse;"><tr><td><input type="image" name="DLQuickVideos$ctl00$imgbtn" id="DLQuickVideos_ctl00_imgbtn" src="../Trailorvideos/dasava04.flv.jpg" style="border-color:#A70202;border-width:4px;border-style:Double;height:83px;width:111px;" /> </td><td> <input type="image" name="DLQuickVideos$ctl01$imgbtn" id="DLQuickVideos_ctl01_imgbtn" src="../Trailorvideos/dasava06.flv.jpg" style="border-color:#A70202;border-width:4px;border-style:Double;height:83px;width:111px;"/>Placida

© 2022 - 2024 — McMap. All rights reserved.