Saturday, March 24, 2012

Conditional confirmation pop up?

I have a <asp:checkbox> control on my ASP.NET page as a column (template item) in a GridView. This check box is to mark certain thing as reviewed. When the user checks it, before I run my sql query to mark the sql table rows as reviewed, I would like to pop up a confirmation dialog to the user.

However, this confirmation is not always needed, based on a condition (i.e.) if the GridView row that this check box is on is currently highlighted i.e. selected, then I don't need a confirmation. But if some other GridView row was selected and the check box on some other row is checked then I would like to confirm with the user.

I checked the modalpopup in Ajax toolkit, but it seems like it pops up every time the target control is clicked. I need the ability to be able to decide when to show the popup and when not to.

Please help.

Sounds like this can be solved by a simple if statement in your JavaScript that generates the popup. I found however that the client-side onchange event for checkboxes only fires when the focus has changed, so in this case I capture the onclick and keyup event for the checkbox, for example:

myCheckbox.InputAttributes.Add("onclick","EVENT");
myCheckbox.InputAttributes.Add("onkeyup","EVENT");

Hope this helps...

-Damien


In the client side onclick event handler for the check box, would I be able to know which row on the Server side GridView was selected before checking the checkbox.?

Should this hidden field be a server side control or just a regualr HTML hidden field?

My intent is to be able to pass to the client side javascript function, the GridView's selected row index and the row index of the row on which the clicked check box is sitting.

And then inside the JavaScript function ex: SomeValidationFunction(), I would compare the values of both the indexes, if they are equal then I return true and if not then I popup the confirmation.

So, In a nut shell, the main deal is to make the server side's GridView's row index values available to the onclick function...and how do I do it ( from the onclick() function, without going to the server) is the question?.


You could do this by passing the row index to yourSomeValidationFunction as a parameter. When you add these events to the checkbox, you can do this during the data binding event, just append the parameter(s) you need when you add the function call.


When I append the parameters during the databinding event, my parameters wont be having values. Will I be able to value the parameters when the check box is clicked later on?

Could you please provide a snippet of code as to appending parametes to a function call and valuing them...if its not too much to ask.

Thank you.


In theRowDataBound event, you can use theGridViewRowEventArgs (in this example's case, the parameter is named "e". Then this code will add the javascript event to the checkbox:

myCheckbox.InputAttributes.Add("onclick","EVENT(" + e.Row.RowIndex + ")");
myCheckbox.InputAttributes.Add("onkeyup","EVENT(" + e.Row.RowIndex + ")");

Hope this helps you out...

-Damien

Hi Damien

I have added the below code to the RowDataBound event handler for my GridView. When I add the onclick() attributes to the check box, it no more causes a postback.

<script type="text/javascript" language="javascript">
function ReviewConfirmation() {
var confirmation = confirm("Do you really want to mark reviewed?");
return confirmation;
}
</script>

protected void gvAlertSummary_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
CheckBox chkRevSummary = (CheckBox)e.Row.FindControl("chkRevSummary");
chkRevSummary.Attributes.
chkRevSummary.Attributes.Add("onclick","return ReviewConfirmation()");
}
}

I have noticed that previously there used to be a _doPostBack() call in the onclick() for the checkbox, after adding the code for the attributes, I no more see the _doPostBack, which got replaced by the attributes that I have added. How should I work around this problem.

Also, how do I add the EVENT attribute to this check box.

Thanks for you help.

No comments:

Post a Comment