views:

133

answers:

4

I have the following set up, a ddl (ddlProd, radBuyer) and autocomplete text box (txtProdAC, radProd) that when populated and their respective radio buttons are selected, a grid view of the data is produced...lovely stuff.

protected void btSearch_Click(object sender, EventArgs e)   
{   
    lqPackWeights.WhereParameters.Clear();   
    ControlParameter cp = new ControlParameter();   
    cp.Type = TypeCode.String;   

    if (radBuyer.Checked)   
    {   
        cp.ControlID = "ddlProd";   
        cp.PropertyName = "SelectedValue";   
    }   

    if (radProd.Checked)   
    {   
        cp.ControlID = "tbxProdAC";   
        cp.PropertyName = "Text";   
    }
    else    
    {   
        cp.ControlID = "lbRadMiss";   
        cp.PropertyName = "Text";   
        lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";   
    }   

    cp.Name = "IDDesc";   
    lqPackWeights.WhereParameters.Add(cp);   
    GridView1.DataSourceID = "lqPackWeights";   
    GridView1.DataBind();         

}   

I stuck in the else section so that should a user hit the Search button without a radio button being checked, a label would appear and saying "Please check...etc"

This works fine but I have a slight problem. If a user produces this validation (the else), he or she would then naturally hit the appropriate radio button and then click search again. However, when this process is followed, my code seems to ignore it's job and does not pick up either the selected value for the ddl or the text from the tbxProdAC. The else label remains and the grid view remains empty.

Can someone point me in the direction with this.

+1  A: 
else    
{   
    cp.ControlID = "lbRadMiss";   
    cp.PropertyName = "Text";   
    lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";  


    ///Include this line
    return;

}
Daniel A. White
Hmm, he probably wants both the return, and the else if on radprod.checked
McKay
+1  A: 

you probably want

else if (radProd.Checked)

(instead of just the empty if)

McKay
I bloody love you!!!!
MrDean
That has driven me bonkers...I tried the else if elsewhere (excuse the pun0 but that didn't work. Thank you very much.
MrDean
+1  A: 

Do you want the following instead?

if (radBuyer.Checked)   
{   
    cp.ControlID = "ddlProd";   
    cp.PropertyName = "SelectedValue";   
}   

else if (radProd.Checked)   
{   
    cp.ControlID = "tbxProdAC";   
    cp.PropertyName = "Text";   
}   

else    
{   
    cp.ControlID = "lbRadMiss";   
    cp.PropertyName = "Text";   
    lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";   
}
Parappa
A: 

Reroute the flow of control to check specific cases and handle them appropriately.

if (!radBuyer.Checked && !radProd.Checked) 
{
    cp.ControlID = "lbRadMiss";   
    cp.PropertyName = "Text";   
    lbRadMiss.Text = "Please check appropriate radio button before you attempt a search";   
}
else
{
    if (radBuyer.Checked)   
    {   
        cp.ControlID = "ddlProd";   
        cp.PropertyName = "SelectedValue";   
    }   
    if (radProd.Checked)   
    {   
        cp.ControlID = "tbxProdAC";   
        cp.PropertyName = "Text";   
    }   

    cp.Name = "IDDesc";   
    lqPackWeights.WhereParameters.Add(cp);   
    GridView1.DataSourceID = "lqPackWeights";   
    GridView1.DataBind();
}
Donnie
This code implies (by the syntax) that both `radBuyer` and `radProd` can be `Checked`, but that obviously doesn't make much sense. His original code is preferable, aside from changing it to `else if...` as suggested in the accepted answer.
Adam Robinson
Thanks for looking into this Adam, the radio buttons share the same group name so can't both be selected (I just didn't include that syntax). I really appreciate your input, it helps me to learn!
MrDean