Do not show confirm messagebox if gridview checkbox is not checked - c#

I have gridview which has columns of data and a button below the "Apply" to perform some action.
One column in Gridview is a checkbox column. When you select checkboxes and click apply a confirm message pops up asking "Are you sure?" with option of "Yes" and "No".
But if no check box is checked i want to show a popup to user to select a checkbox before performing an action and not show the confirm message popup.
What i have now is if no checkbox is selected the confirm message pops up first and if i press "yes" then an alert message pops up to select atleast one checkbox. If i press cancel no pop up shows.
Here is my code:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ApplyAction_Button.Attributes.Add("onclick", "return confirm('Are you sure?');");
}
}
protected void ApplyAction_Button_Click(object sender, EventArgs e)
{
// Gets the selected checkboxes in the gridview
ArrayList selectedMachines = new ArrayList();
GetSelectedMachineIds(selectedMachines);
if (selectedMachines.Count == 0)
{
Page.ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<script>alert('Please select a machine for the action to be applied');</script>");
return;
}
// Action to be applied
}
I am trying to avoid the use of postback.
Another question:
Is there a server side confirm message box in asp.net?
Any help will be appreciated. Thanks

You can set your button visible to false, once check box is checked then you can show the button.
another way is to use javascript to check if checkbox is checked
code behind
ApplyAction_Button.Attributes.Add("onclick", "ShowConfirm();");
javascript
function ShowConfirm()
{
//check if checkbox is checked if is checked then display confirm message else display alert message
if( "assumed checkbox is checked"){
return confirm("message here");
}
else{
alert("message here");
}
}

try to see this thread
Checkbox in gridview
Determining a which checkbox is checked on a datagrid and updating DB
Regards

Related

Overriding the Telerik RadGrid AddNewRecordButton functionality to redirect the user to a new page rather than add a new insert record row to the grid

Previously when my RadGrid was not a batch edit grid I was able to use the grid's AddNewRecord button to redirect the user to another page with the following code:
protected void RadGrid1_ItemCommand(object sender, GridCommandEventArgs e)
{
if (e.CommandName == "InitInsert")
{
Response.Redirect(redirectUrl + "?ProductID=" + this.ProductId);
}
}
After I made my grid a batch edit grid the Add New Button doesn't go into the ItemCommand event anymore and instead tries adding an inline insert record row to the grid. Is there anyway I can still use this button and override its functionality to still redirect the user?
So I've tested this and confirmed what I suspected in the comments. When EditMode="Batch", the "Add New Record" button, along with others, no longer cause a postback. You can override this by removing the JavaScript of the OnClientClick in the RadGrid1_ItemCreated like so:
Add this to your RadGrid1 attributes:
OnItemCreated="RadGrid1_ItemCreated"
Code behind (note: there is actually a Button AND a LinkButton):
protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
{
if (e.Item.ItemType == Telerik.Web.UI.GridItemType.CommandItem) {
//This is the icon with the plus (+) sign unless you've changed the icon
Button iconButton = e.Item.FindControl("AddNewRecordButton");
if (iconButton != null) {
iconButton.OnClientClick = "";
}
//This is the words "Add New Record" or whatever you've called it
LinkButton wordButton = e.Item.FindControl("InitInsertButton");
if (wordButton != null) {
wordButton.OnClientClick = "";
}
}
}
This should allow the postback to happen and the code you posted should be able to run.

RadComboBox show message if No Item is found after searching

