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

Grid Template Column-Get data from User Control

5 Answers 152 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Pams
Top achievements
Rank 1
Pams asked on 07 Sep 2010, 02:49 PM
I have a telerik Radgrid. For all the columns I will attach the data using EMP object datasource. Only for one column I use itemTemplateColumn. For filling that column I have created a user control. Lets say I have Emp details in the grid. So, I need to get the Emp address details (other database table) into that template column. Now, I have to send the row specific  ID to the user control which has the paramter EMPID . Then in the user control, we connect to the table, use EmpID as parameter and retrieve the address details. We attach all the address details to the textbox which is present in the Usercontrol. So, this textbox should be present in all the rows under template column. Will it be possible to call the usercontrol in the aspx page.

5 Answers, 1 is accepted

Sort by
0
Princy
Top achievements
Rank 2
answered on 08 Sep 2010, 02:25 PM
Hello Anu,

To add a UserControl to a wep page first add a directive that registers the control.

<%@ Register src="WebUserControl.ascx" tagname="WebUserControl" tagprefix="uc1" %>

Then create a tag for your control where you want the control to appear. Here it is inside ItemTemplate of GridTemplateColumn

<telerik:GridTemplateColumn>
<ItemTemplate>
   <uc1:WebUserControl ID="WebUserControl1" runat="server" />
</ItemTemplate>
</telerik:GridTemplateColumn>

Then in usercontrol add the TextBox where you want to dispaly the employee details like below.

User Control:
<asp:TextBox ID="TextBox1" runat="server" Text='<%#  Eval( "EmployeeDetails" ) %>'></asp:TextBox>

Could you elaborate your requirement if it doesn't help?

Thanks,
Princy.
0
Pams
Top achievements
Rank 1
answered on 08 Sep 2010, 02:55 PM

//The user control is Sample.ascx---Has one textbox


<asp:TextBox ID="rolesBox" runat="server" TextMode="MultiLine" ></asp:TextBox>


//In Sample.ascx.cs

public partial class Sample : BaseComponent

    {

//EmployeeId is the parameter whose data should come from aspx page

        [Browsable(false)]

        public long EmployeeId { get; set; }

       

  protected void Page_Load(object sender, EventArgs e)

        {

            TextBox rolesBox = new TextBox();

//Emp has different roles

//semproles—containes the roles related to the Employee with ID equal to EmployeeId

            foreach (SmsEmployeeRole role in semproles)

            {

                rolesBox.Text += role.RolePkId.ToString() + Environment.NewLine;

            }

        }

    }


//In Programme.aspx


<%@ Register Src="~/Components/Sample.ascx" TagName="eSample" TagPrefix="es" %>

//In the grid—Gets data from Objectdatasource. It has one column called ID

<telerik:GridTemplateColumn AutoPostBackOnFilter="true" UniqueName="RolesColumn" CurrentFilterFunction="Contains" >

   <HeaderTemplate>

        <asp:Label ID="Label1" runat="server" Text="Sample"></asp:Label>

   </HeaderTemplate>                       

   <ItemTemplate>                       

      <es:eSample runat="server" ID="UserControlSample" EmployeeId= '<%# ((long)Eval("Id")) %>' ></es:eSample>

   </ItemTemplate>                                     

 </telerik:GridTemplateColumn>


//Here The EmployeeId of usercontrol should be intialized to the Id value for each row of the grid and the textbox collecting all the addressdetails in the usercontrol should be place in the Itemtemplate column.

//When I debug the code, The EmployeeId parameters value in usercontrol is always ‘0’. But the (long)Eval(“Id”) is giving the correct Id values.

0
Pams
Top achievements
Rank 1
answered on 09 Sep 2010, 07:06 PM
Thanks for your reply. I am new to this controls.
I tried passing parameter this way. but it didn't work.
Any help pls.

Thanks
0
Pams
Top achievements
Rank 1
answered on 10 Oct 2010, 10:46 PM
As I posted long time back I didn't get any code snippet which works to solve my problem.
Any help would be appreciated.
In the code snippet which you have given , I didn't find the place where you are passing the grid's row specific ID value to the user control.
Thank you
0
Pams
Top achievements
Rank 1
answered on 11 Oct 2010, 12:23 AM
Here I am giving my clear requirement.

I have radgrid which populates the Id, Name and Address details.
Id and Name columns are from same table and I am able to bind them properly. The address details are in the different table in database
I have the requirement that I need to create a usercontrol with a textbox. This user control has the property and receives the EmployeeId value as input.
The method in the user control code behind is used to collect the address details and populates the textbox text with this information.

The thing which I am looking for is...
I have used Gridtemplatecolumn in the radgrid for address details as follows

<%@ Register Src="~/Sample.ascx" TagName="eSample" TagPrefix="es" %>

//In the grid—Gets data from Objectdatasource. It has one column called ID

<telerik:GridTemplateColumn AutoPostBackOnFilter="true" UniqueName="RolesColumn"CurrentFilterFunction="Contains" >

   <HeaderTemplate>

        <asp:Label ID="Label1" runat="server" Text="Sample"></asp:Label>

   </HeaderTemplate>                       

   <ItemTemplate>                       

      <es:eSample runat="server" ID="UserControlSample" EmployeeId= '<%# ((long)Eval("Id")) %>' ></es:eSample>

   </ItemTemplate>                                     

 </telerik:GridTemplateColumn>

// Here EmployeeId is the property present in the User control Sample.ascx and "Id" is the EmployeeId value from the ID column in the datasource binded to the radgrid.

//The user control is Sample.ascx---Has one textbox


<asp:TextBox ID="addressBox" runat="server" TextMode="MultiLine" ></asp:TextBox>


//In Sample.ascx.cs

public partial class Sample :

    {

//EmployeeId is the parameter whose data should come from aspx page

        [Browsable(false)]

        public long EmployeeId { getset; }

       

  protected void Page_Load(object sender, EventArgs e)

        {

            TextBox addressBoxnew TextBox();
//code to bind data to addressBox.Text
           

        }

    }


This is the way which I tried. I dont know whether it is correct or not.

Any help pls.

Thank you.



Tags
Grid
Asked by
Pams
Top achievements
Rank 1
Answers by
Princy
Top achievements
Rank 2
Pams
Top achievements
Rank 1
Share this question
or