Use commandeventargs to a HTML Button control - c#

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"];
...
}

Related

Changing ImageUrl onclick ImageButton Programmatically 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";
}

onserverclick wont trigger the event

Nothing works, The button doesn't trigger the event
code of button:
<input type="button" id="regBtn" runat="server" onserverclick="RegClick"/>
script code:
<script runat="server">
void RegClick(object sender, EventArgs e)
{
regBtn.Value += "a";
}
</script>
If you use ASP.Net Web Form, you want to use Service Control whenever posting back to server. It will go through Life Cycle Events, and trigger the appropriate event correctly.
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click"/>
Code Behind
protected void Page_Load(object sender, EventArgs e)
{
}
protected void SubmitButton_Click(object sender, EventArgs e)
{
SubmitButton.Text += "a";
}

asp.net keeps on calling a function in page_load

I've a form (mainpage.aspx), on which there's a button called 'Cancel'. Here's the code for the button:
<asp:LinkButton ID="btnCancel" runat="server" Text="Cancel" CssClass="btn btn-default" OnClick="btnCancel_Click" />
and the called function
protected void btnCancel_Click(object sender, EventArgs e)
{
Response.Redirect("EventList.aspx");
}
So as you can see, what the button do is simple. Just go to another page. But it seems here that doesn't happen. Every time I click the Cancel button, first it will try to load the code
Response.Redirect("EventList.aspx");
but then somehow it will try to execute the codes below:
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
I know this because I put a breakpoint on Response.Redirect("EventList.aspx"); and after that I step into the code above
btw the codes above are located inside
protected void Page_Load(object sender, EventArgs e)
inside the file mainpage.aspx.cs
That's because Page_Load is called on any postback and you're actually doing a postback by clicking Cancel. The solution is to wrap your non-postback specific code in a IsPostBack check:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (hdnEventId.Value != "" && hdnEventId.Value != "0")
{
LoadEvent();
}
}
}

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.

how to Call cs function from html button

Html Button
<input id="Button1" type="button" value="button" runat="server"/>
.cs file:
public void display()
{
Response.Redirect("default.aspx");
}
How to call the display function which is in .cs file from html button click
Use a <asp:button> instead and just bind the click event in the designer.
This will generate an input in the actual html.
In your codebehind file use the following:
protected void Page_PreInit(object sender, EventArgs e)
{
Button1.ServerClick += display;
}
And change your display method signature to:
private void display(object sender, EventArgs e)
{
Response.Redirect("default.aspx");
}

Categories

Resources