Onclick event on html button - javascript

I have a simple button
<input type="submit" value="Global" onclick="Global_Click" />
that when clicked should search into a db
and here is the event that it should call:
protected void Global_Click(object sender, EventArgs e)
{
FillUsers("global");
}
FillUsers is the actual method that searches id DB.
When I click the button a JavaScript error is displayed: Global_Click is not defined!
Why is that if I am not using JavaScript? And how can I call the Global_Click from the button?

You miss the runat="server" attribute.
In your code,in that way,it is called javascript handler.

Related

Why IE cannot close tab on "Cancel" button click c#

I have this cancel button:
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Cancel" />
I want click that button it will close the current window, so in codebehind:
protected void Button1_Click(object sender, EventArgs e)
{
string jScript = "<script>window.close();</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);
// Response.Write("<script>parent.close_window();</script>");
}
But I do not see that window tab close in my IE browser. Is there anything can be corrected here?
Why are you doing this server side when you can just attach to the buttons click event in js/jQuery?
This probably wont work due to a browser security setting unless the window itself was spawned via the window.open() method, so you'll want to use a workaround if this is absolutely necessary.
<script type="text/javascript">
$('#button').click(function(e){
e.preventDefault();
window.open('','_self').close();
});
</script>
<button id="button" value="close" />
function CloseWindow() {
window.open('','_self').close();
}
Code Behind
protected void Button1_Click(object sender, EventArgs e)
{
string jScript = "<script>CloseWindow();</script>";
ClientScript.RegisterClientScriptBlock(this.GetType(), "keyClientBlock", jScript);
}
I would also suggest to create a function in javascript and then just call it with your code, and if your not going to do anything in the .cs side us DGibs answer.

Unable to make value fields editable after clicking on a link button

I've created a link button and made a event handler for it. But still unable to make the values editable.
This is the link button:
<FooterTemplate>
<asp:LinkButton ID="lbEdit" runat="server" OnClick="lbEdit_Click">Edit</asp:LinkButton>
</FooterTemplate>
Here is the code behind:
protected void lbEdit_Click(object sender, EventArgs e)
{
dvEAEdit.DefaultMode = DetailsViewMode.Edit;
}
Any help is appreciated!

Why is there no __doPostBack() JavaScript added to a CommandField's Update button in GridView when rendered to HTML

I have a basic GridView with a CommandField which uses the CommandField's in-built Edit, Cancel, Update and Delete buttons, with the ButtonType property set to "Image" (ImageButtons), defined as:
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="false"
OnRowEditing="gv_RowEditing" OnRowCancelingEdit="gv_RowCancelingEdit" OnRowUpdating="gv_RowUpdating" OnRowDeleting="gv_RowDeleting">
<Columns>
<asp:CommandField
ShowEditButton="true" ShowDeleteButton="true" ButtonType="Image"
EditImageUrl="~\Images\Icons\gridView_Edit.png"
CancelImageUrl="~\Images\Icons\gridView_CancelEdit.png"
UpdateImageUrl="~\Images\Icons\gridView_Update.png"/>
</Columns>
The server-side C# event handlers for the OnRowEditing, OnRowCancelingEdit, OnRowUpdating and OnRowDeleting events are working fine and are defined as:
protected void gv_RowUpdating(object sender, GridViewUpdateEventArgs e) { }
protected void gv_RowEditing(object sender, GridViewEditEventArgs e) { }
protected void gv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { }
protected void gv_RowDeleting(object sender, GridViewDeleteEventArgs e) { }
When the page is rendered to HTML, the ImageButtons in the CommandField are rendered to the following (simplified) <input> tags:
<input type="image" name="ctl00$Body$gv$ctl02$ctl00" src="Images/update.png" alt="Update" style="border-width:0px;">
<input type="image" src="Images/delete.png" alt="Delete" onclick="javascript:__doPostBack('ctl00$Body$gv','Delete$0')" style="border-width:0px;">
<input type="image" src="Images/edit.png" alt="Edit" onclick="javascript:__doPostBack('ctl00$Body$gv','Edit$0')" style="border-width:0px;">
<input type="image" src="Images/cancel.png" alt="Cancel" onclick="javascript:__doPostBack('ctl00$Body$gv','Cancel$0')" style="border-width:0px;">
NOTE: In reality, only either Edit/Delete or Update/Cancel are rendered at any one time, depending on whether the GridView is in edit mode or not.
So, why does the rendered Update button not use ASP.NET's __doPostBack JavaScript function, when the Delete, Edit and Cancel buttons do?
How is it that the OnRowUpdating event is handled?
NOTE: When using the default ButtonType for the CommandField it does render __doPostBack, but when using ButtonType="Image" __doPostBack is not included.
Note that these 4 buttons are never displayed together, you have either "Edit" "Delete" or, after you click "Edit", you get "Update" "Cancel".
So just as you click your "Edit" button and the set of buttons changes from "Edit" "Delete" to "Update" "Cancel", your "Update" button WILL have the __doPostback attached to it.
The OnRowUpdating is then handled normally, by the doPostback just after you click the "Edit" button.
You can download one of my examples
http://www.ii.uni.wroc.pl/~wzychla/ra2829/example3a.zip
to see that. Just change the button type to Image in Default.aspx and attach your handlers to the grid.

