Add ServerClick event to ASP.NET <button> element - asp.net

I am looking to handle the click event of an HTML <button> (not an <input type="button">) on the back end. The reason I want to handle a button is because the design I have has button controls all over the place, so to modify all of the CSS would be much more work.
For my button, I have:
<button type="submit" id="submit" runat="server">Send</button>
In my code behind, I have:
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
submit.ServerClick += new EventHandler(submit_click);
}
}
protected void submit_click(object sender, EventArgs e)
{
// never being hit
}
Is this possible?

You should remove the conditional check for if (Page.IsPostBack). Your code will not currently work because the onclick handler only gets set when the page gets first rendered. Then when the user clicks on the button a postback occurs but now your page code no longer has the handler associated with the button so there is no code to execute. Just do this:
protected void Page_Load(object sender, EventArgs e)
{
submit.ServerClick += new EventHandler(submit_click);
}
protected void submit_click(object sender, EventArgs e)
{
// now it should get hit
}

Another way is to just add it to the button element:
<button type="submit" id="submit" runat="server" onserverclick="Button1_OnClick">Send</button>
void Button1_OnClick(object Source, EventArgs e)
{
// Do Something here
}
http://msdn.microsoft.com/en-us/library/a8fd2268%28v=VS.100%29.aspx

Related

DropDownButton conditional statement with attributes?

I'm having trouble to find out how to check if my DropDownList has the attribute Disabled
Here is my code (of how I declare my DropDownList):
<div class="col-7">
<asp:DropDownList ID="cmbProperty" runat="server" class="browser-default z-depth-5">
</asp:DropDownList>
</div>
On page load:
protected void Page_Load(object sender, EventArgs e)
{
cmbProperty.Attributes.Add("disabled", "disabled");
}
On button click:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Enabled == true)
{
// I always get a true statement
}
}
Someone has a clue about it?
Thank you
Since you commented saying setting cmbProperty.Enabled = false messes with your css, you should check the disabled attribute in your button click event instead of the Enabled property. This is simply:
protected void btnCheckMyProperty_Click(object sender, EventArgs e)
{
if(cmbProperty.Attributes["disabled"] == "disabled")
{
// Your code here...
}
}
Note: This will NOT error out if the disabled attribute is not set. It will return false in that case...

Parameters through onclick event in asp

I've been trying to pass parameters through the onclick event using asp as follows:
<asp:Button runat="server" ID="btnUpdateFacturaID" style="display:none;" onclick="btnUpdateFacturaID_Click" CommandArgument="test" />
And on the other side:
protected void btnUpdateFacturaID_Click(object sender, CommandEventArgs e)
{
string s = e.CommandArgument.ToString();
}
But I receive an error of no overload for 'btnUpdateFacturaID_Click' mathes delegate 'System.EventHandler'
In fact, I had initially the functions as follows:
protected void btnUpdateFacturaID_Click(object sender, EventArgs e)
{
string s = e.CommandArgument.ToString();
}
But this way I can't (or I don't actually know, which is much more probable) pass parameters through the event. What amb I missing?
Try this :
protected void btnUpdateFacturaID_Click(Object sender, EventArgs e)
{
Button btn = (Button)sender;
//do whatever with
//btn.CommandArgument.ToString()
}

Refresh ascx from button click

How to refresh another user control declared in a ascx page from button click event of that page(ascx.cs)?
You can load it dynamically.
First from page load event
protected void Page_Load(object sender, EventArgs e)
{
if(!Page.IsPostback)
{
LoadControl();
}
}
And then load it from a button again on which you want to reload it
protected void Button_Click(object sender, EventArgs e)
{
LoadControl();
}
private void LoadControl()
{
usercontrols_SimpleControl ucSimpleControl = LoadControl("~/UserControlName.ascx") as usercontrols_SimpleControl;
Placeholder1.Controls.Add(ucSimpleControl);
}

How to know the previous event handled in asp.net

on page load i have shown data from database onto a gridview(with edit and delete option). I also have a search button on the page. when i click on search the searched data should be visible in the gridview, which is working fine. But when i click on delete link after search it does not take the searched row. The page postsback after clicking on delete. I need to check the event performed prior to event performed on gridview. How to do that?? I hope that the issue is clear.. Any help is appreciated. Thanks.
Page_Load occurs before RowCommand, are you only binding grid if(!IsPostBack) ?
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
BindGrid();
}
}
protected void SearchButton_Click(object sender, EventArgs e)
{
BindGrid(searchText.Text);
}
protected void GridView_RowCommand(object sender, GridViewRowEventArgs e)
{
switch(e.CommandName)
{
case "Delete":
DeleteRow(e.Row.CommandArgument);
break;
}
}
private void BindGrid()
{
//GridView binding business logic.
}
private void BindGrid(String searchTerm)
{
//GridView binding logic with searchTerm.
}
private void DeleteRow(String commandArg)
{
//Convert command arg to ID or whatever and delete data.
}

How to add event handler to html tag in asp.net code behind?

I have this html.
<a id="a1" runat=server>
I want to add event handler to this <a> tag using code behind.
If a user press this tag, it should cause an event on the server-side.
For an easy solution there is the linkbutton on the toolbox which shows as a link but reacts as a button.
I'm not sure what you are trying to accomplish, but it seems that you want something like this:
On the .ASPX page
<asp:LinkButton ID="myLinkButton" OnClick="myLinkButton_Click" runat="server"></asp:LinkButton>
In the .ASPX.CS
protected void Page_Load(object sender, EventArgs e)
{
myLinkButton.CommandArgument = "1";
}
protected void myLinkButton_Click(object sender, EventArgs e)
{
int myLinkButtonID = Convert.ToInt32(((LinkButton)sender).CommandArgument);
}
Try this,
Html Tag:
<a id="a1" runat="server">
Code Behind:
protected void Page_Init(object sender, EventArgs e)
{
a1.ServerClick += new EventHandler(a1_ServerClick);
}
protected void a1_ServerClick(object sender, EventArgs e)
{
//Your Code here....
}
This will Create a Click Event for the <a> tag.

Resources