Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi folks,
i have a query regarding, how to store multiple session values in a single loop.Here is my code:
Its a function()
C#
void ClearInputs(ControlCollection cs)
    {
        foreach (Control c in cs)
        {
            if (c is TextBox)
            {
                Session["New"] = ((TextBox)c).Text;
                lbl_savesubmit1.Text += Session["New"];
            }
        }
    }


and in submit button am calling function like:

ClearInputs(Page.Controls);



any help will be appreciated.
Posted
Comments
AmitGajjar 29-Aug-12 8:38am    
Similar question is already posted..... why you are asking same question two times ?

Even if I do not understand the utility of this thing, try this:

C#
void ClearInputs(ControlCollection cs)
    {
        foreach (Control c in cs)
        {
            if (c is TextBox)
            {
                Session[c.ID] = ((TextBox)c).Text;
                lbl_savesubmit1.Text += Session[c.ID];
            }
        }
    }
 
Share this answer
 
Comments
narlakanti 29-Aug-12 8:40am    
Hi Mario, In my page there are huge number of text boxes, so i need to keep text entered in text boxes. In a single loop i need to store the data in sessions..
Mario Majčica 29-Aug-12 8:49am    
The code I proposed will do the job. If you need to restore the values, you can do the opposite process. However this is not an elegant solution for solving a problems of this type.
Cheers
_Amy 29-Aug-12 11:37am    
This answer looks better for me. 5'ed.
Hello,

I also doesn't understand the scenario you want to resolve, Hope following code snippet will help you to solve your problem.


C#
void ClearInputs(ControlCollection cs)
        {
            List<String> lstSessions = new List<string>();
            foreach (Control c in cs)
            {
                if (c is TextBox)
                {
                    lstSessions.Add(((TextBox)c).Text);
                    lbl_savesubmit1.Text += ((TextBox)c).Text;
                }
            }
            Session["TxtSession"] = lstSessions;
        }


Thanks!!!!!!!
 
Share this answer
 
Comments
narlakanti 29-Aug-12 8:51am    
Tell me how to call this function in a submit button. I wrote this:ClearInputs(Page.Controls); //want to get all the controls in the webpage.
But its not working.
Mario Majčica 29-Aug-12 8:55am    
What you are asking is to find recursively all the controls in the page. Try this
public static List<Control> GetAllControls(ControlCollection ctrls)
{
List<Control> RetCtrls = new List<Control>();

foreach (Control ctl in ctrls)
{
if (ctl is TextBox)
{
RetCtrls.Add(ctl);
}
else
{
RetCtrls.AddRange(GetAllControls(ctl.Controls));
}
}

return RetCtrls;
}
Vinod Viswanath 29-Aug-12 9:50am    
Are you using master pages? if so this code will not work.
narlakanti 30-Aug-12 0:26am    
Ya am using master page...So what is the solution for this scenario.. Any help will be appriciated.
Vinod Viswanath 30-Aug-12 0:40am    
See my below solution hope that will help you to iterate through content panels
Hi,
If you are using Master Pages you have to iterate through all the containers to reach the control. Hope this will help you.

C#
protected void Button1_Click(object sender, EventArgs e)
    {
       // if(!IsPostBack)
        ClearInputs(Page.Controls);
    }

    void ClearInputs(ControlCollection cs)
    {
       
        foreach (Control container in cs)
        {
            foreach (Control childContainer in container.Controls)
            {
                foreach (Control innerChild in childContainer.Controls)
                {
                    foreach (Control control in innerChild.Controls)
                    {
                        if (control is TextBox)
                        {
                            ((TextBox)control).Text = string.Empty;
                            Session[childContainer.ID] = ((TextBox)control).Text;
                            lbl_savesubmit1.Text += Session[childContainer.ID];
                        }
                    }
                }
            }
        }
    }


Thanks!!!
 
Share this answer
 
v2
Comments
narlakanti 30-Aug-12 0:49am    
Hi, control is not going into the loop.
Vinod Viswanath 30-Aug-12 1:24am    
Hi, Can you paste your code behind here?
narlakanti 30-Aug-12 1:53am    
This is my code behind. My scenario is I have a tab container with 3 Levels like Level 1, Level 2, Level 3 and a button in each button. So after entering the level 1 data i will click on button, then the tab will move to the level 2 and the data entered in level 1 should be kept in sessions.Similarly for Level 2 and level 3. So finally if i click on submit button in level 3 the data in Level 1,Level 2 and Level 3 should be submitted to database.


protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
tab_level1.ActiveTabIndex = 0;
txt_dob.Attributes.Add("readonly","");
txt_fd_dob1.Attributes.Add("readonly","");

}
if (Session["New"] != null)
{
lbl_savesubmit1.Text += Session["New"].ToString();
}
else
{
//Response.Redirect("")
}
}
protected void btn_save_continue_Click(object sender, EventArgs e)
{
pnl_level2.Enabled = true;
tab_level1.ActiveTabIndex = 1;

ListControlCollections();
}

private void ListControlCollections()
{
ArrayList controlList = new ArrayList();
AddControls(Form.Controls, controlList);
foreach (string str in controlList)
{
Response.Write(str + "<br/>");
}
Response.Write("Total Controls:" + controlList.Count);
}
private void AddControls(ControlCollection page, ArrayList controlList)
{
foreach (Control c in page)
{
if (c is TextBox)
{
Session["New"] += ((TextBox)c).Text;
lbl_savesubmit1.Text += Session["New"].ToString();
controlList.Add(c.ID);
}
if (c.HasControls())
{
AddControls(c.Controls, controlList);
}
}
}
protected void btn_save_continue1_Click(object sender, EventArgs e)
{
pnl_level3.Enabled = true;
tab_level1.ActiveTabIndex = 2;
}
Vinod Viswanath 30-Aug-12 2:27am    
Sorry i was asking you the html code behind, not the code. i tried your code with my webpage it works fine for me.
narlakanti 30-Aug-12 2:44am    
<asp:TabContainer ID="tab_level1" runat="server" ActiveTabIndex="0"
Width="100%" BorderStyle="Solid" >
<asp:TabPanel ID="pnl_level1" runat="server" HeaderText="Level-1">
<HeaderTemplate>Level 1</HeaderTemplate>
<contenttemplate>
<table border="1.5" width="100%"><tr><td bgcolor="#66FFFF" colspan="4">ENROLLMENT FORM</td></tr><tr><td align="right">Name</td><td><asp:TextBox ID="txt_name" runat="server" Height="16px" Width="414px"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
ControlToValidate="txt_name" ErrorMessage="*" ForeColor="#CC3300"
SetFocusOnError="True"><asp:RegularExpressionValidator ID="RegularExpressionValidator1" runat="server"
ControlToValidate="txt_name" ErrorMessage="Please Enter characters"
ForeColor="#CC3300" SetFocusOnError="True"
ValidationExpression="^[a-zA-Z''-'\s]{1,30}$"></td><td style="text-align: right">Gender </td><td><asp:RadioButtonList ID="opt_gender" runat="server"
RepeatDirection="Horizontal"><asp:ListItem>Male<asp:ListItem>Femlae<asp:RequiredFieldValidator ID="RequiredFieldValidator5" runat="server"
ControlToValidate="opt_gender" ErrorMessage="*" ForeColor="#CC3300"
SetFocusOnError="True"></td></tr><tr><td align="right">Salutation</td><td><asp:RadioButtonList ID="opt_salutation" runat="server"
RepeatDirection="Horizontal"><asp:ListItem>Mr<asp:ListItem>Mrs<asp:ListItem>Ms<asp:ListItem>Dr<asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server"
ControlToValidate="opt_salutation" ErrorMessage="*" ForeColor="#CC3300"
SetFocusOnError="True"> </td><td>Others <asp:TextBox ID="txt_saluothers" runat="server"></td><td>Date of Birth <asp:TextBox ID="txt_dob" runat="server"><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server"
ControlToValidate="txt_dob" ErrorMessage="*" ForeColor="#CC3300"
SetFocusOnError="True"><asp:CalendarExtender ID="txt_dob_CalendarExtender" runat="server"
Enabled="True" Format="dd/MM/yy" TargetControlID="txt_dob"
TodaysDateFormat="dd/MM/yy" DefaultView="Years"></td></tr><tr><td align="right">Marital Status</td><td><asp:RadioButtonList ID="opt_maritalstatus" runat="server"
RepeatDirection="Horizontal"><asp:ListItem>Married<asp:ListItem>Unmarried<asp:ListItem>Divorced<asp:ListItem>Widowed<asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server"
ControlToValidate="opt_maritalstatus" ErrorMessage="*" ForeColor="#CC3300"
SetFocusOnError="True"></td><td colspan="2">Others <asp:TextBox ID="txt_ms_others" runat="server"></td></tr></table><table width="100%" border="1.5"><tr><td bgcolor="#66FFFF" colspan="7">Family Details</td></tr><tr><td>Name<

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