Click here to Skip to main content
12,942,905 members (41,987 online)
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,
i have an asp page like this:

<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("cell")%>' />
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("group")%>' />
 </td>
<td>
<asp:Label ID="Label1" runat="server" Text='<%#Eval("nm")%>' />
</td>
<td>
<asp:CheckBox ID="CheckBox1" Text="" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button Text="Change" runat="server" OnClick="ChangeItems"/>


that contains asp:repeater and asp:button
i want to do that, when user clicked on button, the content of asp:repeater, changed to this, for example:

<pre lang="HTML">
<ItemTemplate>
<tr>
<td>
<asp:Label ID="Label2" runat="server" Text='<%#Eval("new")%>' />
</td>
<td>
<asp:Label ID="Label3" runat="server" Text='<%#Eval("test")%>' />
 </td>
<td>
<asp:CheckBox ID="CheckBox1" Text="" runat="server" />
</td>
</tr>
</ItemTemplate>
</asp:Repeater>
<asp:Button Text="Change" runat="server" OnClick="ChangeItems"/>
</pre>
Posted 19-Dec-12 1:40am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

do you want to do this work in client side or server side? ok. for client side you can visible/hidden with javascript.

like for example:

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <script type="text/javascript">
        function ToggleRepeater(chkVal) {
            var tr1 = document.getElementById('trValue1');
            var tr2 = document.getElementById('trValue2');
 
            tr1.style.visibility = "hidden";
            tr2.style.visibility = "hidden";
            if (chkVal == 1) {
 
                tr2.style.visibility = "visible";
            }
            else {
                tr1.style.visibility = "visible";
            }
 
        }
    </script>
    <form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rpter1" runat="server">
            <ItemTemplate>
                <table border="0" cellpadding="0" cellspacing="0">
                    <tr id="trValue1">
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("val1")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("val2")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("val3")%>' />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox1" Text="" runat="server" onclick="ToggleRepeater(1)" />
                        </td>
                    </tr>
                    <tr id="trValue2" style="visibility:hidden">
                        <td>
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("val4")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label5" runat="server" Text='<%#Eval("val5")%>' />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox2" Text="" runat="server"  onclick="ToggleRepeater(2)"/>
                        </td>
                    </tr>
                </table>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>
</body>
</html>



code behind:

  protected void Page_Load(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("val1", typeof(string));
            dt.Columns.Add("val2", typeof(string));
            dt.Columns.Add("val3", typeof(string));
            dt.Columns.Add("val4", typeof(string));
            dt.Columns.Add("val5", typeof(string));
 
            DataRow drNew = dt.NewRow();
            drNew["val1"] = "value 1";
            drNew["val2"] = "value 2";
            drNew["val3"] = "value 3";
            drNew["val4"] = "value 4";
            drNew["val5"] = "value 5";
            dt.Rows.Add(drNew);
 

            rpter1.DataSource = dt;
            rpter1.DataBind();
        }
  Permalink  
Comments
shajarian_lover 19-Dec-12 11:06am
   
no, i want to do that server side
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

but I think its a bad idea, because only for visible and disable its not good to go server side.
ok, then I am giving you this code:

client side:

<form id="form1" runat="server">
    <div>
        <asp:Repeater ID="rpter1" runat="server">
            <ItemTemplate>
            <% if (status == "first")
               { %>
                <table id="tbl1" runat="server"  border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <asp:Label ID="Label2" runat="server" Text='<%#Eval("val1")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label3" runat="server" Text='<%#Eval("val2")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label1" runat="server" Text='<%#Eval("val3")%>' />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox1" runat="server" Text="" AutoPostBack="true" Checked="false" OnCheckedChanged="CheckBox1_ToggleRepeater" />
                        </td>
                    </tr>
 
                </table>
                <%}
                    if (status == "second")
               { %>
                <table id="tbl2" runat="server" border="0" cellpadding="0" cellspacing="0">
                    <tr>
                        <td>
                            <asp:Label ID="Label4" runat="server" Text='<%#Eval("val4")%>' />
                        </td>
                        <td>
                            <asp:Label ID="Label5" runat="server" Text='<%#Eval("val5")%>' />
                        </td>
                        <td>
                            <asp:CheckBox ID="CheckBox2" Text="" runat="server"  AutoPostBack="true" Checked="false" OnCheckedChanged="CheckBox2_ToggleRepeater"/>
                        </td>
                    </tr>
                </table>
                <%} %>
            </ItemTemplate>
        </asp:Repeater>
    </div>
    </form>

server side:

public string status { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
           this.status = "first";
           if (!IsPostBack)
           {
               DataTable dt = new DataTable();
               dt.Columns.Add("val1", typeof(string));
               dt.Columns.Add("val2", typeof(string));
               dt.Columns.Add("val3", typeof(string));
               dt.Columns.Add("val4", typeof(string));
               dt.Columns.Add("val5", typeof(string));
 
               DataRow drNew = dt.NewRow();
               drNew["val1"] = "value 1";
               drNew["val2"] = "value 2";
               drNew["val3"] = "value 3";
               drNew["val4"] = "value 4";
               drNew["val5"] = "value 5";
               dt.Rows.Add(drNew);
 

               rpter1.DataSource = dt;
               rpter1.DataBind();
 
           }
 
           
        }
        protected void CheckBox1_ToggleRepeater(object sender, EventArgs e)
        {
            CheckBox objChecked = (CheckBox)sender;
            if (objChecked.Checked == true)
            {
                this.status = "second";
                objChecked.Checked = false;
            }
        }
        protected void CheckBox2_ToggleRepeater(object sender, EventArgs e)
        {
            CheckBox objChecked = (CheckBox)sender;
            if (objChecked.Checked == true)
            {
                this.status = "first";
                objChecked.Checked = false;
            }
          
           
        } 
  Permalink  

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 4,565
CHill60 2,860
Maciej Los 2,178
Jochen Arndt 1,900
ppolymorphe 1,755


Advertise | Privacy | Mobile
Web02 | 2.8.170518.1 | Last Updated 19 Dec 2012
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100