Changing ImageUrl onclick ImageButton Programmatically C# - c#

I'm trying to change ImageUrl onclick Imagebutton that is present on my Repeater. I have to change it from behind code(cs file) not using jquery or js.
<asp:ImageButton ID="imgLike" ImageUrl="Content/images/thumbsup.png" onclick="imgLike_Click" runat="server" style="width:25px;height:25px" />
and here is code
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = (ImageButton)rptinserting.FindControl("imgLike");
imgLike.ImageUrl = "Content/images/thumbsup1.png";
}

You cast the sender back to an ImageButton and change the ImageUrl.
protected void imgLike_Click(object sender, ImageClickEventArgs e)
{
ImageButton imgLike = sender as ImageButton;
imgLike.ImageUrl = "/Content/images/thumbsup1.png";
}

Related

Use commandeventargs to a HTML Button control

I have a HTML control ie. Button. PFB Snippet
<button type="button" id="Button7" onserverclick="btnSave_OnClick"
runat="server" commandname="SaveNext">
Next
</button>
I am calling a same server side method from 2 different Button. I need to validate which button is clicked using CommandName in method.
Code behind:
protected void btnSave_OnClick(object sender, EventArgs e)
{
}
The problem is EventArgs e does not have CommandName . I changed method parameter to commandeventargs e which gives error as it requires EventArgs e
I am not preferring to change the HTML control to a asp:button.
Please suggest.
In the server-side event handler, you can retrieve the CommandName from the attributes of the button:
protected void btnSave_OnClick(object sender, EventArgs e)
{
HtmlButton btn = sender as HtmlButton;
string commandName = btn.Attributes["CommandName"];
...
}

get gridview label field while clicking on linkbutton

I want to retrieve the value of label field by clicking on link button. it has to be fired only in onclick event only. i have tried with this , but it is giving null value.
protected void verifycount_Click(object sender, EventArgs e)
{
GridViewRow link = ((LinkButton)sender).NamingContainer as GridViewRow;
Label qrcode = (Label)link.FindControl("lblqrcode");
string result=qrcode.text;
}
pls help do solve this
In your link button in grid view column as a command argument send the value the lable in that field.think you have
<asp:BoundField DataField="FileName" HeaderText="Attached Files" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID = "lnkDelete" Text = "Delete" CommandArgument = '<%# Eval("FileName") %>'
runat = "server" OnClick = "DeleteHWAttachment" />
</ItemTemplate>
</asp:TemplateField>
Then your code behind is like that,
protected void DeleteHWAttachment(object sender, EventArgs e)
{
string filename = (sender as LinkButton).CommandArgument;
}
protected void verifycount_Click(object sender, EventArgs e)
{
LinkButton Lnk = (LinkButton)sender;
string result=Lnk.Text;
}
protected void verifycount_Click(object sender, EventArgs e)
{
GridViewRow grdrow = (GridViewRow)((LinkButton)sender).NamingContainer;
string qrcode = grdrow.Cells[0].Text;
}
Make sure that all fields are bound fields except linkbutton

LinkButton control lifecycle, when do I declare the button?

If I have a LinkButton in an ItemTemplate of a GridView, and I want that LinkButton to fire it's events (having given up on the row event), when should I declare the button?
<ItemTemplate>
<asp:LinkButton runat="server" Text="Edit" OnInit="EditLinkButton_Init" CommandName="Edit" OnCommand="EditLinkButton_Command" ID="EditLinkButton"></asp:LinkButton>
The only event below that ever fires is EditLinkButton_Init. The text of the LinkButton is changed to ROCKON, so I think the events should fire as well? I'm still LifeCycle challenged, so if you could offer a brief explanation or link that would be great.
Here is the code-behind:
protected void EditLinkButton_Init(object sender, EventArgs e)
{
LinkButton myLinkButton = new LinkButton();
myLinkButton = (LinkButton)sender;
myLinkButton.Text = "ROCKON";
myLinkButton.Click += new EventHandler(EditLinkButton_Click);
myLinkButton.CommandName = "Edit";
myLinkButton.Command += myLinkButton_Command;
}
void myLinkButton_Command(object sender, CommandEventArgs e)
{
throw new NotImplementedException();
}
protected void EditLinkButton_Command(object sender, CommandEventArgs e)
{
}
protected void EditLinkButton_Click(object sender, EventArgs e)
{
}
I've desperately thrown everything I can think of at it....

