1

I have created a panel in my page view (tour.aspx file). Now I want to access it in my class file (add_tour.cs file).

This is my panel:

<asp:Panel ID="itinerary_panel" runat="server"></asp:Panel>

This is my code behind tour.aspx file:

add_tour tour_obj = new add_tour();
int days_count = 2;
tour_obj.textbox_generator(days_count);

And this code is in add_tour.cs file:

public void textbox_generator(int days_count)
{

}

Now how to access the panel from aspx file? Please help.

krlzlx
  • 5,752
  • 14
  • 47
  • 55

2 Answers2

1

There's no need to actually add the text boxes to the panel from this class.

public List<TextBox> textbox_generator(int days_count)
{
    var textBoxes = new List<TextBox>();

    for(int i = 0; i < days_count; i++)
    {
        txt_desc = new TextBox();
        txt_desc.ID = "txt_desc" + i.ToString();
        txt_desc.CssClass = "form-control";
        txt_desc.Attributes.Add("placeholder", "Enter day " + i + " description");
        txt_desc.TextMode = TextBoxMode.MultiLine;
        textBoxes.Add(txt_desc);          
    }

    return textBoxes;
}

Then change your code behind to:

add_tour tour_obj = new add_tour();
int days_count = 2;
var textBoxes = tour_obj.textbox_generator(days_count);
foreach(var textBox in textBoxes)
{
    itinerary_panel.Controls.Add(textBox);
}

Note that you need to be careful where you add these controls in the page lifecycle. See Microsoft documentation.

This keeps your textbox_generator from needing to know anything about the specific page using it.

Also, you should really align your naming conventions with C# standards. Use PascalCasing. textbox_generator should be TextBoxGenerator etc. And you can probably make textbox_generator into a static method if it doesn't need to access any fields or properties of its class.

mason
  • 31,774
  • 10
  • 77
  • 121
1

If you really wanted your other class to itself add the controls directly to the panel, then you would just pass a reference to the panel from the code behind to the class.

public void textbox_generator(int days_count, Panel panel)
{
    for(int i = 0; i < days_count; i++)
    {
        txt_desc = new TextBox();
        txt_desc.ID = "txt_desc" + i.ToString();
        txt_desc.CssClass = "form-control";
        txt_desc.Attributes.Add("placeholder", "Enter day " + i + " description");
        txt_desc.TextMode = TextBoxMode.MultiLine;
        panel.Controls.Add(txt_desc);          
    }
}

and call this this way from your code behind:

add_tour tour_obj = new add_tour();
int days_count = 2;
var textBoxes = tour_obj.textbox_generator(days_count, itinerary_panel);

This works because itinerary_panel actually is a reference to the panel. See Passing Objects By Reference or Value in C#. However, it's often a bad idea to have a method modify the state in that manner.

Community
  • 1
  • 1
mason
  • 31,774
  • 10
  • 77
  • 121
  • yes its working... and if i want to get the value from all those textboxs then what should i use? Ajax or Page_PreInit? – Ajay Chorge Feb 12 '17 at 15:26
  • Can I ask why you're using web forms and not MVC? – mason Feb 12 '17 at 15:31
  • because this is small scale of website application that's why i am making it with the use of web forms... there are only some dynamic pages. – Ajay Chorge Feb 12 '17 at 17:39
  • @AjayChorge If you're just getting started, learn ASP.NET MVC. Don't waste your time with Web Forms. Web Forms will be going away, is slow, and is a terrible framework. – mason Feb 12 '17 at 17:43
  • Thanks @mason can you please suggest me some material which is available online? Which will helps me learn MVC Framework. – Ajay Chorge Feb 13 '17 at 09:37
  • @AjayChorge I've found various videos to be helpful. See [these](https://channel9.msdn.com/Series/Introduction-to-ASP-NET-MVC) for ASP.NET MVC (watch them in order) and [this](https://channel9.msdn.com/Events/TechEd/NorthAmerica/2014/DEV-B412) for Dependency Injection. – mason Feb 13 '17 at 12:40