Why OnClick event of a button inside usercontrol not happening http://forums.asp.net/t/1792602.aspx/1?Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Sat, 14 Apr 2012 21:55:09 -040017926024931243http://forums.asp.net/p/1792602/4931243.aspx/1?Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Why OnClick event of a button inside usercontrol not happening <p>Greetings to all the ASP.NET pros out there</p> <p>I have a problem that i would like to share with you and if possible get an advice:</p> <p>I am loading a usercontrol dynamically using datalist. inside this usercontrol i have a imgbuttton.</p> <p>in order to find the specific usercontrol who's button was clicked, iam trying to assign a customized ID to the userControl, and the button&nbsp;like this:</p> <p>&nbsp;</p> <p><strong>Setting the id for the UserControl:</strong></p> <p>&nbsp;</p> <pre class="prettyprint">protected void Page_Load(object sender, EventArgs e) { i&#43;&#43;; this.PreRender &#43;= new EventHandler(Cast_PreRender); this.ID = &quot;Result&quot; &#43; i.ToString(); }</pre> <p>&nbsp;</p> <p><strong>Setting the id for the button</strong></p> <pre class="prettyprint">void Cast_PreRender(object sender, EventArgs e) { PlayerName1.Text = Player1NameSB; PlayerName2.Text = Player2NameSB; CasterName.Text = CasterNameSB; ImageRace1.ImageUrl = Race1SB; ImageRace2.ImageUrl = Race2SB; Map.Text = MapSB; GameFrame.Text = GameFrameSB; LikeAmount.Text = LikeAmountSB; CasterLOGOIMG.ImageUrl = CasterLOGO; PlayButton.ID = i.ToString(); }</pre> <p>the problem is, after doing this, when i click the button nothing happens, i mean, when debugging, i never get inside the OnButton_Click event. if i delete the --&gt; this.ID = "Result" + i.toString();&nbsp; and the--&gt; PlayButton.ID = i.ToString();&nbsp; it's ok. here is the onClick code</p> <pre class="prettyprint">protected void PlayButton_Click1(object sender, ImageClickEventArgs e) { Image img = (Image)sender; string tt = img.ID.ToString(); Response.Redirect("default2.aspx"); // lb.Text = img.ClientID; }</pre> <p>&nbsp;</p> <p>if there anything elese you might need to evaluate my problem, ill gladly prove and additional code needed.<br> thank you very much for your help,</p> <p>elli pertzov</p> 2012-04-13T13:34:43-04:004931372http://forums.asp.net/p/1792602/4931372.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>If you move the assignment of the ID for the PlayButton from the PreRender to the Load event it will probably work.</p> 2012-04-13T14:37:09-04:004931419http://forums.asp.net/p/1792602/4931419.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>hi mm10, thanks for your response,</p> <p>&nbsp;</p> <p>this is not working</p> <p>i have it written like this</p> <pre class="prettyprint">protected void Page_Load(object sender, EventArgs e) { if (IsPostBack == false) { i&#43;&#43;; this.PreRender &#43;= new EventHandler(Cast_PreRender); PlayButton.ID = i.ToString(); this.ID = &quot;Result&quot; &#43; i.ToString(); } }</pre> <p>&nbsp;</p> <p>still nothing happens, it is not getting to the onclick event. any other ideas?</p> 2012-04-13T15:06:20-04:004931616http://forums.asp.net/p/1792602/4931616.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>Set it on every request to make it work on the first postback:</p> <p>protected void Page_Load(object sender, EventArgs e) <br> &nbsp;&nbsp;&nbsp; { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (IsPostBack == false) <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; { <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i&#43;&#43;; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.PreRender &#43;= new EventHandler(Cast_PreRender); <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; this.ID = &quot;Result&quot; &#43; i.ToString();&nbsp; <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; } <br> PlayButton.ID = i.ToString();<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; <br> &nbsp;&nbsp;&nbsp; }</p> 2012-04-13T17:19:53-04:004931694http://forums.asp.net/p/1792602/4931694.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <pre class="prettyprint">where have you declared the variable &quot;i&quot;</pre> 2012-04-13T18:39:59-04:004931750http://forums.asp.net/p/1792602/4931750.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>hi again,</p> <p>the postback condition is not the problem, i dont know what is, but for sure is not the postback, i added this condition just now.</p> <p>ofcourse i tried what you suggested, nothing new there.....:)</p> 2012-04-13T19:40:51-04:004931756http://forums.asp.net/p/1792602/4931756.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>hi nirman, thank you for your response...</p> <p>&nbsp;</p> <p><strong>Here the declaration of the &quot;i&quot;:</strong></p> <pre class="prettyprint">public partial class SearchBullet : System.Web.UI.UserControl { public static int i = 0; private string casterLOGO; public string CasterLOGO { get { return casterLOGO; } set { casterLOGO = value; } }</pre> <p><strong></strong>&nbsp;</p> <p>in addition, this is the aspx page where i load the user control, may this has to do with the problem:</p> <pre class="prettyprint">&lt;asp:DataList ID="WatchLaterDL" runat="server" DataSourceID="SDSWatchLater" RepeatColumns="1"&gt; &lt;HeaderTemplate&gt; &lt;/HeaderTemplate&gt; &lt;ItemTemplate&gt; &lt;Search:Bullet runat="server" EnableViewState="false" ID="SearchResults" Player1NameSB='&lt;%#Bind("Player_Name") %&gt;' Player2NameSB='&lt;%#Bind("Expr1") %&gt;' CasterNameSB='&lt;%#Bind("Caster_Name") %&gt;' Race1SB='&lt;%#Bind("Race_1") %&gt;' Race2SB='&lt;%#Bind("Race_2") %&gt;' MapSB='&lt;%# Bind("Map")%&gt;' GameFrameSB='&lt;%#Bind("Game_Frame") %&gt;' LikeAmountSB='&lt;%#Bind("Like_Amount") %&gt;' CastURLSB='&lt;%#Bind("Cast_URL") %&gt;' CasterLOGO='&lt;%#Bind ("Caster_LOGO") %&gt;' /&gt; &lt;/ItemTemplate&gt; &lt;SeparatorTemplate&gt; &lt;br /&gt; &lt;/SeparatorTemplate&gt; &lt;/asp:DataList&gt;&lt;/div&gt;</pre> <p><br> </p> <p>thanks again for your help</p> 2012-04-13T19:44:24-04:004932720http://forums.asp.net/p/1792602/4932720.aspx/1?Re+Why+OnClick+event+of+a+button+inside+usercontrol+not+happening+Re: Why OnClick event of a button inside usercontrol not happening <p>my solution was like this:</p> <p>instead of using an image buttom, i used Hyperlink.</p> <p>on_Load event of the usercontrol, i assigned to the src Attribute of the hyperlink all the properties of my user control and passed those using</p> <p>querystring to another page.</p> 2012-04-14T21:55:09-04:00