2

I have a C# ASP .net web form with one panel having many controls like text boxes and drop downs in a tabular column. User is allowed to enter data in these controls and when Copy button is pressed, all the contents of this panel should be copied to another panel on the same page having exactly same controls and columns whose enabled property is set to false

I am only getting the option of doing it like below

eg:-

Protected void Button_click ()
{
    Panel2.textbox1.Text = Panel1.textbox1.Text;
    Panel2.textbox2.Text = Panel1.textbox2.Text;
                   ...
                   ...
                   ...
    Panel2.textbox30.Text = Panel1.textbox30.Text;

which causes lot many lines of code and i feel its just not the good approach. Could you suggest any other alternatives or is it the only way?

My markup looks like this

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm5.aspx.cs" Inherits="AjaxToolkitTrial.WebForm5" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
    <style type="text/css">
        .auto-style1 {
            width: 100%;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    
        <table class="auto-style1">
            <tr>
                <td>
                    <asp:Panel ID="Panel1" runat="server">
                        <table class="auto-style1">
                            <tr>
                                <td>
                                    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:DropDownList ID="DropDownList1" runat="server">
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Copy" />
                                </td>
                            </tr>
                        </table>
                    </asp:Panel>
                </td>
                <td>
                    <asp:Panel ID="Panel2" runat="server">
                        <table class="auto-style1">
                            <tr>
                                <td>
                                    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:DropDownList ID="DropDownList2" runat="server">
                                    </asp:DropDownList>
                                </td>
                            </tr>
                            <tr>
                                <td>
                                    <asp:TextBox ID="TextBox6" runat="server"></asp:TextBox>
                                </td>
                            </tr>
                            <tr>
                                <td>&nbsp;</td>
                            </tr>
                        </table>
                    </asp:Panel>
                </td>
            </tr>
        </table>
    
    </div>
    </form>
</body>
</html>

Thanks in advance..

Rajesh
  • 1,600
  • 5
  • 33
  • 59
codeboyz
  • 43
  • 1
  • 6

3 Answers3

0

you should create all of panel2 controls dynamiclly. Like

    TextBox txt1 = new TextBox()
    TextBox txt2 = new TextBox()
    Panel2.Controls.Add(txt1);
    Panel2.Controls.Add(txt2);
    ...
    ...

This is not good approach too. So you should create a loop and create your all controls in a loop.

Alternatively you can think for using a UserControl..

Mehmet
  • 1,824
  • 3
  • 19
  • 28
0

What i would do in this instance is use some form of templated control like a repeater, then when the copy button is pressed, i copy the data as a new row in the datasource, and then rebind the repeater to the new data - you will then have a copy of both the data and the control in two rows in the repeater.

Symeon Breen
  • 1,531
  • 11
  • 25
0

you can consider this as hint, but the name of controls should be same in both the panels to identify it.. see the below pseudocode which may help you

foreach(Control c in Panel1.Controls)
{
    var control = Panel2.Controls.AsEnumerable().Where(p => p.name = c.name).select();
    Textbox txt = control as TextBox;
    c.Text = txt.Text;
}
Bhuvan Ram
  • 61
  • 3