Development Experience

Thursday, October 30, 2014

Asp.net RadioButton with JS Confirmation

Here is the way of Asp.net radiobutton with confirmation.

1. In code behind find radio buttons and add onclick event.
2. Write a JS function that can postback with option.
3. Catch postback parameter in Page load 
(string parameter = Request["__EVENTARGUMENT"]; )
4. In RadioButtonSelectedIndexChanged event do what you need to do in confirm and not confirm.

PS: I know that is doesn't explain well the process but when you implement the code you will get it.

Code behind

 protected void Page_Load(object sender, EventArgs e)
        {
            string parameter = Request["__EVENTARGUMENT"];

            if(parameter!=null)
            if (parameter.Equals("Itrue") || parameter.Equals("Ifalse"))
            {
                confirm = parameter;

            }
            
            ListItem l1 = InterestTypeList.Items.FindByValue("stb");
            ListItem l2 = InterestTypeList.Items.FindByValue("spt");
            l1.Attributes.Add("onclick", "javascript:return validate_delete('" + l1.Value + "','" + InterestTypeList.ClientID + "')");
            l2.Attributes.Add("onclick", "javascript:return validate_delete('" + l2.Value + "','" + InterestTypeList.ClientID + "')");
            
        }



  protected void InterestCMRadioButtonListOnSelectedIndexChanged(object sender, EventArgs e)
        {
            if (confirm.Equals("Ifalse"))
            {
                // if not confirm

                return;
            }

            //    if confirm
            
        }

JS function to post back with options

  function validate_delete(InputValue, InputItem)
        {
                var item = document.getElementById(InputItem);
           
                var r = confirm("Confirm ?");
                if (r == true)
                {
                    setTimeout('__doPostBack(\'' + item.id + '\',\'' + "Itrue" + '\')', 0);
                    return true;
                }
                else {
                    setTimeout('__doPostBack(\'' + item.id + '\',\'' + "Ifalse" + '\')', 0);
                    return true;
                }


        }

No comments:

Post a Comment