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;
}
Related
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.
I have a repeater control to show some pictures from a specified folder. Repeater has and image and a button inside to remove that image. But the problem how to reach that image name in button click event? This is my code below:
Aspx file:
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnClick="btnDelete_Click" />
<asp:Image ID="image" runat="server" ImageUrl='<%# Container.DataItem %>' />
</ItemTemplate>
</asp:Repeater>
CS file:
private void getImages(string serialNo)
{
try
{
string[] filesindirectory = Directory.GetFiles(Server.MapPath("~/Images/" + serialNo + "/"));
List<String> images = new List<string>(filesindirectory.Count());
foreach (string item in filesindirectory)
{
images.Add(String.Format("~/Images/" + serialNo + "/{0}", System.IO.Path.GetFileName(item)));
}
rptImage.DataSource = images;
rptImage.DataBind();
}
catch (Exception)
{
header.Visible = true;
}
}
protected void btnDelete_Click(object sender, EventArgs e)
{
//string imgUrl;
//The url of image is all i need to delete it
}
Use CommandName, CommandArgument and OnCommand properties of Button.
protected void Btn_Click(object sender, CommandEventArgs e)
{
if (e.CommandName == "BtnClick"){
e.CommandArgument.ToString();
}
}
<asp:Repeater ID="rptImage" runat="server">
<ItemTemplate>
<asp:Button id="btnDelete" runat="server" Text="DELETE" OnCommand="Btn_Click" CommandName="Btn_Click" CommandArgument="<%# Container.DataItem %>" />
<asp:Image ID="image" runat="server" ImageUrl="<%# Container.DataItem %>" />
</ItemTemplate>
</asp:Repeater>
I want to do something simple. I have a text box in a repeater Item that will allow people to add a note to the item. My code is not working, it doesn't seem like anything is happening at all.
ASPX:
<asp:Repeater ID="rptList" runat="server" ViewStateMode="Enabled">
<HeaderTemplate></HeaderTemplate>
<ItemTemplate>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:TextBox ID="NoteTextBox" runat="server"></asp:TextBox>
<asp:Button ID="SubmitNote" runat="server" Text="Button" OnClick="lnkClient_Click" CommandName="AddNote" CommandArgument='<%# Eval("UID")%>'/>
<asp:Label ID="ShowNotes" runat="server" Text='<%# getNotes(Eval("UID").ToString())%>'></asp:Label>
</ContentTemplate>
</asp:UpdatePanel>
CODEBEHIND - This is what should be executed on click. I replaced my SQL code with Response.Write:
public void lnkClient_Click(object sender, EventArgs e)
{
Button btn = (Button)(sender);
string FID = btn.CommandArgument.ToString();
string note = ((TextBox)rptList.Items[0].FindControl("NoteTextBox")).Text;
Response.Write(FID + " " + note);
}
UPDATE: Changed some settings and now the only problem I am having is that the text entered client side is not passed to the command.
Try this
protected void Repeater_OnItemCommand(object source, RepeaterCommandEventArgs e)
{
if (e.CommandName.Equals("AddNote"))
{
string FID =e.CommandArgument.ToString();
TextBox txtNote=e.Item.FindControl("NoteTextBox") as TextBox;
string note=txtNote.Text;
Response.Write(FID + " " + note);
}
}
and in Mark up
<asp:Repeater ID="rptList" runat="server" OnItemCommand="Repeater_OnItemCommand" ViewStateMode="Enabled">
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;
}
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
}