Datalist on asp.net? Does the ItemCommand event not fire?

in my web application i have a datalist because i am binding some images. in the datalist itemcommand event, I write code that does not fire, I mean that the itemcomand event does not fire. Could you 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) { } } 

code code .cs

+4
source share
5 answers

You posted that this is a 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 should not bind data to the message in response. Otherwise, any pending event handler requests will be canceled.

You must remove the else part of this if statement.

+7
source

When do you bind a DataList? If the DataList is bound to the Load page, but you are not processing PostBack, then all attached event handlers will be lost, because the DataList will be 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 ensures that the DataList will not be re-created on PostBack.

Take a look at the MSDN article, which covers the Page.IsPostBack property.

+4
source

Add the command name to the button. I think this is the reason.

NTN.

+1
source

I assume that you are not binding the DataList to the postback. Events do not fire if the control is not data bound. Can you post code that performs data binding, perhaps in Page_Load?

+1
source

It 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 I do not understand that you are calling GetQuickList in DLQuickVideos_ItemCommand . What for? But where do you initially populate the DataList?

+1
source

Source: https://habr.com/ru/post/1316263/


All Articles