Question

I'm developing a scoring sheet app and I have a repeater with a radio buttons, when I check a radio, a popup shows up. it works fine. but the problem is whenever I check the next radio in the other row I get duplicate popups (one for the new radio and the other one for the previous checked radio. and it happens for all the row of the repeater (if I check 20 radios in 20 rows it will display 20 popup at the same time). I need the radios to remain checked all the time but because Autopost will fire the code behind them every time.

the code was under the repeater's PreRender event, but someone suggested that I take it out of the repeater. (by the way what is the difference between the PreRender and ItemDataBound events?) please help.

this is my code:

namespace GAPP
{
    public partial class GRADE : System.Web.UI.Page
{
    bool rdchk = false;

    protected void Page_Load(object sender, EventArgs e)
    {

        //-----------to define an event handler for the RadioButton control inside a repeater----------

        this.rptr1.ItemDataBound += new RepeaterItemEventHandler(rptr1_ItemDataBound);



        if (IsPostBack)
        {


            for (int i = 0; i < this.rptr1.Items.Count; i++)
            {
                RadioButton RDB1 = rptr1.Items[i].FindControl("L1") as RadioButton;
                RadioButton RDB2 = rptr1.Items[i].FindControl("L2") as RadioButton;
                RadioButton RDB3 = rptr1.Items[i].FindControl("L3") as RadioButton;
                RadioButton RDB4 = rptr1.Items[i].FindControl("L4") as RadioButton;
                RadioButton RDB5 = rptr1.Items[i].FindControl("R1") as RadioButton;
                RadioButton RDB6 = rptr1.Items[i].FindControl("R2") as RadioButton;
                RadioButton RDB7 = rptr1.Items[i].FindControl("R3") as RadioButton;
                RadioButton RDB8 = rptr1.Items[i].FindControl("R4") as RadioButton;

                RDB1.AutoPostBack = true;
                RDB1.CheckedChanged += new EventHandler(RDBX1_CheckedChanged);
                RDB2.AutoPostBack = true;
                RDB2.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);
                RDB3.AutoPostBack = false;
                RDB3.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);
                RDB4.AutoPostBack = true;
                RDB4.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);
                RDB5.AutoPostBack = true;
                RDB5.CheckedChanged += new EventHandler(RDBX1_CheckedChanged);
                RDB6.AutoPostBack = true;
                RDB6.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);
                RDB7.AutoPostBack = false;
                RDB7.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);
                RDB8.AutoPostBack = true;
                RDB8.CheckedChanged += new EventHandler(RDBX2_CheckedChanged);

            }

        }
    }

    //----------------------------end of define event-----------------------------------------


    private void RDBX1_CheckedChanged(Object sender, EventArgs e)
    {

        foreach (RepeaterItem item in rptr1.Items)
        {
            if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                AjaxControlToolkit.ModalPopupExtender mpex1 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender1");
                AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender2");

                Button bsr1 = (Button)item.FindControl("btn_saveRep1");
                Button bsr2 = (Button)item.FindControl("btn_saveRep2");
                Button bsr3 = (Button)item.FindControl("btn_repeat_cnxl1");
                Button bsr4 = (Button)item.FindControl("btn_repeat_cnxl2");
                RadioButton l1 = (RadioButton)item.FindControl("L1");
                RadioButton r1 = (RadioButton)item.FindControl("R1");

                if (l1.Checked && rdchk == false)
                {
                    bsr1.Visible = true;
                    bsr2.Visible = false;
                    bsr3.Visible = true;
                    bsr4.Visible = false;
                    mpex2.Show();

                }


                if (r1.Checked && rdchk == false)
                {
                    bsr1.Visible = false;
                    bsr2.Visible = true;
                    bsr3.Visible = false;
                    bsr4.Visible = true;
                    mpex2.Show();
                }



            }

        }

    }

    private void RDBX2_CheckedChanged(Object sender, EventArgs e)
    {

        foreach (RepeaterItem item in rptr1.Items)
        {
            if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                AjaxControlToolkit.ModalPopupExtender mpex1 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender1");
                AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender2");
                RadioButton l2 = (RadioButton)item.FindControl("L2");
                if (l2.Checked) mpex1.Show();



                RadioButton l4 = (RadioButton)item.FindControl("L4");
                if (l4.Checked) mpex1.Show();

                RadioButton r2 = (RadioButton)item.FindControl("R2");
                if (r2.Checked) mpex1.Show();



                RadioButton r4 = (RadioButton)item.FindControl("R4");
                if (r4.Checked) mpex1.Show();


            }

        }

    }




    protected void rptr1_ItemDataBound(object sender, RepeaterItemEventArgs e)
    {





    }

    protected void rptr1_PreRender(object sender, EventArgs e)
    {


        foreach (RepeaterItem item in rptr1.Items)
        {
            if (item.ItemType == ListItemType.AlternatingItem || item.ItemType == ListItemType.Item)
            {
                CheckBox chb = (CheckBox)item.FindControl("chkb_NO");
                Label lbl1 = (Label)item.FindControl("lbl_dg_task");
                Label lbl2 = (Label)item.FindControl("lbl_dg_seq");
                Label lbl3 = (Label)item.FindControl("lbl_dg_repeat1");
                Label lbl4 = (Label)item.FindControl("lbl_dg_final1");
                RadioButton l1 = (RadioButton)item.FindControl("L1");
                RadioButton l2 = (RadioButton)item.FindControl("L2");
                RadioButton l3 = (RadioButton)item.FindControl("L3");
                RadioButton l4 = (RadioButton)item.FindControl("L4");
                RadioButton r1 = (RadioButton)item.FindControl("R1");
                RadioButton r2 = (RadioButton)item.FindControl("R2");
                RadioButton r3 = (RadioButton)item.FindControl("R3");
                RadioButton r4 = (RadioButton)item.FindControl("R4");
                AjaxControlToolkit.ModalPopupExtender mpex1 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender1");
                AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)item.FindControl("ModalPopupExtender2");
                Button bsr1 = (Button)item.FindControl("btn_saveRep1");
                Button bsr2 = (Button)item.FindControl("btn_saveRep2");
                Button bsr3 = (Button)item.FindControl("btn_repeat_cnxl1");
                Button bsr4 = (Button)item.FindControl("btn_repeat_cnxl2");




                //-----------------------------

                if (chb.Checked == true)
                {

                    l1.Enabled = false;
                    l2.Enabled = false;
                    l3.Enabled = false;
                    l4.Enabled = false;
                    r1.Enabled = false;
                    r2.Enabled = false;
                    r3.Enabled = false;
                    r4.Enabled = false;

                    l1.Checked = false;
                    l2.Checked = false;
                    l3.Checked = false;
                    l4.Checked = false;
                    r1.Checked = false;
                    r2.Checked = false;
                    r3.Checked = false;
                    r4.Checked = false;


                    lbl1.CssClass = "grayedout";
                    lbl2.CssClass = "grayedout";


                }
                else
                {
                    l1.Enabled = true;
                    l2.Enabled = true;
                    l3.Enabled = true;
                    l4.Enabled = true;
                    r1.Enabled = true;
                    r2.Enabled = true;
                    r3.Enabled = true;
                    r4.Enabled = true;



                    lbl1.CssClass = "seq";
                    lbl2.CssClass = "task";

                }

            }

        }

    }

    protected void rptr1_OnItemCommand(object source, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "saveRep1")
        {
            DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddl_dg_repeat");
            RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("radio_dg_final");
            Label lbl3 = (Label)e.Item.FindControl("lbl_dg_repeat1");
            Label lbl4 = (Label)e.Item.FindControl("lbl_dg_final1");
            AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)e.Item.FindControl("ModalPopupExtender2");

            lbl3.Text = ddl1.SelectedItem.Text;
            lbl4.Text = rbl1.SelectedItem.Text;
            mpex2.Hide();
            rdchk = true;





        }
        //------------------------------------------------------------


        if (e.CommandName == "saveRep2")
        {
            DropDownList ddl1 = (DropDownList)e.Item.FindControl("ddl_dg_repeat");
            RadioButtonList rbl1 = (RadioButtonList)e.Item.FindControl("radio_dg_final");
            Label lbl3 = (Label)e.Item.FindControl("lbl_dg_repeat2");
            Label lbl4 = (Label)e.Item.FindControl("lbl_dg_final2");
            RadioButton l1 = (RadioButton)e.Item.FindControl("L1");
            AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)e.Item.FindControl("ModalPopupExtender2");

            lbl3.Text = ddl1.SelectedItem.Text;
            lbl4.Text = rbl1.SelectedItem.Text;
            mpex2.Hide();
            rdchk = true;


        }
        //------------------------------------------------------------

        if (e.CommandName == "cnxlRepeat1")
        {
            AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)e.Item.FindControl("ModalPopupExtender2");
            RadioButton l1 = (RadioButton)e.Item.FindControl("L1");
            l1.Checked = false;
            mpex2.Hide();

        }


        //------------------------------------------------------------

        if (e.CommandName == "cnxlRepeat2")
        {
            AjaxControlToolkit.ModalPopupExtender mpex2 = (AjaxControlToolkit.ModalPopupExtender)e.Item.FindControl("ModalPopupExtender2");
            RadioButton r1 = (RadioButton)e.Item.FindControl("R1");
            r1.Checked = false;
            mpex2.Hide();

        }

        //------------------------------------------------------------

        if (e.CommandName == "rdown")
        {
            ListBox lbx1 = (ListBox)e.Item.FindControl("ListBox1");
            ListBox lbx2 = (ListBox)e.Item.FindControl("ListBox2");




            if (lbx1.SelectedIndex != -1)
            {
                lbx2.Items.Add(lbx1.SelectedItem);
                lbx1.Items.Remove(lbx1.SelectedItem);
                lbx2.SelectedIndex = -1;
            }
        }
        //------------------------------------------------------------


        if (e.CommandName == "rup")
        {
            ListBox lbx3 = (ListBox)e.Item.FindControl("ListBox1");
            ListBox lbx4 = (ListBox)e.Item.FindControl("ListBox2");


            if (lbx4.SelectedIndex != -1)
            {
                lbx3.Items.Add(lbx4.SelectedItem);
                lbx4.Items.Remove(lbx4.SelectedItem);
                lbx3.SelectedIndex = -1;
            }
        }



        //----------------------------------------------------------

        if (e.CommandName == "ddl3Databound")
        {
            DropDownList ddlr1 = (DropDownList)e.Item.FindControl("DropDownList3");
            ListBox lbx5 = (ListBox)e.Item.FindControl("ListBox1");
            lbx5.DataBind();

        }

        //----------------------------------------------------------

        if (e.CommandName == "plus")
        {
            Panel pnl1 = (Panel)e.Item.FindControl("pnlXtra");
            TextBox txtb1 = (TextBox)e.Item.FindControl("txtb_comment");
            ImageButton imgb1 = (ImageButton)e.Item.FindControl("imgBtn_xtraPlus");
            ImageButton imgb2 = (ImageButton)e.Item.FindControl("imgBtn_xtraMin");
            Label lblx1 = (Label)e.Item.FindControl("label2");

            lblx1.Text = "after";
            imgb1.Visible = false;
            imgb2.Visible = true;
            pnl1.Visible = true;


        }

    }
}

}

Était-ce utile?

La solution 2

Solved, I have 2 labels which gets created when I check the radio, I used them in if statment. so if the 2 labels have text then dont display popup.

many thanks.

Autres conseils

what is the difference between the PreRender and ItemDataBound events?

Prerender is something related to your page(here, controls too have prerender event), whereas ItemDataBound is associated with your controls like repeater, grid etc.

Prerender is called at the end of asp.net page event lifecycle just before pages gets rendered on clients browser.

ItemDataBound Occurs after an item(foreach item) in the Repeater/grid/datalist control is data-bound but before it is rendered on the page

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top