0

I am beginner is ASP.NET. I've a small doubt.I've written a simple code to print the text of checked radio button in the textbox.I haven't used the autopostback property for the radio button.The code is as follows

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace web
{
    public partial class SAMPLE : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }
        protected void TextBox1_TextChanged(object sender, EventArgs e)
        {
        }
        protected void RadioButton1_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton1.Checked)
                 TextBox1.Text = RadioButton1.Text;
        }
        protected void RadioButton2_CheckedChanged(object sender, EventArgs e)
        {
            if (RadioButton2.Checked)
                 TextBox1.Text = RadioButton2.Text;
        }

        protected void Button1_Click(object sender, EventArgs e)
        {

        }        
    }
}

checking the radio button, the text in radio button is not printed in the text box, but got printed after hitting the button. My doubt is during the postback(on hitting button) only the contents in button control event handler gets executed but how come the statements in other event handlers got executed?

1
  • post your aspx code,probably you may have interchanged the events Commented Oct 26, 2015 at 11:35

1 Answer 1

1

The reason is the radio button change event is Cached Event. These events are saved in the View State to be processed when a Postback event occurs. So in this case when you click the button postback happens and the cached event of radio button changed event which was stored in View State is executed.

TextChanged event of TextBox control, SelectedIndexChanged event of a DropDownList control are also examples of cached events.

Cached events can be converted into Postback Events, by setting the AutoPostBack property of the control to true.

Sign up to request clarification or add additional context in comments.

4 Comments

I guess the OP is trying to say even though there's no AutoPostBack property attached to radio button still it's getting executed on button click. How come CheckedChanged events are getting executed on postback when there's no AutoPostBack property attached.
@SuprabhatBiswal - Hey I guess I was not so clear, have updated my answer. So when button is actully doing the postback, the cached event of radio button is getting executed. It happens for textbox, dropdownlist, radio button etc.
But If we set autopostback to any control for ex in the above case radio button and when I check the radio button, it is executing all the event handlers........then what is the use of defining separate event handlers for other controls..?
@METALHEAD - Hey no, when you set the AutoPostBack property of radio button list and chage the radio button only the selected index change event of radio button will be triggered (of-course along with other page events) but button click event will not be executed. Check Page Life Cycle.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.