This is a migrated thread and some comments may be shown as answers.

Radgrid change itemtemplate

3 Answers 372 Views
Grid
This is a migrated thread and some comments may be shown as answers.
fer
Top achievements
Rank 1
fer asked on 01 Sep 2010, 04:41 PM
Hi

I use radgrid with itemtemplate like this


<ItemTemplate>
            <asp:ImageButton ID="Pict" runat="server" ImageUrl='<%# "~/mypic/" + Eval("picture") %>'
                    OnClientClick='<%# "Pic_click(" + Eval("ID") + ")" %>' />

             <div style="text-align: left">
                <br />
               <span style="font-family:Verdana; font-size: large; color: #000080;">
                    <%# Eval("Name") %>
                    </span>
                    <br />
                    <br />
               <span style="font-size:large; font-family:Verdana; color: #800000;">
                    <%# Eval("price", "{0:N} TL") %>
                    </span>
                    <br />
                   
             </div>  

             <div align="right">
                 <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Add" ImageUrl="~/Common/B.png" />
             </div>

           
            </ItemTemplate>



i will put a button on page and when user clicked it i will change mygrid to standart column type grid without itemtemplates.
columns with (name, price, ....),  after reclicking will change to first type with itemtemplates.

how can i do this ?..


best regards

3 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 02 Sep 2010, 06:47 AM
Hello,

If you want to toggle the ItemTemplate cell content on a button-click event, then using container controls (like asp:Panel, PlaceHolder, AjaxPanel etc ...) will be an option. In the button-click event, access the container contriol and set the Visible property accordingly.

The following code shows an example of similar approach. Feel free to change the code based on your requirement.

ASPX:
<telerik:GridTemplateColumn HeaderText="Temp Column">
    <ItemTemplate>
        <asp:Panel ID="Panel1" runat="server">
            <asp:ImageButton ID="Pict" runat="server" ImageUrl="~/Images/btnCancel.gif" />
            <div style="text-align: left">
                <br />
                <span style="font-family: Verdana; font-size: large; color: #000080;">
                    <%# Eval("FirstName") %>
                </span>
                <br />
                <br />
                <span style="font-size: large; font-family: Verdana; color: #800000;">
                    <%# Eval("EmployeeID", "{0:N} TL") %>
                </span>
                <br />
            </div>
            <div align="right">
                <asp:ImageButton ID="ImageButton1" runat="server" CommandName="Add" ImageUrl="~/Images/btnCancel.gif" />
            </div>
        </asp:Panel>
        <asp:Panel Visible="false" ID="Panel2" runat="server">
            <%# Eval("FirstName") %>
        </asp:Panel>
    </ItemTemplate>
</telerik:GridTemplateColumn>


C#:
protected void Button1_Click(object sender, EventArgs e)
{
    Button btn = (Button)sender;
    if (btn.Text == "Clear ItemTemplate")
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            Panel panel1 = (Panel)item.FindControl("Panel1");
            panel1.Visible = false;
            Panel panel2 = (Panel)item.FindControl("Panel2");
            panel2.Visible = true;            
            btn.Text = "Show ItemTemplate";
        }
    }
    else
    {
        foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
        {
            Panel panel1 = (Panel)item.FindControl("Panel1");
            panel1.Visible = true;
            Panel panel2 = (Panel)item.FindControl("Panel2");
            panel2.Visible = false;             
            btn.Text = "Clear ItemTemplate";
        }
    }
}


Thanks,
Princy.
0
fer
Top achievements
Rank 1
answered on 02 Sep 2010, 11:11 PM
Yes what i want is similar to that

but there is some problems;

1- There is paging in my grid. For every page  do i have to do some coding ?.. or is there a simple method.

2- On the second panel i put spans. but it is too ugly.
can't i show the grid's default column type
i mean this
at first i want to show itemtemplate part
second columns part.

(When i put a panel to columns part it gives error.)
0
Princy
Top achievements
Rank 2
answered on 03 Sep 2010, 02:06 PM
Hello,

If you have enabled paging then you can set the panel visibility on PreRender event. And change the Button Text in Button_Click event like below.

ASPX:
<asp:Button ID="Button1" runat="server" Text="Clear ItemTemplate" OnClick="Button1_Click" />

C#:
protected void RadGrid1_PreRender(object sender, EventArgs e)
    {
       if (Button1.Text == "Show ItemTemplate")
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                Panel panel1 = (Panel)item.FindControl("Panel1");
                panel1.Visible = false;
                Panel panel2 = (Panel)item.FindControl("Panel2");
                panel2.Visible = true;
            }
        }
        else
        {
            foreach (GridDataItem item in RadGrid1.MasterTableView.Items)
            {
                Panel panel1 = (Panel)item.FindControl("Panel1");
                panel1.Visible = true;
                Panel panel2 = (Panel)item.FindControl("Panel2");
                panel2.Visible = false;
            }
        }
    }
    
 
 protected void Button1_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        if (btn.Text == "Clear ItemTemplate")
            btn.Text = "Show ItemTemplate";
        else
            btn.Text = "Clear ItemTemplate";
     }

Thanks,
Princy.
Tags
Grid
Asked by
fer
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
fer
Top achievements
Rank 1
Share this question
or