Find Control in asp:repeater on button click event

I have a dropdown list inside a asp:repeater item template.
how can I get its value on button click event.
<asp:Repeater runat="server" ID="WorkflowListAfter" onitemcreated="WorkflowListAfterItemCreated">
<ItemTemplate>
<asp:DropDownList ID="ddlWorkflowMembers" runat="server" DataTextField="MemberName" DataValueField="MemberID">
</ItemTemplate>
</asp:Repeater>
protected DropDownList ddlWorkflowMembers = new DropDownList();
protected void WorkflowListAfterItemCreated(object sender, RepeaterItemEventArgs e)
{
ddlWorkflowMembers = (DropDownList) e.Item.FindControl("ddlWorkflowMembers");
}
protected void BtnSaveClick(object sender, EventArgs e) {
if (ddlWorkflowMembers.SelectedItem == null) return;
}
the code above is working at first time but after postback ddlWorkflowMembers is always null expersion.
Assuming that BtnSave is also inside the repeater.
You get the RepeaterItem by casting the button's NamingContainer. Then you can use FindControl to get the reference to your DropDownList:
protected void BtnSaveClick(object sender, EventArgs e) {
var btn = (Button)sender;
var item = (RepeaterItem)btn.NamingContainer;
var ddl = (DropDownList) item.FindControl("ddlWorkflowMembers");
// ...
}
If the button is outside of the repeater and you want to save all items, you need to loop through all:
protected void BtnSaveClick(object sender, EventArgs e) {
foreach(RepeaterItem item in WorkflowListAfter.Items)
{
var ddl = (DropDownList) item.FindControl("ddlWorkflowMembers");
// ...
}
}

event attched to link button inside gridview is not working?

I have a grid in asp.net, inside the asp.net i am binding data as linkbutton when clicking on link button I need to call a method in code behind. the attached event is not woking in my code. how i can solve this?
my code is similar like this,
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
link.Click += new EventHandler(this.onLinkClick);
e.Row.Cells[0].Controls.Add(link);
}
}
protected void onLinkClick(object sender, EventArgs e)
{
LinkButton btn = (LinkButton)(sender);
string value = btn.CommandArgument;
TextBox1.Text=value;
}
You have to call the function that binds the source to the GridView everytime in the Page Load
ex.
protected void Page_Load(object sender, EventArgs e)
{
PopulateGridView();
}
Because there is not logic for adding or not the link button(I guess you have to add it for each record) why don't you add it at design time?
<asp:GridView ID="GridView1" runat="server">
....
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">LinkButton</asp:LinkButton>
</ItemTemplate>
......
</asp:GridView>
Make sure that AutoEventWireup="true" on the page
Handle RowCommand event of GridView to handle "events" of buttons and you are adding LinkButton dynamically then Data Binding must be performed either at Page_Init or Page_Load.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
LinkButton link = new LinkButton();
link.Text = e.Row.Cells[0].Text;
link.CommandArgument = "Hello";
e.Row.Cells[0].Controls.Add(link);
}
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
string value = e.CommandArgument.ToString();
TextBox1.Text=value;
}
You need to hook up the event handler for your dynamic button in the GridView's RowCreated event or it won't fire. Then use "FindControl" in the RowDataBound event handler. Personally, I don't like this model at all but sometimes it's unavoidable.
You should use the GridView's <asp:ButtonField> with the grid's RowCommand event. This way, you're not the one creating the dynamic control and wiring up the events.
Here's an article on how to use it.

Categories

Resources