I have a RadGrid within which one of my columns is a GridTemplateColumn, which has a RadComboBox loading some items (the Edit mode is set to 'PopUp').
What I want is, if while searching for an item in the RadComboBox, no item is found, then give the user an option to add a new item. Currently, just for testing purposes, I want to be able to show a message if no item is found. This is what I have tried till now.
My RadComboBox within the RadGrid is defined as follows:
<EditItemTemplate>
<telerik:RadComboBox runat="server" ID="Product_PKRadComboBox"
ShowDropDownOnTextboxClick="false" ShowMoreResultsBox="true" EnableVirtualScrolling="true"
EnableLoadOnDemand="true" EnableAutomaticLoadOnDemand="true" ItemsPerRequest="10"
OnItemsRequested="Product_PKRadComboBox_ItemsRequested" AllowCustomText="true"
Filter="StartsWith" DataSourceID="SqlProducts" DataTextField="ProductCode"
DataValueField="Product_PK"></telerik:RadComboBox>
</EditItemTemplate>
So I am checking my logic in the 'OnItemsRequested' event as follows:
protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
//RadComboBox combo = (RadComboBox)sender;
if (e.EndOfItems && e.NumberOfItems==0)
{
ScriptManager.RegisterStartupScript(this, this.GetType(), "testMessage", "alert('Product Not Found. Do you want to add a Custom Product?');", true);
//Page.ClientScript.RegisterStartupScript(typeof(Page), "some_name", "if(confirm('here the message')==false)return false;");
}
}
I tried both lines of code within the IF Statement (which is checking if what the user typed in the RadComboBox exists or not, if it doesn't return any items, then show a message), but none of them seem to work. I tried the same in debug mode and set a Breakpoint on the line within the IF statement, it actually DOES execute but I cannot see the alert.
I have just done something similar to this and my solution seems to be working pretty well.
Basically in ItemsRequested, once I know that no matches are found, I add a RadComboBoxItem manually. Give it a meaningful text such as "Add a new product..." and give it a distinct style as well to differentiate it from actual results.
Something like this:
protected void Product_PKRadComboBox_ItemsRequested(object sender, RadComboBoxItemsRequestedEventArgs e)
{
if (e.EndOfItems && e.NumberOfItems==0)
{
var addItem = new RadComboBoxItem("Add a new product...", "addnewproduct");
addItem.ToolTip = "Click to create a new product...";
addItem.CssClass = "UseSpecialCSSStyling";
Product_PKRadComboBox.Items.Add(addItem);
}
}
You then need to handle the SelectedIndexChanged event when the "addnewproduct" item is selected. Make sure to set the combobox's AutoPostBack="true".
protected void Product_PKRadComboBox_SelectedIndexChanged(object sender, RadComboBoxSelectedIndexChangedEventArgs e)
{
if (!string.IsNullOrEmpty(e.Value) && e.Value.Equals("addnewproduct"))
{
// do whatever you have to do to add a new product
}
}
You can use a RadWindow to show a confirmation box that says "Are you sure you want to add a new product?" with Yes and Cancel buttons. Go a step further by displaying the search text that the user typed in in a textbox within the RadWindow. This way the user can modify the text before adding the new item.
For example the user may type "water bottle" to search for a product. No results found so the user click on the "Add a new product..." item. The confirmation box shows up, then the user can change "water bottle" to "Green Durable Water Bottle 600ml" before clicking Yes to actually add the product.

How to call event when right click and select value from gridview cell?

I want to call event when right click then select value in gridview cell.
Is it possible to achieve this?
When right click the cell it will show one popup it contains some value using jquery I achieved this
If I select some value it will be placed in that cell that time I want to call event
Any ideas? Thanks in advance
If it happens when you click on Q1, then you could add onclick attribute to the popup item when you are populating it. Then you can use that event to do the work you need.
protected override void AddAttributesToRender(System.Web.UI.HtmlTextWriter writer)
{
writer.AddAttribute("onclick", "YourFunction()")
base.AddAttributesToRender(writer);
}
private void dataGridView1_CellMouseDown(object sender, DataGridViewCellMouseEventArgs e)
{
if (e.Button == MouseButtons.Right)
{
// Add this
dataGrid.CurrentCell = dataGrid.Rows[e.RowIndex].Cells[e.ColumnIndex];
// Can leave these here - doesn't hurt
dataGrid.Rows[e.RowIndex].Selected = true;
dataGrid.Focus();
}
}
https://stackoverflow.com/a/16765616/1428854
or
Right click to select a row in a Datagridview and show a menu to delete it

How to call Confirmation box in IF condition from Code-Behind?

I'm using a LinkButton and a DropDown.
When I click on the LinkButton the DropDown appears.
After selecting a DropDown value, I want a confirmation box called from JavaScript to appear, ensuring that the value is changed.
I'm calling this script in the second if condition, but it's not working.
After the confirmation I want to change the other value and exit from the condition.
protected void lnkbtnSave_Click(object sender, EventArgs e)
{
if ((ddlHiringManager.SelectedItem != null &&
(ddlHiringManager.SelectedItem.Text != lblHiringManager.Text)) &&
(Convert.ToInt32(ddlHiringManager.SelectedValue)) != -1)
{
if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))
{
ClsClientManager objClientManager = new ClsClientManager();
if (objClientManager.UpdateHRManagerByReqID(Convert.ToInt32(hdnReqId.Value), Convert.ToInt32(ddlHiringManager.SelectedValue)) > 0)
{
lblShowHiringManager.Text = ddlHiringManager.SelectedItem.Text;
}
}
}
else
{
ClientScript.RegisterStartupScript(typeof(Page), "SymbolError", "<script type='text/javascript'>alert('Please Select Hiring Manager !');</script>");
}
}
You cannot use the result of RegisterStartupScript method.
Change ASPX page code for the LinkButton as given below
<asp:LinkButton ID="lnkbtnSave" runat="server" OnClick="lnkbtnSave_Click"
OnClientClick="javascript: return confirm('Are you sure you want to change Hiring Manager for this requirement.');">Save</asp:LinkButton>
I have added the client side click event.
On clicking the LinkButton you will get the confirmation box. The page will postback only if you click OK in the confirmation box.
Please refer this Code Snippet. On dropdown selected index change event
protected void ddlTest_SelectedIndexChanged(object sender, EventArgs e)
{
string str = "Are you sure, you want to upload leave ?";
this.ClientScript.RegisterStartupScript(typeof(Page), "Popup", "ConfirmApproval('" + str + "');", true);
}
And for Client Side declare that method.
<script type="text/javascript">
function ConfirmApproval(objMsg) {
if (confirm(objMsg)) {
$('#divUploadLeave').fadeTo('slow', .6);
return true;
} else {
$('#divUploadLeave').fadeTo('slow', 1);
return false;
}
}
Hope It helps you.
Still if you want all things on Client Side please let me know.
Please add return before Confirm this will solve your issue.
**if (ClientScript.RegisterStartupScript(typeof(Page), "Confirm", "<script type='text/javascript'>return Confirm('Are you sure you want to change Hiring Manager for this requirement.');</script>"))**

