How to generate "OnCheckedChanged" to control "CheckBox" on a "RadCombobox"? - c#

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;
}

Related

How to click on a button that is dynamically generated using JQuery

I have got a gridview. Each row has a Alternate Names (TextBox) and a hidden button, this button has to get clicked by JQuery on the textbox "onblur" automatically.
<asp:GridView ID="GridView1" CssClass="GridView1" runat="server" AutoGenerateColumns="False" EnableViewState="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="checkboxing(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="City Name Id">
<ItemTemplate>
<asp:Label ID="NameId" runat="server" Text='<%# Eval("Name_Id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names">
<ItemTemplate>
<asp:TextBox runat="server" ClientIDMode="Static" ID="altTxtNames" Style="display: none" onblur="buttonupdate(this)"></asp:TextBox>
<asp:Button runat="server" ID="TxtButton" ClientIDMode="Static" Style="visibility: hidden; display: none" OnClick="TxtButton_Click" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Whenever "onblur" event occurs in the Alternate Names (textbox)
onblur="buttonupdate(this)"
the following JQuery is executed:
function buttonupdate(alternateTxtNames) {
$('#TxtButton').click();
}
This buttonclick prompts execution of aspx.cs method where the values of edited "Alternate Names" and "City Name Id" from the clicked button grid view row are selected using following code:
protected void TxtButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string updatedSNo = (gvr.FindControl("NameId") as Label).Text;
int SNo = Convert.ToInt32(updatedSNo);
string updatedText = (gvr.FindControl("altTxtNames") as TextBox).Text;
}
But everytime the click comes from another row only the first gridview row values are selected which I think is the causality of using ClientIDMode because of which it is not able to distinguish the button click from other rows.
How should I overcome this issue. Can someone kindly guide.
Apart from the logical error of using duplicate id's, if you attach the event handler through jQuery, you would be able to get access to the target of the event.
So in that case, rather than onblur="buttonupdate(this), you could use a jQuery event handler such as this:
Change id to class: (This is necessary to attach the handler to more than one textbox)
<asp:TextBox runat="server" ClientIDMode="Static" class="altTxtNames" Style="display: none" ></asp:TextBox>
New function:
function buttonupdate(row) {
$(row).find('#TxtButton').click();
}
jQuery event handler:
$("#GridView1 .altTxtNames").on("blur", function(event){
var clickedRow = $(event.target).closest("tr");
buttonupdate(clickedRow);
});
The above handler will take the blur event, then look for the closest parent tr, then find the button to click in that row.
I'm not familiar with asp, but the above problem should be solvable through just jQuery.
This is what has worked for me:
Following is the GridView markup:
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="checkbox1" runat="server" OnClick="checkboxing(this)" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Alternate Names">
<ItemTemplate>
<asp:Label runat="server" ID="AlternateNames" Text='<%# Eval("alternateNames") %>'></asp:Label>
<asp:TextBox runat="server" ID="altTxtNames" class="altTxtNameClass" Style="display: none"></asp:TextBox>
<asp:Button runat="server" ID="TxtButton" Style="visibility: hidden; display: none" OnClick="TxtButton_Click" />
</ItemTemplate>
</asp:TemplateField>
This is the Jquery code which changes label to textbox on checkbox click:
function checkboxing(chkbox) {
var cellz = $(chkbox).parent();
var lbl = $(cellz).siblings().find('span[id*=AlternateNames]');
var txtbox = $(cellz).siblings().find('input[id*=altTxtNames]');
if ($(chkbox).is(':checked')) {
lbl.hide();
var str = lbl.text();
txtbox.val(str);
txtbox.show();
}
else {
lbl.show();
txtbox.hide();
}
}
This is the code which gets triggered on textbox blur:
$(document).ready(function () {
$('.altTxtNameClass').on('blur', function (event) {
var clickedRow = $(event.target).closest('tr');
buttonupdate(clickedRow);
});
});
Below is the code that gets called on from above code:
function buttonupdate(row) {
$(row).find('input[id*=TxtButton]').click();
}
Below is the code that gets triggered once Button is clicked
protected void TxtButton_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
GridViewRow gvr = (GridViewRow)btn.NamingContainer;
string updatedSNo = (gvr.FindControl("NameId") as Label).Text;
int SNo = Convert.ToInt32(updatedSNo);
string updatedText = (gvr.FindControl("altTxtNames") as TextBox).Text;
}
Hope this is of some help to somebody. The syntax of the Jquery on how to implement the idea is provided by Rich so got to thank him for that.

Change backColor of Repeater Button onClick

I have Repeater buttons that i want to change their backcolor on click.
The problem is that I dont know how to identify which button is clicked and I cant reach it through the onclick function.
Just to make it clear - I want to change the button that has been clicked.
It should be like a "selected" button.
THOSE ARE THE BUTTONS
<asp:Repeater ID="rptFillButtonCategory" runat="server">
<ItemTemplate>
<asp:Button ID="FillButton" runat="server" Width="100%" OnClick="ButtonSelectionFill"
CommandArgument='<%#DataBinder.Eval(Container.DataItem, "Id") %>'
Text='<%#DataBinder.Eval(Container.DataItem, "Name") + " for next version" %>'/>
</ItemTemplate>
</asp:Repeater>
This is the on click function
public void ButtonSelectionFill(object o, EventArgs e)
{
Button btn = ((Button)o);
btn.BackColor = System.Drawing.Color.Red;
DropCategory.SelectedValue = Convert.ToInt16(((Button)o).CommandArgument.ToString());
}
Thank you for the help.
This worked:
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="ObjectDataSource1">
<ItemTemplate>
<asp:Button ID="Button1" runat="server"
Text='<%#DataBinder.Eval(Container.DataItem, "Id") %>' OnClick="Button1_Click" />
</ItemTemplate>
</asp:Repeater>
protected void Button1_Click(object sender, EventArgs e)
{
Button b = (Button)sender;
b.BackColor = System.Drawing.Color.Red;
}

Onclick of ImageButton in asp.net is not working

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
}

textbox - btn and label

I have the next code and i don't get why it's not working.
<asp:Label ID="lblFull" Visible="false" runat="server">
<asp:TextBox ID="EmailtxtboxFull" Width="250px" runat="server" Font-Size="Medium" ForeColor="Blue" BorderWidth="1"></asp:TextBox>
</asp:Label>
<asp:Label ID="lblEnd" Visible="false" runat="server"></asp:Label>
<asp:Button ID="btnFull" OnClick="btnFull_Click" runat="server" Text="Send" Font-Size="Medium" ForeColor="#0066FF" BorderStyle="Solid" BorderWidth="1" />
c # code :
protected void btnFull_Click(object sender, EventArgs e)
{
String Email = EmailtxtboxFull.Text.ToString();
lblFull.Visible = false;
lblEnd.Visible = true;
lblEnd.Text = Email;
}
When i click on the btn ... i got empty lable and not the email that i wrote on the textbox. why this it's happent? that's connect to the autopostback ? how to fix that?
You have your EmailtxtboxFull textbox inside your label, are you sure the code sample is right?
when i corrected the label the code behaved correctly 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