Onclick of ImageButton in asp.net is not working - c#

OnClick event of ImageButton is not working in asp.net. This question has been asked here but all the answers do not work for me. In my case the only difference is I don't have UpdatePanel on my page.
My HTML is as follows:
<ul>
<asp:DataList ID="PhotoGalleryDataList" runat="server" RepeatColumns="2">
<ItemTemplate>
<li><a href="#">
<div>
<span>
<asp:Label ID="idlabel" runat="server" Text='<%#Eval("AdPhotoID") %>'></asp:Label>
<asp:ImageButton ID="AdGallaryImageButton" runat="server" ToolTip="View Photo" CssClass="AdPhotoGallery"
AlternateText="Photo" CausesValidation="false" ImageUrl='<%#string.Format("data:image/jpg;base64,{0}",Convert.ToBase64String((byte[])Eval("NormalImage"))) %>'
OnClick="AdGallaryImageButton_Click" CommandArgument='<%#Eval("AdPhotoID")%>' />
</span>
</div>
</a></li>
</ItemTemplate>
</asp:DataList>
</ul>
In the code-behind, I'm binding like this:
PhotoGalleryDataList.DataSource = dt1;
PhotoGalleryDataList.DataBind();
In dt1 I have NormalImage, IsProfileImage and AdPhotoID, where NormalImage is a byte array and IsProfileImage is Byte and AdPhotoID is varchar(15).
In OnClick event handler of the ImageButton I have:
protected void AdGallaryImageButton_Click(object sender, ImageClickEventArgs e)
{
// breakpoint here does not get hit
}

In data bound controls like DataList or Repeater you should use OnCommand event to handle clicks:
<asp:ImageButton ID="AdGallaryImageButton" runat="server" ToolTip="View Photo" CssClass="AdPhotoGallery"
AlternateText="Photo" CausesValidation="false" ImageUrl='<%#string.Format("data:image/jpg;base64,{0}",Convert.ToBase64String((byte[])Eval("NormalImage"))) %>'
OnCommand="AdGallaryImageButton_Click" CommandArgument='<%#Eval("AdPhotoID")%>' />
And change the signature of your event handler:
protected void AdGallaryImageButton_Click(object sender, CommandEventArgs e)
{
// Here you can access AdPhotoID argument via e.CommandArgument property
}

Related

How to add ASP.NET control for <i></i> tags in Html and assign them inside .FindControl() method on .cs?

Does anyone knows How to add ASP.NET control for <i></i> tags?
i am looking for ASP.NET Control that is equivalent with <i> Html tag
in my case, I also want to implement .FindControl() method for it inside the .cs file.
Here's the part of the code..
Example.axcs
<asp:Panel id="forDiv" CssClass="input-group colorpicker-component" runat="server">
<asp:TextBox ID="txtHeree" class="form-control" runat="server" Style="width: auto"></asp:TextBox>
<asp:Panel CssClass="input-group-append" runat="server">
<asp:Label CssClass="input-group-addon input-group-text" runat="server"><i></i></asp:Label>
</asp:Panel>
</asp:Panel>
from the code above,
the FindControl method that I have implemented goes like this,
Example.ascx.cs
TextBox txtVal = e.Row.FindControl("txtHeree") as TextBox;
Panel txtVal = e.Row.FindControl("forDiv") as Panel;
and, etc..
My current problem is, I still can't find both the equivalent ASP.NET Control for <i></i> tags and also for .FindControl() method for it.
sure, I have read the reference to another QnA forum from both Stackoverflow and Microsoft Docs:
ASP.NET Control to HTML tag equivalentandhttps://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/server-controls
ANY suggestion, answer, or efficient code for the <i></i> tag control, will be very.. very helpful.
Thank You.
Sure, if you add a "id" tag, and runat=server, then that "i" thing becomes quite much a server side control.
And doing that allows use in listview, repeaters etc.
So, do this:
<i id="MyIguy" runat="server"></i>
<br />
<asp:Button ID="Button1" runat="server" Text="Button"
CssClass="btn" OnClick="Button1_Click" />
So, with code behind the button like this:
protected void Button1_Click(object sender, EventArgs e)
{
MyIguy.InnerText = "this is fun";
}
And we see this when run:
And say we use a gridview, repeater etc.?
say this markup, we just dropped in that (wiht id, and runat=server).
Quite much makes that control work, look, and feel like any other server side asp.net control.
Say, this markup and gridview (for the find control example).
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false"
DataKeyNames="ID" CssClass="table" >
<Columns>
<asp:TemplateField HeaderText="Hotel Name"> <ItemTemplate>
<asp:Label ID="txtHotel" runat="server" Text='<%# Eval("HotelName") %>' ></asp:Label>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="City"> <ItemTemplate>
<asp:Label ID="txtCity" runat="server" Text='<%# Eval("City") %>' ></asp:Label>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="Description"> <ItemTemplate>
<i id="Descript" runat="server"> <%# Eval("Description") %> </i>
</ItemTemplate> </asp:TemplateField>
<asp:TemplateField HeaderText="View"> <ItemTemplate>
<asp:Button ID="Button1" runat="server" Text="View"
CssClass="btn" OnClick="Button1_Click" />
</ItemTemplate> </asp:TemplateField>
</Columns>
</asp:GridView>
Code to load:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = General.MyRst("SELECT * FROM tblHotelsA ORDER BY HotelName");
GridView1.DataBind();
}
}
And we have this now:
And our view button click code, say this:
protected void Button1_Click(object sender, EventArgs e)
{
Button btn = sender as Button;
GridViewRow gRow = btn.NamingContainer as GridViewRow;
int? PKID = GridView1.DataKeys[gRow.RowIndex]["ID"] as int?;
Debug.Print("pk data base row id = " + PKID.ToString());
Debug.Print("Row click index = " + gRow.RowIndex);
// get hotel name
Label txtHotel = gRow.FindControl("txtHotel") as Label;
Debug.Print("Hotel name = " + txtHotel.Text);
// get our <i> guy
HtmlGenericControl myIthing = gRow.FindControl("Descript") as HtmlGenericControl;
Debug.Print("Our i guy = " + myIthing.InnerText);
}
Output :
so, most such controls - adding a "id" and runat="server", and at that point, it quite much behaves like most other asp.net controls.