How to show checkbox at the first column of gridview on mousehover

I think every one was aware of chrome browser in this if we select History we will get all the browsing data. When we hover the mouse on a particular row a check box was visible and when we select that the button was enabled how to do this type of scenario
Set the checkbox to display:none and then try this
protected void RecordsGrid_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
e.Row.Attributes.Add("onmouseover","checkbox.style.display='block'");
e.Row.Attributes.Add("onmouseout", "checkbox.style.display='none'");
}
}
#User: Hi you can make check box visible using j query for that follow folowing link in which it highlight row, but using same funda you can make check box visible and button also.
http://www.dotnetcurry.com/ShowArticle.aspx?ID=250
http://jquerybyexample.blogspot.com/2011/06/highlight-row-on-mouseover-in-gridview.html
Try this
$(function(){
$("#gridviewname tr").live("hover",function(){
$(this).find("input[id*='chkName']").show();
},
function(){
$(this).find("input[id*='chkName']").hide();
});
$("#gridviewname tr").live("click",function(){
if($(this).find("input[id*='chkName']").attr("checked"))
{
$(this).find("input[id*='chkName']").removeAttr("checked");
$(this).find("input[id*='chkName']").hide();
}
else
{
$(this).find("input[id*='chkName']").attr("checked",true);
$(this).find("input[id*='chkName']").show();
}
});
});

Categories