0

I've created a basic Visual Web Part, in VS 2017. When I Deploy it to my SharePoint server(2013) It works.

But I'm not sure how to find my table so I can add/remove values in the table.

My VisualWebPart1UserControl.ascx :

<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %> 
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %> 
<%@ Register Tagprefix="WebPartPages" Namespace="Microsoft.SharePoint.WebPartPages" Assembly="Microsoft.SharePoint, Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="VisualWebPart1UserControl.ascx.cs" Inherits="vwp1.VisualWebPart1.VisualWebPart1UserControl" %>

<asp:Table ID="Table1" runat="server"></asp:Table>

My VisualWebPart.cs :

public class VisualWebPart1 : WebPart
{
    // Visual Studio might automatically update this path when you change the Visual Web Part project item.
    private const string _ascxPath = @"~/_CONTROLTEMPLATES/15/vwp1/VisualWebPart1/VisualWebPart1UserControl.ascx";

    protected override void CreateChildControls()
    {
        Control control = Page.LoadControl(_ascxPath);
        Controls.Add(control);
    }
}

Is there a way to access Table1 from my VisualWebPart1.cs ?

  • The table is accesible from the user control itself. You really dont need to access it from VisualWebPart1 class. All logic should be located in VisualWebPart1UserControl.ascx.cs – Lukas Nespor Feb 8 '18 at 11:14
0

All runat="server" control are accessible in .cs file.

Control control = Page.LoadControl(_ascxPath);
Table tbl = (Table)this.FindControl("Table1");
// tbl....
Controls.Add(control);
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.