How to Cancel a DataView's ButtonField PostBack?
-
I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
End IfOr without the "return"
e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
HTML:
<asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:ButtonField>JavaScript
function viewRow(id, row) {
try {
if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
previousRow = row;
row.style.background="lightblue";
document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
return true; //Tried false as well
} catch (e) { alert(e.message);
}
}Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)
-
I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
End IfOr without the "return"
e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
HTML:
<asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:ButtonField>JavaScript
function viewRow(id, row) {
try {
if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
previousRow = row;
row.style.background="lightblue";
document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
return true; //Tried false as well
} catch (e) { alert(e.message);
}
}Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)
Hi Michael, You need to add the onlick attribute to the
ImageButton
(theButtonField
with theButtonType="Image"
) instead of the the row (tr) element. So there are two ways here to cancel the postback of the ButtonField: + Get reference to the ImageButton which is automatically added to the cell:if (e.Row.RowType == DataControlRowType.DataRow)
{
ImageButton btn = e.Row.Cells[cellIndex].Controls[0] as ImageButton;
if(btn != null)
btn.On_Client_Click = "clientsidefunction(); return false;";
}+ Use the TemplateField with the ImageButton in the ItemTemplate instead of the ButtonField:
asp:TemplateField
<ItemTemplate>
<asp:ImageButton On_Client_Click="clientsidefunction(); return false;" ... />
</ItemTemplate>
</asp:TemplateField> -
I have tried combinations of the following to cancel the postback of a ButtonField. I am obtaining the data to view via Ajax and do not want the postback. Any ideas? Code Behind in the dgData.RowDataBound event:
If e.Row.RowType = DataControlRowType.DataRow Then
e.Row.Attributes.Add("onclick", "javascript:return viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
End IfOr without the "return"
e.Row.Attributes.Add("onclick", "javascript:viewRow(" & CType(e.Row.DataItem, BusinessObjects.Course).CourseID.ToString & ",this);")
HTML:
<asp:ButtonField Visible="true" ImageUrl="../images/ViewSearch.gif" ButtonType="Image" Text="View Record" >
<ItemStyle HorizontalAlign="Center" Width="20px" />
</asp:ButtonField>JavaScript
function viewRow(id, row) {
try {
if (!(previousRow==null)) previousRow.backcolor= row.backcolor; //reset the previous row's backcolor
previousRow = row;
row.style.background="lightblue";
document.getElementById("<%=tblEdit.ClientID %>").style.display="none"; //Hide and display HTML
document.getElementById("<%=tblView.ClientID %>").style.display="inline"; //tables from the client
requestSimpleService = CourseList.GetCourse(id, populateCourse, onError); //Ajax call
return true; //Tried false as well
} catch (e) { alert(e.message);
}
}Note I am using the "<%=tblView.ClientID %>" due to using masterpages. Thanks, Michael I firmly believe that any man's finest hour, the greatest fulfillment of all that he holds dear, is that moment when he has worked his heart out in a good cause and lies exhausted on the field of battle - victorious. Vince Lombardi (1913-1970)
Like this: (replace the X in cells(X) with the correct column number for you case) Private Sub DataGrid1_ItemCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.DataGridItemEventArgs) Handles DataGrid1.ItemCreated If e.Item.ItemType = ListItemType.Item Or e.Item.ItemType = ListItemType.AlternatingItem Then Dim c As Button = e.Item.Cells(X).Controls(0) c.Attributes.Add("onclick", "return confirm('Do you really want to click this?')") End If End Sub cheers P