ImageButton OnClick event not working in asp.net - c#

I have an Imageutton in asp.net. I want when I clicked on it, an event fire (that code of this event is written in c#).
asp code is:
<div align="right">
<table dir="rtl">
<tr>
<td>
<asp:ImageButton ID="imgScoreStar1" runat="server" ImageUrl="~/Images/ScoreStars/Star0_28px.png" OnClick="Star_Onclick" />
</td>
</tr>
</table>
</div>
and behind code is:
protected void Star_Onclick(object sender, ImageClickEventArgs e)
{
//do something
}
but ever fire click event (I tested it by breakpoint).
Another question: When I click on ImageButton, postback happens. how can I avoid this?
Thank.

Few things to check:
1. Check CausesValidation property. Test with making it false.
2. Use Page.RegisterRequiresRaiseEvent(ImageButton)

How to avoid postback.
Add ScriptManager. There must be only one ScriptManager in one page.
(If you are using Master page, add to master page and no need to add anymore in nested content page)
Add UpdatePanel and add your desired content including imagebutton to ContentTemplate of UpdatePanel.

in aspx
<asp:ScriptManager runat="server"></asp:ScriptManager>
<asp:UpdatePanel runat="server">
<ContentTemplate>
<div align="right">
<table dir="rtl">
<tr>
<td>
<asp:ImageButton ID="imgScoreStar1" runat="server" ImageUrl="~/Images/ScoreStars/Star0_28px.png" OnClick="Star_Onclick" />
</td>
</tr>
</table>
</div>
</ContentTemplate>
</asp:UpdatePanel>
in cs
protected void Star_Onclick(object sender, ImageClickEventArgs e)
{
//do something
}
i checked its working you need to check property of that button try to add another button and change ID of button that will Ignore the property that are stopping click event

You can surround your Page load sequence like this...
if(!IsPostBack){
//PageLoad code
}
Then write code under the button click event.
The button click event should work
It worked for me!

Related

asp RadioButton not selected when Checked="true"

I have two radio buttons "Yes" and "No" and I want the "No" radio button to be selected by default. However, I can't get it be be selected through the asp code or by the code behind. Am I doing this completely wrong or did I just miss something.
ASP code:
<div class="query_header" runat="server" id="ScanOnStartup">
<div class="formlabel" style="width: 300px">Perform Scan on startup</div>
<div class="formfield" style="line-height: 10px">
<asp:RadioButton ID="scanOnStartupYes" Text="Yes" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false" Checked="false"/>&nbsp
<asp:RadioButton ID="sanOnStartupNo" Text="No" GroupName="ScanOnStartupRadio" runat="server" AutoPostBack="false" Checked="true"/>
</div>
<div class="formdivider"></div>
</div>
Code behind:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
this.scanOnStartupNo.Checked = true;
}
If you need to make a radio button checked by default on page load , you can do one of the above
From the front end , you can add the property
Checked="checked"
From the code behind, you can write it as
scanOnStartupNo.Checked=true
When I checked your code , You have made a spelling mistake in declaring the ID of NO button. That was why the code behind code piece was not working
Okay aside from the typo "sanOnStartupNo", I know where the problem is coming from. There was actually another control from my code behind that renders the page and that is where the selection for the radio button is being set. I added in code for my radio button and it works now.

How to avoid post-back of Check Box inside repeater

I am newbie for asp.net,
I have a repeater which contains check box,whenever I check the check box I am firing an event on checkchanged,but the page postbacks.
I have an update panel for the entire content in my page,but still postback occurs.Is there anyway to avoid postback.
(Ps:To avoid Postback,I am meaning to avoid the flicker that occurs)
Thanks
<asp:Repeater ID="rptrDepartment" runat="server" OnItemCommand="rptrDepartment_ItemCommand"
OnItemDataBound="rptrdepartment_databound">
<ItemTemplate>
<tr>
<td>
<asp:CheckBox ID ="chkRow" runat="server" OnCheckedChanged="ChkRow_ChkChanged" AutoPostback="true" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
And in my .cs page,
protected void ChkRow_ChkChanged(object sender, EventArgs e)
{
//some method
}
Just keep your repeater inside update panel rather than entire page, if you want some other controls also need partial postback then you can go for multiple update panels.

Setting my TextBox Text value isn't doing anything

In my webform I have a few textboxes. I've got an event handler for one of my web user controls. When the event is fired, I set the textbox value but nothing happens.
It doesn't look like the page is posting back (does it have something to do with my web user control in a modal popup?) which I assume is the problem...
Am I doing something wrong?
Webform Event Handler:
protected void Page_Load(object sender, EventArgs e)
{
SearchCompanies1.CompanyFound += new WebParts_SearchCompanies.CompanyFoundEventHandler(SearchCompanies1_CompanyFound);
}
void SearchCompanies1_CompanyFound(Company company)
{
myTextBox.Text = company.Name;
popup.Hide();
}
Modal popup and panel:
<cc1:ModalPopupExtender ID="popup" runat="server" DropShadow="true"
TargetControlID="lnkSearchEditCompany" PopupControlID="pnlSearch"
BackgroundCssClass="modalBackgroundSearchCompany" CancelControlID="lnkCancel">
</cc1:ModalPopupExtender>
<asp:Panel ID="pnlSearch" runat="server" CssClass="modalPopupSearchCompany" style ="display:none">
<table width = "100%" cellpadding = "0" cellspacing = "0">
<tr>
<td align = "right">
<strong><asp:LinkButton ID="lnkCancel" runat="server" Text = "[X]"></asp:LinkButton></strong>
</td>
</tr>
<tr>
<td>
<uc2:SearchCompanies ID="SearchCompanies1" runat="server" />
</td>
</tr>
</table>
</asp:Panel>
My events are definitely being fired and handled. I have an update panel and update progress on the web user control.
Any ideas?
It turns out that after removing my UpdatePanel, it worked. I tried setting the UpdateMode to both "Always" and "Conditional" but to no avail.
This isn't a solution, but it's a workaround for anyone who's got the same problem until I figure out what I'm doing wrong. Let me know if anyone figures it out please.