Get button name inside javascript function

I have a html button with runat=server tag, whoose Name has been set dynamically during run time.
protected void Page_Load(object sender, EventArgs e)
{
btnShowHideCols.Name = "some text here";
}
the button HTML code is like below.
<input id="btnShowHideCols" type="button" value="+/-" runat="server" onclick="return btnShowHideCols_onclick()" />
How Do I get the button name form inside the javascript function? can I get the sender of the event there?
You may do this:
<input id="btnShowHideCols" type="button"
value="+/-" runat="server" onclick="return btnShowHideCols_onclick(this)" />
and than:
function btnShowHideCols_onclick(el) {
el.getAttribute("name");
}
While writing something like onclick="" or onchange="" or any other event you can refer to current object with this.
Also, note that name will not be the same like in your code. It will be changed to something like ctl00$content$test, just the same as ID
Im quiet confused about what your trying to do..
Is this it:
document.getElementByid("btnShowHideCols").getAttribute("name")
^Will return the name
Then use this to send it to server.. Or am i wrong here?
Hope this helps!

click() Event in javascript with a PARAM?

I'm using the event click() to call a method in Code Behind, like this:
HTML
<asp:button bordercolor="White" id="btnAddGS" onclick="AddGSBandeira" runat="server">
JAVASCRIPT
$("#ctl00_ContentPlaceHolder1_btnAddGS").click();
C#
public void AddGSBandeira(object sender, EventArgs e)
{
}
Its work normally, but I need to pass a param in the javascript call, like this:
$("#ctl00_ContentPlaceHolder1_btnAddGS").click("param");
But I do not know how this works ...can anybody help?
The best thing to do is create a hidden control and populate it's value with JavasScript on the click event. Your code behind will be able to access that value on your postback (AJAX or otherwise).
Markup
<asp:HiddenField ID="myHiddenField" runat="server" />
<asp:button bordercolor="White" id="btnAddGS"
onclick="AddGSBandeira"
onclientclick="SetHiddenValue()" runat="server">
JavaScript
function SetHiddenValue()
{
document.getElementById("<%=myHiddenField.ClientID%>").value = "[Your value here]";
}
C#
public void AddGSBandeira(object sender, EventArgs e){}
{
var jsVal = myHiddenField.Value;
}
You can do this with trigger.
http://api.jquery.com/trigger/
$("#ctl00_ContentPlaceHolder1_btnAddGS").trigger("click",["param"]);
I believe the 2nd parameter to trigger should be an array of arguments to pass to the function.

Categories