I have a LinkButton that I need to perform a click on to cause a postback. The actual link target is:
javascript:__doPostBack('ctl00$c1$btnRefreshGrid','');
Clicking the link does perform the postback, as verified by a breakpoint in the code-behind. Also pasting javascript:__doPostBack('ctl00$c1$btnRefreshGrid','') in the address bar of the browser works with the same effect.
I've tried the following with no effect at all:
__doPostBack('ctl00$c1$btnRefreshGrid','');
$('#ctl00$c1$btnRefreshGrid').click();
$('#ctl00$c1$btnRefreshGrid').trigger('click');
eval($('#ctl00$c1$btnRefreshGrid').attr("href"));
I've tried using both <%= btnRefreshGrid.UniqueID %> and <%= btnRefreshGrid.ClientID %> to generate the selector.
You were close, this works in Firefox:
function clickMyButton() {
javascript:__doPostBack('<%= MYBUTTONID.UniqueID %>','')
};
the following works for the following anchor (originally asp:LinkButton in server side) inside li
<li>
<a id="ctl00_ContentPlaceHolder1_ChangeNumberItemGrd_ctl01_FindByID" href="javascript:__doPostBack('ctl00$ContentPlaceHolder1$ChangeNumberItemGrd$ctl01$FindByID','')">287573</a>
</li>
because i do not have the name i must generate it from it
$(".msglist li").on("click", function () {
var postbackArg = $(this).find("a").prop("id").replace(/_/g,"$");
__doPostBack(postbackArg, '');
});
$("#<%= btnRefreshGrid.ClientID %>").click();
Should work...
Hope it helps!!!
In firebug you can get the correct name and link action of the link button:
<a id="MainContent_ctl00_Submit_Button" href="javascript:__doPostBack('ctl00$MainContent$ctl00$Submit_Button','')"></a>
var Eventtarget = $("#btnSave").attr("name");
__doPostBack(Eventtarget, "");
ASP.NET:
<asp:LinkButton ID="btnDelete" runat="server" CssClass="btn-u btn-u-xs btn-u-red"
OnClientClick="return get_confirm(this,event);"> <i class='fa fa-trash-o'> Delete </i> </asp:LinkButton>
JavaScript:
function get_confirm(obj, e) {
e.preventDefault()
var postbackArg = obj.href.replace("javascript:__doPostBack('", "").replace("','')", "");
$.confirm({
title: 'Confirm',
content: 'Are you sure to delete this item?',
closeIcon: true,
buttons: {
confirm: {
text: 'Ok',
btnClass: 'btn-red',
action: function () {
__doPostBack(postbackArg, '');
}
},
cancel: {
text: 'Cancel',
action: function () {
}
}
}
});
}
Related
My question is how can I Activate or InActivate users by clicking the button. If I clicked InActivate, the text on the button should be changed to Activate. I searched on google but I didn't get the solution:
This is Example Code For Asp.net C# code
<asp:LinkButton ID="lbtnStatus" runat="server" CommandArgument='<%#Eval("ModuleID") %>'
CssClass="imagelink" OnClick="lbtnStatus_Click"><span class='label <%#Eval("Status").ToString()=="Y"?"label-success":"label-danger"%>'><%#Eval("Status").ToString()=="Y"?"Active":"Inactive"%></span></asp:LinkButton>
How can I change the c# code to MVC Code any Idea?
You should perform it in client side. Try something like that;
$("#lbtnStatus").click(function() {
var value = $("#lbtnStatus span").text();
if(value == "Inactive")
{
$(this).html("<span class='label-success'>Active</span>");
} else if(value == "Active")
{
$(this).html("<span class='label-danger'>Inactive</span>");
}
});
You can use the following way in client side with the help of jQuery.
<button type="button" id="lbtnStatus"><span class="label-success">Active</span></button>
<script type="text/javascript">
$(document).ready(function () {
$('#lbtnStatus').click(function () {
if ($(this).find('span').hasClass("label-success"))
$(this).find('span').removeClass("label-success").addClass("label-danger").text("InActive");
else
$(this).find('span').removeClass("label-danger").addClass("label-success").text("Active");
});
});
</script>
I have a quick question...I have a masterpage and is there anyway that I can target the contentarea without having to use hyper links? I have tried using buttons and link buttons but it never worked. The reason why is because I want to have button states and I'm not sure how to go about doing this.
I have added an image to the link
<asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Tabbet.aspx"ImageUrl="~/Icons/Icon_english_a.png">HyperLink</asp:HyperLink>
and my code behind to try and show any state
void btn()
{
HyperLink1.Attributes.Add("onmousedown", "src='Images/logo.png'");
}
when I click on the image, the image is not changing to a down state.
Thanks
yes I have solution you should try following way:
<script src="Scripts/jquery-1.7.1.js"></script>
<script type="text/javascript">
$(document).ready(function () {
$("#<%=Hyperlink1.ClientID%> img").hover(function () {
$(this).attr("src", "/Images/logo.png");
}, function () {
$(this).attr("src", "/Icons/Icon_english_a.png");
}).mousedown(function () {
$(this).attr("src", "/Images/logo.png");
}).mouseup(function () {
$(this).attr("src", "/Icons/Icon_english_a.png");
});
});
</script>
Hello, is it possible to get the current rowindex of a gridview using jQuery?
Bit of background:
I delete rows from a gridview using a server side link button in a template field like so:
<asp:LinkButton CausesValidation="false" CommandName="Delete" ID="lnkDelete"
OnClientClick="javascript: return(confirm('Delete item?'));" runat="server" Text="Delete" />
Which prompts the user to confirm or cancel the deletion. If the user clicks OK, it then calls this method on the codebehind:
protected void GridViewRowDeleting(object sender, GridViewDeleteEventArgs e)
{
this.gridview_uploads.EditIndex = -1;
if (!this.UploadsList.Count.Equals(0))
{
DocumentUpload upload = this.UploadsList[e.RowIndex];
if (upload != null)
{
this.UploadsList.RemoveAt(e.RowIndex);
this.BindInputGridview();
}
}
}
But the javascript confirm (Delete item?) looks a bit naff.
I'd much prefer to use something like JQuery's dialog, but if I do, I have no idea how to grab the rowindex using this approach (I can figure out how to call the server code).
Any ideas?
Sorry if this has already been asked - I did a trawl of SO and Googled it but couldn't find anything useful.
If the LinkButton is the only LinkButton/Anchor within the GridView, then you should be able to do something like
$('#GridView1 a').click(function(){
return confirm("Delete Item");
});
edit: change the #GridView1 to the .net ID of the control.
vb
<%=(me.GridView1.ClientID)%>
c#
<%=(this.GridView1.ClientID)%>
Reply to adrianos
If you look into the jQuery UI Dialog, this has a nice Modal Confirmation box.
In a similar way to the above code, but replacing the confirm function, you could have:
<script type="text/javascript">
$().ready(function(){
$( "#dialog" ).dialog( "destroy" );
$( "#dialog-confirm" ).dialog({
resizable: false,
height:140,
modal: true,
autoOpen: false;
buttons: {
"Delete item": function() {
$( this ).dialog( "close" );
// Put in your return true/false here to activate button
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$('#GridView1 a').click(function(){
$('#dialog-confirm').dialog("open");
return false;
});
)};
</script>
I figured out how to do this using the __doPostBack method (in Javascript)
>>> In the aspx:
Hidden field:
<asp:HiddenField ID="hidden_gridRowIndex" runat="server" />
In a script tag:
$(document).ready
(
function () {
$("#div_dialog_confirmUploadDelete").dialog({
autoOpen: false
, title: "Delete upload"
, buttons: {
"OK": function () {
__doPostBack('GridViewRowDelete', $("#<%# hidden_gridRowIndex.ClientID %>").val());
$(this).dialog('close');
}
, "Cancel": function () { $(this).dialog('close'); }
}
});
});
function deleteConfirm(index) {
$("#<%# hidden_gridRowIndex.ClientID %>").val(index)
$("#div_dialog_confirmUploadDelete").dialog('open');
}
On the gridview:
<asp:TemplateField>
<ItemTemplate>
<a href="javascript: void(0);" onclick='javascript:return deleteConfirm(<%# Container.DataItemIndex %>);'>Delete</a>
</ItemTemplate>
</asp:TemplateField>
>>> In the codebehind
On Page_Load:
if (Request["__EVENTTARGET"] != null)
{
switch (Request["__EVENTTARGET"])
{
case "GridViewRowDelete":
if (Request["__EVENTARGUMENT"] != null)
{
int index = -1;
if (int.TryParse(Request["__EVENTARGUMENT"], out index))
{
this.GridViewRowDelete(index);
}
}
break;
}
}
New method called by the page_load:
protected void GridViewRowDelete(int rowIndex)
{
this.gridview_uploads.EditIndex = -1;
if (!this.UploadsList.Count.Equals(0))
{
DocumentUpload upload = this.UploadsList[rowIndex];
if (upload != null)
{
this.UploadsList.RemoveAt(rowIndex);
this.BindInputGridview();
}
}
}
Thinking about it, I could have probably made the asp:HiddenField a regular html hidden input control as the server side never needs to see it.
It feels a bit ropey so feel free to throw stones at me / suggest improvements.
Add a custom attribute to your grid and set value on binding event
<asp:GridView ID="GridView1" runat="server">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<a href="#" test='<%# Container.DataItemIndex %>'>content</a>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
Using .net clientId get the custom attribute value.
$(document).ready(function () {
$('#<%=(this.GridView1.ClientID)%> a').click(function () {
return alert("Last Name : " + this.getAttribute("test") );
})
}
);
Asp.Net 3.5 / WebForms (no ajax)
I am trying to update a delete confirm box with jquery & UIBlock. Old code looks something like this...
<asp:LinkButton OnClientClick="return confirm('Are you sure you want to delete?')" runat="server" ID="DeleteButton" OnClick="DeleteButton_OnClick">Delete</asp:LinkButton>
What is the best practice to postpone and then continue a postback with jquery & asp.net? I haven't found a clean way/example/guidance on this. Dave Ward (encosia.com) has some examples w/ UIBlock but none of them uses UIBlock as a confirmation / modal popup.
Thanks for any help/pointers.
Answer
<a href="#" id="delete">
<span>Delete?</span>
</a>
<div id="question" style="display:none; cursor: default">
<h1>Are you sure you want to delete?</h1>
<asp:LinkButton runat="server" Text="Yes" OnClick="DeleteButton_OnClick"></asp:LinkButton>
<span>No</span>
</div>
<script type="text/javascript">
$(document).ready(function() {
$('#delete').click(function(ev) {
ev.preventDefault();
$.blockUI({ message: $('#question'), css: { width: '275px'} });
});
$('#no').click(function() {
$.unblockUI();
return false;
});
});
</script>
You can do something like this :
$('#DeleteButton').bind('click', function() {
// if confirmation button inside the form is pressed ..
$('#confirm_button').bind('click', function() {
// redirect to the page where it resolves the request
window.location = "http://www.site.com/?delete";
// or use ajax call
$.ajax({
// the request to delete
});
// this one in case you choose to make ajax request
$.unblockUI();
});
$.blockUI({
// form setted with display:none; in css to be trigged when delete button is clicked
// css here is an example
message: $('#delete_form'),
css: {
border: 'none',
padding: '15px',
width: '400px',
backgroundColor: '#000',
'-webkit-border-radius': '10px',
'-moz-border-radius': '10px',
color: '#fff'
}
});
// cancel button inside the form, when clicked, dismiss form
$('#cancel').click($.unblockUI);
// if user clicks outside the form, it dismisses the form as well
$('.blockOverlay').click($.unblockUI);
});
I am trying to use confirmation dialog from jQuery UI.
I ran into this problem: how to trigger correctly the dialog and at the same time prevent trigger OnClick event specified at button until user click on Yes or No buttons at dialog?
In the example below are two ways how to popup confirmation. Lower one
works well. It's a classic JavaScript confirm dialog. When I try to use the jQuery UI dialog, it displays a dialog but allows it to run the event assigned at OnClick (here by using Command, but I suppose there is no difference. Hope I am not wrong.). The piece is taken from the ASP.NET Repeater control btw.
<li>
<asp:LinkButton ID="lbtnRenew" runat="server" Text="Renew" CssClass="ContextMenuItem"
CommandName="Renew" CommandArgument="<%# Container.ItemIndex %>"
OnClientClick="javascript: openModalDiv('dialogRenew');" /></li>
<li>
<asp:LinkButton ID="lbtnRemove" runat="server" Text="Remove" CssClass="ContextMenuItem"
CommandName="Remove" CommandArgument="<%# Container.ItemIndex %>"
OnClientClick="return confirm('Are you sure that you want to delete package?');" /></li>
This is the JavaScript I used so far:
function openModalDiv(divname) {
$('#' + divname).dialog({
bgiframe: true,
resizable: false,
modal: true,
overlay: {
backgroundColor: '#000',
opacity: 0.5
},
buttons: {
Ok: function() {
$(this).dialog('close');return true;
},
Cancel: function() {
$(this).dialog('close');return false;
}
}
});
}
I am missing something, but don't know what. How do I solve this problem?
Thanks for any tip.
P.S. if you need add some additional information let me know.
You need to configure your modal dialog and then attach an onclick event handler in the document.ready handler. Also since you're using asp.net server controls, the id generated in the html will contain the naming container so you won't be able to select using #lbtnRenew selector mentioned above. The actual generated ID will be something like ctl00_...lbtnRenew. You can use alternate jquery selectors to get just the last part of the id or name as follows
$(function() {
// configure modal dialog
$('#dialogRenew').dialog({
bgiframe: true,
resizable: false,
modal: true,
autoOpen: false,
overlay: {
backgroundColor: '#000',
opacity: 0.5
},
buttons: {
Ok: function() {
$(this).dialog('close');return true;
},
Cancel: function() {
$(this).dialog('close');return false;
}
}
});
// attach onclick event handler to open dialog
// $= selector for elements with attribute ending in text
$("submit[name$=lbtnRenew]").click(function(event) {
event.preventDefault();
$('#dialogRenew').dialog('open');
});
});
then you can remove the onclientclick inline javascript for your linkbutton
Remove the onClientClick and use jQuery to add the event, then you can use preventDefault...
$("#lbtnRenew").click(function(e) {
e.preventDefault(); //stops OnClick event
//jscript code here
});