how to code on on image button if image button are in listview in asp.net C#

Here is a code tag code of asp.net
and there is a Image button where i want code on click event for download item
i try to code on click event but it was not working
<asp:ListView runat="server" ID="ListView1" GroupItemCount="1">
<LayoutTemplate>
<div>
<asp:PlaceHolder runat="server" ID="groupPlaceHolder" />
</div>
</LayoutTemplate>
<GroupTemplate>
<div style="clear: both;">
<asp:PlaceHolder runat="server" ID="itemPlaceHolder" />
</div>
</GroupTemplate>
<ItemTemplate>
<div style="margin-left: 20px; border: 2px rgba(255, 102, 0, 0.36)
dotted; width: 900px; padding: 10px;">
<video src='<%# "Handler.ashx?id=" + Eval("id") %>'
width="900" height="400" controls="" preload=""></video>
</div>
<div style="width:950px; height:90px;" align="right">
<asp:ImageButton ID="ImageButton1"
ImageUrl="images/download-button.jpg" runat="server" width="230px"/>
</div>
</ItemTemplate>
<GroupSeparatorTemplate>
<div style="clear:both"></div>
</GroupSeparatorTemplate>
<EmptyDataTemplate>
</EmptyDataTemplate>
</asp:ListView>
Here is a c# Private code class on same page that is
Private void Download(string ID)
{
some code here for download
}
please let me know how can i code on image button for download a file.
Error:
Invalid postback or callback argument.
Event validation is enabled using
<pages enableEventValidation="true"/> in configuration or
<%# Page EnableEventValidation="true" %> in a page.
For security purposes, this feature verifies that arguments to
postback or callback events originate from the server
control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to
register the postback or callback data for validation.
button control is inside listview why dont you give it Command-name and then run the Item-command of the listview.
e.g
Assign the CommandName property of the Image button and then in ItemCommand of listview write
<asp:ImageButton ID="ImageButton1" ImageUrl="images/download-button.jpg"
runat="server" width="230px" CommandName ="Download"/>
if (e.CommandName = "Download")
{
WriteYour Code here..
}
for accessing that button in listview you should use item command event like this:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e)
{
if (e.CommandName == "EditRecord")
{
}
else if (e.CommandName == "DeleteRecord")
{
}
}
you can also use the onclick event of button but that's not recommended way of doing. you have to manually add the OnClick="ImageButton1_OnClick()" in button markup and then put the event manually in the code behind file
protected void ImageButton1_OnClick(object sender, EventArgs e)
{
}
Regarding your Invalid postback or callback argument error, some html is generated by some control. you have to overview your whole webform, and register any script you are using under scriptmanager. or if nothing works out you can disable this check. <%# Page EnableEventValidation="false" %>

why is my updatepanel generate hundreds of script block?

I have an asp.net web form with a repeater. In this repeater is multiple controls and I affect to these controls javascript functions with the ItemCreated event. I made this with this kind of code:
btnBareme.OnClientClick = string.Format("ChangeBareme('{0}','{1}',this); return false", item.Num_dossier, item.IdDemande);
The javascript function "changeBareme()" is defined in the header of the page. It is working fine but i need to update the repeater independently from the other elements of the page.
So I surround the repeater with a asp:UpdatePanel. Something like this:
<asp:UpdatePanel ID="UpdatePnl2" runat="server" UpdateMode="Conditional">
<ContentTemplate>
<div>
<h1>Foo</h1>
</div>
<div>
<asp:Repeater runat="server" ID="rptFoo" OnItemCreated="rptFoo_ItemCreated" OnItemCommand="rpt_ItemCommand">
<HeaderTemplate>
<table id="TabRep" class="DataRepeater">
<thead>
<tr>
<th></th>
</tr>
</thead>
<tbody>
</HeaderTemplate>
<ItemTemplate>
<tr>
SomeContent Here with controls
</tr>
</ItemTemplate>
<FooterTemplate>
</tbody>
</table>
</FooterTemplate>
</asp:Repeater>
<br/>
</div>
</ContentTemplate>
</asp:UpdatePanel>
And there.. It won't work. Controls of page outside the updatepanel lost theirs states and in debug mode, there is hundreds of "script blocks" generated..
My problem is that I don't really well understand how updatepanels are processing with javascript inside. So I need some advices from more experienced people. How can I make this updatepanel effective? What are thes hundreds of script blocks? I need to make the repeater more "Independent" from the rest of the page..
Thanks in advance.
these script block are here because you have a script block for each of you button
<input type="button" onclick="changebareme.."/>
It's not the update panel fault here.
A better approach would be to add a common css class to each of your button. Then with javascript select all of your button, and observe the click event on all of them.

Categories