Tassisto
Tassisto

Reputation: 10345

CheckChanged event called when trying to switch to another TAB?

I have some radiobuttons on some tabs, when I click on one it generates an Excel-file.

<asp:RadioButton ID="rbAantallen1" runat="server" AutoPostBack="True" 
                GroupName="Soort" oncheckedchanged="rbRapport_CheckedChanged" 
                Text="Aantallen" />

The bug happens when I want to switch to the other tab. It keeps generating Excel-files. What can I do to stop running the checkedchanged event and SWITCH IN PEACE TO ANOTHER TAB?

 protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                MyDataSource.SelectCommand = @"
                    select  melder_account, 
                            aanvraag_titel, 
                            fase_datum_opgelost_oplosser, 
                            Melding_niveau_2,
                            rapporteren, 
                            fase_datum_gestart, 
                            fase_datum_opgelost, 
                            doorlooptijd, 
                            jurentkode
                    from uvw_HD_AANVRAAG_DOORLOOPTIJD_ALGEMEEN
                    where Melding_niveau_1 = 'Brandje'";
            }
        }

    protected void MenuTabs_MenuItemClick(object sender, MenuEventArgs e)
    {
        int index = Int32.Parse(e.Item.Value);
        multiTabs.ActiveViewIndex = index;
        MyDataSource.SelectCommand = BepaalDataSource(index);
    }

    public string BepaalDataSource(int index)
    {
        string select = @"
            select  melder_account, 
                    aanvraag_titel, 
                    fase_datum_opgelost_oplosser, 
                    Melding_niveau_2,
                    rapporteren, 
                    fase_datum_gestart, 
                    fase_datum_opgelost, 
                    doorlooptijd, 
                    jurentkode
            from uvw_HD_AANVRAAG_DOORLOOPTIJD_ALGEMEEN
            where Melding_niveau_1 = '";
        if (index == 0)
        {
            cbPage.Checked = false;
            return select += "Brandje'";
        }
        else
        {
            cbPage.Checked = true;
            return select += "System - Netwerk'";
        }
    }

    public DataView GetDataFromDataSource()
    {
        MyDataSource.SelectCommand = BepaalDataSource(Convert.ToInt16(cbPage.Checked));
        return MyDataSource.Select(DataSourceSelectArguments.Empty) as DataView;
    }

    protected void rbRapport_CheckedChanged(object sender, EventArgs e)
    {
        DataTable dtOriginal = (DataTable)GetDataFromDataSource().ToTable(); //Return Table consisting data
        DataTable dtTemp = new DataTable(); //Create Temporary Table

        //Creating Header Row
        dtTemp.Columns.Add("<b>Melder</b>");
        dtTemp.Columns.Add("<b>Onderwerp</b>");
        dtTemp.Columns.Add("<b>Oplosser</b>");
        dtTemp.Columns.Add("<b>Niveau 2</b>");
        dtTemp.Columns.Add("<b>Rapporteren</b>");
        dtTemp.Columns.Add("<b>Gestart op</b>");
        dtTemp.Columns.Add("<b>Opgelost op</b>");
        dtTemp.Columns.Add("<b>Doorlooptijd</b>");
        dtTemp.Columns.Add("<b>Jurentkode</b>");
        DataRow drAddItem;
        for (int i = 0; i < dtOriginal.Rows.Count; i++)
        {
            drAddItem = dtTemp.NewRow();
            drAddItem[0] = dtOriginal.Rows[i][0].ToString();//Melder
            drAddItem[1] = dtOriginal.Rows[i][1].ToString();//Onderwerp
            drAddItem[2] = dtOriginal.Rows[i][2].ToString();//Oplosser
            drAddItem[3] = dtOriginal.Rows[i][3].ToString();//Niveau 2
            drAddItem[4] = dtOriginal.Rows[i][4].ToString();//Rapporteren
            drAddItem[5] = dtOriginal.Rows[i][5].ToString();//Gestart op
            drAddItem[6] = dtOriginal.Rows[i][6].ToString();//Opgelost op
            drAddItem[7] = dtOriginal.Rows[i][7].ToString();//Doorlooptijd
            drAddItem[8] = dtOriginal.Rows[i][8].ToString();//Jurentkode
            dtTemp.Rows.Add(drAddItem);
        }

        DataGrid dg = new DataGrid(); //Temp Grid
        dg.DataSource = dtTemp;
        dg.DataBind();

        ExportToExcel("Rapport.xls", dg);
        dg = null;
        dg.Dispose();
    }

    private void ExportToExcel(string strFileName, DataGrid dg)
    {
        Response.ClearContent();
        Response.AddHeader("content-disposition", "attachment; filename=" + strFileName);
        Response.ContentType = "application/excel";
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        dg.RenderControl(htw);
        Response.Write(sw.ToString());
        Response.End();
    }

    protected void cbRapport_CheckedChanged(object sender, EventArgs e)
    {
    }

Upvotes: 0

Views: 396

Answers (2)

taylonr
taylonr

Reputation: 10790

A couple things come to mind.

In your rbRapport_CheckChanged() function you could check to make sure the radio button is visible.

You could also check your tab control to make sure you're on the right tab.

EDIT Based on your comments, the hosting tab is invisible.

If that's the case, do something like this in your code

rbRapport_CheckChanged()
{
    if(tab1.Visible == false)
        return;

    <rest of code here>
}

Where tab1 is the tab that rbRapport is on. This will check, if the tab isn't visible, you probably don't want to create an xls, so it will just short circuit and kick out of the event. If the tab is visible, it will process the event.

Edit maybe you shouldn't be creating the xls when your radio button changes. Maybe you should have a button to click that says "Generate XLS" or something, and capture that click event. – taylonr Apr 28 at 11:42

Upvotes: 2

Bharath
Bharath

Reputation: 807

Nothing wrong with the code provided. Please ensure that, you have not bind this rbRapport_CheckedChanged function with any other control's event like TabChangedEvent or CheckedChange event of other radio button which values will be changing based on tab change etc.

If not so, there may be problem with the parent controls of the RadioButton.

Upvotes: 0

Related Questions