Link to home
Start Free TrialLog in
Avatar of louise_8
louise_8

asked on

checkbox control parameter not working all the time

Hi,

Im having an issue with a checkbox control parameter.
It seems to be ok when the page first runs (checked - value A)
 but once its unchecked it keeps that value..

When the page loads, the checkbox is selected
(I assign it to a page parameter, hdActiveGroups)  and the value relating to it 'A' is sent to the datasource correctly
Once I uncheck it, it correctly sends Z to the datasource.
Then when I check it again (value A) it seems to send value Z to the datasource.. ie I get results as if Z was passed (unchecked)

(even though hdActiveGroups  displays as A..)

Realistically I just need a way for the checkbox parameter to be A if checked, Z if unchecked and for it refresh the ddl when checkchanged

code:

   <asp:textbox ID="hdActiveGroups" runat="server" />
<asp:CheckBox ID="chkActiveGroups" runat="server" Checked="true" OnCheckedChanged="Check_GroupStatus" AutoPostBack="true"/>
 <asp:SqlDataSource ID="SqlDataSourceGroupDisc" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
            SelectCommand="aspGetDGroups" SelectCommandType="StoredProcedure">
            <SelectParameters>
               <asp:QueryStringParameter DefaultValue="123" Name="iClientid" QueryStringField="iClientid"  Type="Int32" />                    
                <asp:Parameter DefaultValue="0" Name="iGroupDiscountid" Type="Int32" />
                  <asp:ControlParameter ControlID="hdActiveGroups" Name="cStatus"  Type="String" />
            </SelectParameters>

codebehind

        </asp:SqlDataSource>

protected void Page_Load(object sender, EventArgs e)
    {
   
        if (chkActiveGroups.Checked)
        {
            hdActiveGroups.Text = "A";
        }
        else
        {
            hdActiveGroups.Text = "Z";
        }
etc
}

 protected void Check_GroupStatus(Object sender, EventArgs e)
    {

        if (chkActiveGroups.Checked)
        {
            hdActiveGroups.Text = "A";
        }
        else
        {
            hdActiveGroups.Text = "Z";
        }
       
        ddlGroupDiscount.DataBind();
    }
Avatar of guru_sami
guru_sami
Flag of United States of America image

Wrap code within PageLoad code inside if(!Page.IsPostBack)

protected void Page_Load(object sender, EventArgs e)
    {
     if(!Page.IsPostBack){
        if (chkActiveGroups.Checked)
        {
            hdActiveGroups.Text = "A";
        }
        else
        {
            hdActiveGroups.Text = "Z";
        }
etc
}
}
Avatar of louise_8
louise_8

ASKER

Thanks for your help
I think the issue is AppendDataBoundItems="true".. its just adding the items each time I check the checkbox.. if I change to AppendDataBoundItems="false" it works but then I lose the itel Please select value=0
any ideas?
ASKER CERTIFIED SOLUTION
Avatar of guru_sami
guru_sami
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks

My ddl binds when the page is loaded
so I've put the line ddlGroupDiscount.Items.Insert(0, new ListItem("Please Select", "0")); in the page load however it doesnt add the item

Any ideas and I assume I dont to change AppendDataBoundItems="true" beforehand?
I put the code in the if !postback with AppendDataBoundItems="true"  and it worked thanks