How do I get a field value (CourseID) from the code behind of a custom web user control

Sam Riveros asked on June 5, 2018 19:41

How do I get a field value (CourseID) from the code behind of a custom web user control which has been registered in a custom form layout?

I’ve been using the following code which inherit from CMSAbstractLayoutWebPart CurrentDocument.GetValue(“CourseID”);

But no luck...

Fields

Register Custom Web User Control

Custom Web User Control

Visual Studio Code Behind

Server Error

Thanks in advance!!

Correct Answer

Sam Riveros answered on June 7, 2018 18:48

Hi Rui,

Thanks for the help! I was definitely looking in the wrong places. I didn't know I was adding the control into the Layout incorrectly. But after looking at the documentation you sent me, while comparing with existing Kentico controls found in the CMCFOrmControls folder helped me figure this out.

I also found additional information which was really helpful: Example - Developing custom form controls

Below is what I ended up using to get the ID to display. This way I can also display a message like "No ID", until the new item is saved (instead of a 0). However, ultimately, the purpose of getting the ID is not to display it to the end user (just for testing), but eventually so that it can then be used in a select statement.

I am inheriting from ReadOnlyFormEngineUserControl and using CMS.FormEngine.Web.IU.

string courseID = CMS.Helpers.ValidationHelper.GetString(Form.GetFieldValue("CourseID"), "No ID");

Success

0 votesVote for this answer Unmark Correct answer

Recent Answers


Rui Wang answered on June 5, 2018 20:40

Sam, the default xxxxID is the primary key of the page type, so it would be starting from 1 and go up. Is that truly what you are looking for or you want to store real CourseID like other fields?

Also, that CourseID would be in the custom table created for for the Course page type, but when you do CurrentDocument, it would only load data from CMS_Tree and CMS_Document table joined (View_CMS_Tree_Joined), thus that field is not available. If you want to get the CourseID from the View_CMS_Tree_Joined, it would be "DocumentForeignKeyValue".

0 votesVote for this answer Mark as a Correct answer

Sam Riveros answered on June 6, 2018 01:35 (last edited on June 6, 2018 01:41)

@Rui Wang

Thanks for the response...

The primary key doesn't always start at 1 as users will delete items. As you can see in the image below, it's currently starting at 30. And the item I have selected in Kentico is CourseID 41 in the database. Then next item up is 46. Also, items 30 to 40 isn't even under Professional Firefighting... It has a completely different parent. I need to know which item user has currently selected in the Kentico tree (even if user has re-arranged them in the tree) by CourseID (primary key) so that I can find the related values in other columns (CourseName, CourseSnippet, etc).

SqlParameter courseIDParameter = new SqlParameter("@courseIDParameter", SqlDbType.Int, 4); courseIDParameter.Value = CurrentDocument.GetValue("CourseID");

SqlCommand myCommand = new SqlCommand("SELECT * FROM CourseTable WHERE CourseID = @courseIDParameter", myConnection);

Which assembly is DocumentForeignKeyValue from? I am using Kentico 11. And I inherit from CMSAbstractLayoutWebPart.

Can you show me an example of how you would use DocumentForeignKeyValue in CourseForm.ascx.cs to retrieve CourseID or CourseName of the selected item?

CourseID

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on June 6, 2018 04:08

I was just saying when you create a new page type, first page of that type will have ID 1, then it goes up +1. Then of course editor add/delete pages, and the number just keep going up.

You can get the value just like this CurrentDocument.GetValue("DocumentForeignKeyValue")

Also, you should consider using Object Query instead of SQL query directly. https://docs.kentico.com/k11/custom-development/working-with-pages-in-the-api

0 votesVote for this answer Mark as a Correct answer

Sam Riveros answered on June 6, 2018 17:58

Hi Rui,

Thanks again for your response. I tried your suggestion; however, I keep getting the same reference not set exception. Any ideas what I might be doing wrong?

Image Text

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on June 6, 2018 18:09

Sam, it's better for you to enable debug to see what's going on. I have tried both

protected void Page_Load(object sender, EventArgs e)
{
    ltlH1Text.Text = "<h1>" + CurrentDocument.GetValue("DocumentForeignKeyValue") + "</h1>";
}

and

protected void SetupControl()
{
    if (this.StopProcessing)
    {
        // Do not process
    }
    else
    {
        ltlH1Text.Text = "<h1>" + CurrentDocument.GetValue("DocumentForeignKeyValue") + "</h1>";
    }
}

and both return the ID.

0 votesVote for this answer Mark as a Correct answer

Sam Riveros answered on June 6, 2018 19:49

Hi Rui,

I enabled debugging...

Are you testing your results by looking at the Design tab or the Form tab? I've been successful in displayin the CourseID on the Design tab simply using Document.GetValue("CourseID"). It's when I am looking at it from within the Form tab that I get the error.

Image Text

The custom control is inside the Layout of a Page types.

Image Text

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on June 6, 2018 20:00 (last edited on June 6, 2018 20:14)

oh, that small detail. So you are not building a web part but a form control? Then you should looking into the document for form control (https://docs.kentico.com/k11/custom-development/developing-form-controls/creating-form-controls-with-xml-values)

public partial class CMSFormControls_CustomControl : FormEngineUserControl

Also, you cannot add a form control directly into the layout like that. It should be assigned to a field.

0 votesVote for this answer Mark as a Correct answer

Rui Wang answered on June 6, 2018 20:12 (last edited on June 6, 2018 20:13)

Also, what exactly are you trying to do with this "form control"? Show the current CourseID? In that case, you could add a text filed, and set the form control to be "label" from control, and set the output format property to {% CourseID %}.

It will be 0 when you create a new course because the new item hasn't been saved yet, but once you save the new course, that label will show the CourseID.

0 votesVote for this answer Mark as a Correct answer

   Please, sign in to be able to submit a new answer.