Click here to Skip to main content
15,072,051 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi! i am creating a page where 20 fields are available, there are a switch to enable or disable that textbox bit on or off, and there is another page where same fields are, condition is when one field is enable from that page it is shown to other page else it didn't show to other page.


i have tried this but i want to make this not to check every time in if condition, i want to make a loop ,

What I have tried:

C#
if (Request.QueryString["uid"] != null)
{
    using (var db = new UseClientConnectEntities())
    {
        long CID = Convert.ToInt64(Request.QueryString["uid"]);

        tblClient c = db.tblClients.FirstOrDefault(v => v.ID == CID);
        var ca = db.tblCompanyAdditionals.Where(v=> v.CompID==c.CompID).ToList();

        foreach (var obj in ca)
        {
            if (obj.EnableCustomText1 == true)
            {
                dvLblCustomTxt1.Visible = true;
                LblCustomTxt1.Text = obj.LabelCustomText1;
            }
            if (obj.EnableCustomText2 == true)
            {
                dvLblCustomTxt2.Visible = true;
                LblCustomTxt2.Text = obj.LabelCustomText2;
            }
            if (obj.EnableCustomText3 == true)
            {
                dvLblCustomTxt3.Visible = true;
                LblCustomTxt3.Text = obj.LabelCustomText3;
            }
            if (obj.EnableCustomText4 == true)
            {
                dvLblCustomTxt4.Visible = true;
                LblCustomTxt4.Text = obj.LabelCustomText4;
            }
            if (obj.EnableCustomText5 == true)
            {
                dvLblCustomTxt5.Visible = true;
                LblCustomTxt5.Text = obj.LabelCustomText5;
            }
            if (obj.EnableCustomText6 == true)
            {
                dvLblCustomTxt6.Visible = true;
                LblCustomTxt6.Text = obj.LabelCustomText6;
            }
            if (obj.EnableCustomText7 == true)
            {
                dvLblCustomTxt7.Visible = true;
                LblCustomTxt7.Text = obj.LabelCustomText7;
            }
            if (obj.EnableCustomText8 == true)
            {
                dvLblCustomTxt8.Visible = true;
                LblCustomTxt8.Text = obj.LabelCustomText8;
            }
            if (obj.EnableCustomText2 == true)
            {
                dvLblCustomTxt9.Visible = true;
                LblCustomTxt9.Text = obj.LabelCustomText9;
            }
            if (obj.EnableCustomText10 == true)
            {
                dvLblCustomTxt10.Visible = true;
                LblCustomTxt10.Text = obj.LabelCustomText10;
            }
        }
    }
}
Posted
Updated 21-Feb-21 0:08am
v3

1 solution

Not knowing what's going on inside the object itself, I would probably do it this way:

C#
foreach (var obj in ca)
{
    dvLblCustomTxt1.Visible = obj.EnableCustomText1;
    LblCustomTxt1.Text = obj.LabelCustomText1;
    dvLblCustomTxt2.Visible = obj.EnableCustomText2;
    LblCustomTxt2.Text = obj.LabelCustomText2;
    dvLblCustomTxt3.Visible = obj.EnableCustomText3;
    LblCustomTxt3.Text = obj.LabelCustomText3;
    dvLblCustomTxt4.Visible = obj.EnableCustomText4;
    LblCustomTxt4.Text = obj.LabelCustomText4;
    dvLblCustomTxt5.Visible = obj.EnableCustomText5;
    LblCustomTxt5.Text = obj.LabelCustomText5;
    dvLblCustomTxt6.Visible = obj.EnableCustomText6;
    LblCustomTxt6.Text = obj.LabelCustomText6;
    dvLblCustomTxt7.Visible = obj.EnableCustomText7;
    LblCustomTxt7.Text = obj.LabelCustomText7;
    dvLblCustomTxt8.Visible = obj.EnableCustomText8;
    LblCustomTxt8.Text = obj.LabelCustomText8;
    dvLblCustomTxt9.Visible = obj.EnableCustomText2; //<< typo? I think you want 9 instead of 2
    LblCustomTxt9.Text = obj.LabelCustomText9;
    dvLblCustomTxt10.Visible = obj.EnableCustomText10;
    LblCustomTxt10.Text = obj.LabelCustomText10;
}
   
Comments
Member 14976405 22-Feb-21 1:03am
   
no! i have to check first if bit is on database then its visible but i want to check all them in loop how can i?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900