How to get Footer Item value on code behind with nested Repeater?

I have a nested repeater and I use a textbox in footer template. I wanto to get textbox.text value in button click. Here is my repeater:
<asp:Repeater ID="rprSSFirst" runat="server" OnItemDataBound="rprSSFirst_ItemDataBound" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
<asp:Repeater ID="rprSSNested" runat="server" > //Nested Repeater
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="btn_Save_Click" Text="Save" />
</div>
</FooterTemplate>
</asp:Repeater>
</ItemTemplate>
<FooterTemplate>
</FooterTemplate>
</asp:Repeater>
//In Code Behind
protected void btn_Save_Click(object sender, EventArgs e)
{
TextBox txtAns = (TextBox)rprSSFirst.Controls[rprSSFirst.Controls.Count - 1].FindControl("textbox");
}
But txtAns Value is always null. How to get footer item textbox value in button click? or any other way?
Thanks for your answers.
You have to find the nested RepeaterItem first where both controls are sitting. You can get it by casting the NamingContainer:
protected void btn_Save_Click(object sender, EventArgs e)
{
Button btnSave = (Button) sender;
RepeaterItem item = (RepeaterItem) btnSave.NamingContainer;
TextBox txtAns = (TextBox) item.FindControl("textbox");
}
You can use Commandname property like this for button of nested repeater:
<asp:Repeater ID="rprSSNested" runat="server" OnItemCommand="rprSSNested_ItemCommand" >
<HeaderTemplate>
</HeaderTemplate>
<ItemTemplate>
//******Some Items******
</ItemTemplate>
<FooterTemplate>
<div style=" padding: 20px 35px;" id='ajax'>
<asp:TextBox ID="textbox" TextMode="MultiLine" Columns="50" Rows="10" runat="server" ></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" OnClick="Button2_Click" Text="Save" CommandName="cmd" CommandArgument="arg"/>
</div>
</FooterTemplate>
</asp:Repeater>
And add event in c# code like this:
protected void rprSSNested_ItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.Item.ItemType == ListItemType.Footer)
{
if (e.CommandName == "cmd")
{
string ss = ((TextBox)e.Item.FindControl("textbox")).Text;
Response.Write(ss);
}
}
}

How to generate "OnCheckedChanged" to control "CheckBox" on a "RadCombobox"?

I meet are having difficulty in implementing a simple event "OnCheckedChanged" for control "checkBox" which is in a radCombobox.
I found many examples on the net to write the event in Javascript but never in C#! Why? Is it impossible to generate this event in C#?
Here is my example :
<telerik:RadComboBox ID="RadComboBoxSelectedEntity" runat="server" AutoPostBack="false" EnableCheckAllItemsCheckBox="false" EmptyMessage="Tous" CheckedItemsTexts="DisplayAllInInput" CheckBoxes="true" width="300px"
AllowCustomText="true" DataTextField="name" DataValueField="name" HighlightTemplatedItems="true">
<ItemTemplate>
<asp:CheckBox runat="server" ID="CheckBox" Text='<%# DataBinder.Eval(Container, "Text") %>' OnCheckedChanged="checkedChangeCombobox" AutoPostBack="true" />
<asp:Label ID="lblSearchRef" runat="server" Text='<%# DataBinder.Eval(Container, "Text") %>' Visible="true" />
</ItemTemplate>
<CollapseAnimation Duration="200" Type="OutQuint" />
</telerik:RadComboBox>
And the Javascript event "OnCheckedChanged":
<script language="javascript" type="text/javascript">
function checkedChangeCombobox(sender, eventArgs) {
var item = eventArgs.get_item();
sender.set_text("You checked " + item.get_text());
}
</script>
But I need implement this in C# ! Like that:
protected void checkedChangeCombobox(object sender, EventArgs e)
{
CheckBox myCheckBoxes = sender as CheckBox;
string textChk = myCheckBoxes.Text;
}
But not working...
You need to assign the check box event handler in the ItemDataBound event for the combo box, like so:
private void RadComboBoxSelectedEntity_ItemDataBound(object sender, Telerik.Web.UI.RadComboBoxItemEventArgs e)
{
((CheckBox)e.Item.FindControl("CheckBox")).CheckedChanged += checkedChangeCombobox;
}

