views:

206

answers:

6

hi,

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'&gt;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'&gt;Get Flash</a> to see this player.</p> </object>";
        }
        GetQuickList(videoid);
    }
    catch (Exception ex)
    {

    }
}

above code .cs code

A: 

Hello,

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

HTH.

Brian
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.Brain
Surya sasidhar
Temporarily, try using a Button, and set try it with UseSubmitBehavior="false".
Brian
A: 

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?

dave thieben
this is my binding codeprotected 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(); }
Surya sasidhar
+3  A: 

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

Alison
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)} }
Surya sasidhar
Are you able to hit breakpoints placed in DLQuickVideos_ItemCommand?
Alison
A: 

I think what dave said is correct.

rlee923
A: 

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?

Jeroen
yes in page load i am calling the GetQuickList(videoid, moviename)
Surya sasidhar
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) } }
Surya sasidhar
Remove the GetQuickList(videoid); from your DLQuickVideos_ItemCommand !
Jeroen
Can you paste the html that is rendered in your browser for the DataList?
Jeroen
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;"/>
Surya sasidhar
A: 

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.

Philip Smith
@Philip Smith so we not bind when IsPostback is true. Other wise events or not fired
Surya sasidhar
I mean when we bind data in IsPostback is true then events will not fired am i right Mr. Philip Smith.
Surya sasidhar
Thank you Mr. Philip Smith it is working fine.
Surya sasidhar