Setting textbox value dynamically on a custom bizform

Deanna Martens asked on November 6, 2015 22:38

Hello,

Using a custom bizform, I am trying to set the value of a textbox that is a field on that bizform to something specific based on the selected value of a drop down list that is another field on the bizform.

Currently I have the string getting populated by the drop down control. The issue is now getting that string to display in the textbox on the form.

Below is the most recent attempt at not only saving the data of the textbox but also displaying it to the user before the submit button is clicked.

TextBox BonusAmount = (TextBox)viewBiz.FindControl("BonusAmount");

BonusAmount.Text = bonusAmount;

Thank you for your time, Deanna

Correct Answer

Brenden Kehren answered on November 8, 2015 18:01

@Deanna I think I know what you're looking for now. What I'd do is add some jQuery/JavaScript to the page the form is on. Create an event for the dropdown to populate the textbox. As David mentioned and in my tests too, it only populates the first item. So a simple solution is to use some client side code.

Sample rendered HTML

<select id="stores">
    <option value="store1">Store 1</option>
    <option value="store2">Store 2</option>
    <option value="store3">Store 3</option>
    <option value="store4">Store 4</option>
</select>
<input name="storenum" type="text" id="storenum" />

jQuery

$("#stores").change(function () {
    $("#storenum").val($(this).val());
});

This will allow you to use the standard form functionality and simply add 3 simple lines of javascript to get your functionality to work as expected.

1 votesVote for this answer Unmark Correct answer

Recent Answers


Joshua Adams answered on November 6, 2015 22:49

If through C#, then you should be able to get the value from the form object by this.Form.Data["MyField"] or something similar to that. In this example, I am using the DataForm obj.

//Initialize DataForm because it was created dynamically
DataForm control = innerctl as DataForm;

//fetch the id from the form
int uid = ValidationHelper.GetInteger(control.Data["CustomUserID"],0);

//set the id to the form
control.Data["CustomUserID"] = CurrentUser.UserID;
0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on November 6, 2015 23:54

Hi,

you might want to look into depending fields; Mainly aimed at hiding or enabling fields:

article by Jan Hermann: http://devnet.kentico.com/articles/how-to-work-with-depending-fields

or devnet documentation: https://docs.kentico.com/display/K82/Reference+-+Field+editor

Although I'm not sure you can get the selected data through without extra code.

David

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 7, 2015 14:39

You can set values with a macro based on selected values in a dropdown box by checking the depends on another field and in the default value of the other field add a macro in there. Shoot me an email with more info and I'll help you out with it.

0 votesVote for this answer Mark as a Correct answer

David te Kloese answered on November 7, 2015 15:51 (last edited on December 10, 2019 02:30)

@Brenden

Hi,

I'm interested too :)... in my quick test I used the "has depending fields" and "depends on other field" and tried the default value field with some macros (along the line of {%dropdownfieldname.Value|(identity)GlobalAdministrator%}.

But updating the dropdown kept returning me the first dropdown value.

Greets,

David

1 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 7, 2015 16:50

That's what I'd do David.

@Deanna, are you talking about 2 different views for this data (insert and/or update or edit)? If so, you have the one field name and create different alternative forms. In your insert alternative form you have the dropdown, then in the edit, you have a textbox which allows an edit. You'd have to provide some validation rules for the textbox upon edit but it should work. If this isn't what your talking about, disregard.

0 votesVote for this answer Mark as a Correct answer

Deanna Martens answered on November 9, 2015 21:03

Thank you for your responses, I really appreciate them!

Adding the jQuery worked perfectly, I just have to tweak the function to suit my needs now. My only question was if there was a cleaner way of referencing/changing the bizform field's id? Currently it is p_lt_ctl01_pageplaceholder_p_lt_ctl00_On_lineForm_viewBiz_BonusType_dropDownList

Thank you again for your time, Deanna

0 votesVote for this answer Mark as a Correct answer

Brenden Kehren answered on November 10, 2015 15:02

Not a problem. You definitely don't want to use the dynamically created ID. So I'd use a different jQUery selector for that dropdown list. To make it easy you can go to the actual form field and in the CSS styles area add a custom class name in the field css class box. Then in your jQuery reference that, it should give you what you're looking for.

Another way is to use the field name. By default, Kentico wraps a <div> tag around a field and it's label with the ID of that div tag set to the field name property in the form field definition. So you could also use that ID selector with the <select> tag if you didn't want to add a different css class.

0 votesVote for this answer Mark as a Correct answer

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