Call JavaScript function while the asp:LinkButton been clicked

Just wondering, could it be possible call a JavaScript under asp:LinkButton while it been clicked.
Example:
I have the following code that I would like it to be calling a JavaScript function (test()), when it been clicked. How could I do it?
<asp:ListView ID="lvTest" runat="server" DataSourceID="dsTest" DataKeyNames="TestID"
OnItemCommand="lvTest_ItemCommand">
<LayoutTemplate>
<ul style="color:#fe8113;">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:LinkButton runat="server" CausesValidation="true" CommandName="" CssClass="orange"
Text='<%# Eval("TestName")%>'/>
</li>
</ItemTemplate>
</asp:ListView>
You can also add the required attribute as OnClientClick on the design itself, instead of binding it in the code behind.
<asp:ListView ID="lvTest" runat="server" DataSourceID="dsTest" DataKeyNames="TestID"
OnItemCommand="lvTest_ItemCommand">
<LayoutTemplate>
<ul style="color:#fe8113;">
<asp:PlaceHolder ID="itemPlaceholder" runat="server" />
</ul>
</LayoutTemplate>
<ItemTemplate>
<li>
<asp:LinkButton runat="server" CausesValidation="true" CssClass="orange" OnClientClick="return test();"
Text='<%# Eval("TestName")%>'/>
</li>
</ItemTemplate>
</asp:ListView>
Now Javascript function can be added
<script language="javascript" type="text/javascript">
function test()
{
//add the required functionality
alert('Hi');
}
</script>
You need to assign id to you linkbutton for following code to work which is missing in your code.
protected void GridView1_RowDataBond(object source, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton btnAlertStatus = (LinkButton)e.Row.FindControl("btnAlertStatus");
btnAlertStatus.Attributes.Add("onclick", "alert('test'); ");
}
}
You can attach javascript to the button control in the GridView1_RowDataBond event easily as i show in above code.
<asp:LinkButton ID= "lnkButton" runat="server" CausesValidation="true" CommandName="" CssClass="orange"
Text='<%# Eval("TestName")%>'/>
<script language="javascript" type="text/javascript">
function test() {
// your code goes here ..
}
</script>
In code behind file write this
protected void Page_Load(object sender, EventArgs e)
{
lnkButton.Attributes.Add("onclick", "return test()");
}
Adding the onclientclick="JsFunctionNameHere()" to the <asp:LinkButton> worked like a charm for me.

How can i access a control within a ListView once a button has been clicked (ASP.NET c#)?

I need to access a label control in a listview when i've clicked a button (that is on the same row)...
Does anyone know how to do this please? :(
See below for more of an insight...
ASPX Page:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>'
OnClick="voteOnThis" />
</ItemTemplate>
Code Behind:
protected void voteOnThis(object sender, EventArgs e)
{
Button myButton = (Button)sender;
Voting.vote(int.Parse(myButton.CommandArgument));
// Here i would like to access the 'label' lblDone and make this Visible
}
Try like this.
Label lb = e.Item.FindControl("lblDone") as Label;
b.Visible = false;
lb.Text = "text goes here";
#Saar's code should work but you might need to change your event handler to handle the ItemCommand event on the ListView, rather the the Click event of the button:
<asp:ListView ID="ListView1" runat="server" DataSourceID="DataSource"
OnItemCommand="ListView1_ItemCommand">
<LayoutTemplate>//Etc </LayoutTemplate>
<ItemTemplate>
<asp:Label ID="lblDone" runat="server" Visible="false">Your vote has been counted</asp:Label>
<asp:Button ID="voteButton" runat="server" Text="Vote" CommandArgument='<%#Eval("id") %>' />
</ItemTemplate>
...
</asp:ListView>
Then your event handler will look something like this:
protected void ListView1_ItemCommand(object sender, ListViewCommandEventArgs e) {
// #Saar's code
}

Categories