I was wondering if someone would be help me with a problem that I'm having.
I don't know how well this is going to work in my application but we'll see. I have that an application that contains a lot of data in a radgrid which is reading from a SQL server database. The radgrid allows users to insert parameters using the PerformInsertCommandName funcitonality.
However, what I'm trying to do is insert a dropdown list into the edit form and bind it to a table which contains all of the possible parameters that can be inserted into each file (these files are displayed using a treeview - once a user clicks on the required file the radgrid is displayed with its data). All of the parameters have extra information which need to be added along with a parameter value which would be manually added into the database using this edit form - the extra information would remain the same and parameter value would be the field most likely to be updated.
So what I'm trying to do is use this dropdown list to populate the textboxes in the edit form with the extra information as I think this way will save some time when inputting new parameters. All the user will need to update is the parameter value . Once the required textboxes have been filled it can then be inserted into the radgrid (table).
I have managed to bind the dropdownlist to a datasource to display the relevant parameters, I'm struggling to get it to populate the relevant textboxes when a parameter is chosen. Is it best to use the OnSelectedIndexChanged event or use a select button?
Sorry for the long winded post :)
Thanks in advance
10 Answers, 1 is accepted
A better idea is using OnSelectedIndexChanged event of DropDownList to populate the TextBoxes in editform by the following approach since it does not need an extra button-click.
In the event handler access the EditFormItem using NamingContainer property of DropDownList. And then access the TextBoxes using FindControl() method and populate it according to the SelectedValue of DropDownList. Check out the following sample code.
C#:
protected void RadGrid1_SelectedIndexChanged(object sender, EventArgs e) |
{ |
DropDownList drlist = (DropDownList)sender; |
string value = drlist.SelectedValue; |
GridEditFormItem edititem = (GridEditFormItem)drlist.NamingContainer; |
TextBox txt_Title = (TextBox)edititem.FindControl("txtTitle"); //access the TextBox |
//populate TextBox based on SelectedValue of DropDownList |
} |
Regards,
Shinu.
I've looked at your sample code and have altered it so that it looks like this:
protected void parameterList_SelectedChanged(object sender, EventArgs e)
{
DropDownList parametersList = (DropDownList)sender;
string value = parametersList.SelectedValue;
GridEditableItem edititem = (GridEditableItem)parametersList.NamingContainer;
TextBox TextBoxID = (TextBox)FindControl("TextBoxID");
TextBox TextBoxParameterValueID = (TextBox)FindControl("TextBoxParameterValueID");
TextBox TextBoxCodeName = (TextBox)FindControl("TextBoxCodeName");
TextBox parameterDescription = (TextBox)FindControl("parameterDescription");
TextBox TextBoxDefaultValue = (TextBox)FindControl("TextBoxDefaultValue");
TextBox TextBoxMinValue = (TextBox)FindControl("TextBoxMinValue");
TextBox TextBoxMaxValue = (TextBox)FindControl("TextBoxMaxValue");
DropDownList dropDownListTab = (DropDownList)FindControl("dropDownListTab");
TextBoxID.Text = parametersList.SelectedValue;
TextBoxParameterValueID.Text = parametersList.SelectedValue;
TextBoxCodeName.Text = parametersList.SelectedValue;
parameterDescription.Text = parametersList.SelectedValue;
TextBoxDefaultValue.Text = parametersList.SelectedValue;
TextBoxMinValue.Text = parametersList.SelectedValue;
TextBoxMaxValue.Text = parametersList.SelectedValue;
}
The page seems to refresh when I select an item from the dropdown but it doesn't populate any of the textboxes - is what I've added to the sample code you provided correct?
Thanks
In order to access the TextBox in edit form , you have to specify that the control is in edit mode and then access using FindControl() method like below.
C#:
GridEditableItem edititem = (GridEditableItem)parametersList.NamingContainer; |
TextBox TextBoxID = (TextBox)edititem.FindControl("TextBoxID"); |
Regards,
Shinu.
That still doesn't appear to work - below is the code for the parameter list and insert command for when the textboxes are eventually populated and the asp page code for the dropdown lists and textboxes - can you see where I'm going wrong? I really appreciate your help.
<
EditFormSettings EditFormType="Template" PopUpSettings-Width="730px">
<FormTemplate>
<div style="padding: 10px 10px 10px 10px;">
<div style="padding-bottom: 2px;">
<b>Parameter Details</b></div>
<div class="moduleGray" style="width: 680px;">
<table>
<tr>
<td style="width: 100px;">
Name:
</td>
<td style="width: 240px;">
<asp:DropDownList ID="parametersList" runat="server" DataSource='<%# GetParameters() %>'
DataValueField="friendlyName" Width="240px" AutoPostBack="true" OnSelectedIndexChanged="parameterList_SelectedIndexChanged"
SelectValue = '<%# (Container is GridEditFormInsertItem) ? 3 : Eval( "friendlyName") %>'/>
</td>
<td style="width: 140px; padding: 0 0 0 10px;">
Value:
</td>
<td style="width: 240px;">
<asp:TextBox ID="TextBoxValue" Width="200px" Text='<%# (Container is GridEditFormInsertItem) ? 0.0 : Eval( "parameterValue") %>'
runat="server" TabIndex="9"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:CheckBox Text="Default: " ID="CheckBoxDefault" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? true : Eval( "IsDefault") %>' />
protected void parameterList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList parametersList = (DropDownList)sender;
string value = parametersList.SelectedValue;
GridEditableItem edititem = (GridEditableItem)parametersList.NamingContainer;
DropDownList dropDownListTab = (DropDownList)edititem.FindControl("dropdownListTab");
TextBox TextBoxID = (TextBox)edititem.FindControl("TextBoxID");
TextBox TextBoxParameterValueID = (TextBox)edititem.FindControl("TextBoxParameterValueID");
TextBox TextBoxCodeName = (TextBox)edititem.FindControl("TextBoxCodeName");
TextBox parameterDescription = (TextBox)edititem.FindControl("parameterDescription");
TextBox TextBoxDefaultValue = (TextBox)edititem.FindControl("TextBoxDefaultValue");
TextBox TextBoxMinValue = (TextBox)edititem.FindControl("TextBoxMinValue");
TextBox TextBoxMaxValue = (TextBox)edititem.FindControl("TextBoxMaxValue");
}
protected void RadGrid1_ItemCommand(object source, Telerik.Web.UI.GridCommandEventArgs e)
{
SiteComponent =
new SiteComponent();
// Insert Command
if (e.CommandName == RadGrid.PerformInsertCommandName)
{
GridEditableItem editedItem = e.Item as GridEditableItem;
string friendlyName = (editedItem.FindControl("TextBoxName") as TextBox).Text;
string parameterDescription = (editedItem.FindControl("parameterDescription") as TextBox).Text;
string parameterReference = (editedItem.FindControl("RadEditorReferenceLinks") as RadEditor).Content.ToString();
int parameterTypeID = Convert.ToInt16((editedItem.FindControl("DropDownListType") as DropDownList).SelectedItem.Value);
int parameterValueID = Convert.ToInt16((editedItem.FindControl("TextBoxParameterValueID") as TextBox).Text);
float defaultValue = (float)Convert.ToDecimal((editedItem.FindControl("TextBoxDefaultValue") as TextBox).Text);
float parameterValue = (float)Convert.ToDecimal((editedItem.FindControl("TextBoxValue") as TextBox).Text);
float minValue = (float)Convert.ToDecimal((editedItem.FindControl("TextBoxMinValue") as TextBox).Text);
float maxValue = (float)Convert.ToDecimal((editedItem.FindControl("TextBoxMaxValue") as TextBox).Text);
int simcypTabID = Convert.ToInt16((editedItem.FindControl("DropDownListTab") as DropDownList).SelectedItem.Value);
int simcypID = Convert.ToInt16((editedItem.FindControl("TextBoxID") as TextBox).Text);
DateTime postedDate = DateTime.Now;
string postedBy = Context.User.Identity.Name.ToString();
string parameterCodeName = (editedItem.FindControl("TextBoxCodeName") as TextBox).Text;
bool isDefault = (editedItem.FindControl("CheckBoxDefault") as CheckBox).Checked;
bool isUsed = (editedItem.FindControl("CheckBoxUsed") as CheckBox).Checked;
bool isPredicted = (editedItem.FindControl("CheckBoxPredicted") as CheckBox).Checked;
int dataSetID = (int)Session["dataSetID"];
int unitID = Convert.ToInt16((editedItem.FindControl("DropDownListUnits") as DropDownList).SelectedItem.Value);
string tooltip = (editedItem.FindControl("TextBoxTooltip") as TextBox).Text;
SiteComponent.InsertParameter("Stored Procedure name here")}
 
Thanks
 
Here is the complete code that I tried, and it is working fine in my end. Please examine the code below and check whether it is working for you.
ASPX:
<EditFormSettings EditFormType="Template" PopUpSettings-Width="730px"> |
<FormTemplate> |
Name: |
<asp:DropDownList ID="DropDownList1" runat="server" DataSource='<%# GetParameters() %>' |
DataValueField="friendlyName" Width="240px" AutoPostBack="true" OnSelectedIndexChanged="parameterList_SelectedIndexChanged" |
SelectValue='<%# (Container is GridEditFormInsertItem) ? 3 : Eval( "friendlyName") %>' /> |
Value: |
<asp:TextBox ID="TextBoxValue" Width="200px" Text='<%# (Container is GridEditFormInsertItem) ? 0.0 : Eval( "parameterValue") %>' |
runat="server" TabIndex="9"></asp:TextBox> |
</FormTemplate> |
</EditFormSettings> |
C#:
protected void parameterList_SelectedIndexChanged(object sender, EventArgs e) |
{ |
DropDownList parametersList = (DropDownList)sender; |
string value = parametersList.SelectedValue; |
GridEditableItem edititem = (GridEditableItem)parametersList.NamingContainer; |
TextBox TextBoxID = (TextBox)edititem.FindControl("TextBoxValue"); |
TextBoxID.Text = parametersList.SelectedValue; |
} |
Have you used the same textbox id as in FormTemplate to access the control?
Regards,
Shinu.
That worked thanks a lot for your help - it's much appreciated.
Thanks
I now need to populate more than one textbox using this same dropdown list however when I add more textboxes in the codebehind it populates them with item contained in the dropdown list - I want the textboxes to be populated with the dropdown lists additional information i.e. when an item is selected the textboxes are populated with the extra information relating to the selected item.
How do I get this to work?
Thanks
The approach remains the same, regardless of whether you are using single or multiple textboxes - when the selection is made, you can locate any other control, nested in the edit form, and set its value accordingly. Let me know if this approach falls short in any way.
Sincerely yours,
Yavor
the Telerik team
<
asp:DropDownList
ID
=
"parametersList"
runat
=
"server"
DataValueField
=
"friendlyName"
Width
=
"240px"
AutoPostBack
=
"true"
OnSelectedIndexChanged
=
"parameterList_SelectedIndexChanged"
DataSource='<%# GetParameters() %>' SelectValue = '<%# (Container is GridEditFormInsertItem) ? 3 : Eval( "ID") %>'/>
</td>
<td>
<td style="width: 240px;">
<asp:TextBox ID="TextBoxValue" Width="200px" Text='<%# (Container is GridEditFormInsertItem) ? 0.0 : Eval( "parameterValue") %>'
runat="server" TabIndex="9"></asp:TextBox>
</td>
</tr>
<tr>
<td>
<asp:CheckBox Text="Default: " ID="CheckBoxDefault" runat="server" Checked='<%# (Container is GridEditFormInsertItem) ? true : Eval( "IsDefault") %>' />
</td>
<td>
<asp:TextBox ID="TextBoxDefaultValue" Width="240px" Text='<%# (Container is GridEditFormInsertItem) ? 0.0 : Eval( "defaultValue") %>'
runat="server" TabIndex="9"></asp:TextBox>
</td>
<td style="width: 140px; padding: 0 0 0 10px;">
ParameterValueID:
</td>
<td>
<asp:TextBox ID="TextBoxParameterValueID" Width="200px" Text='<%# (Container is GridEditFormInsertItem) ? 0.0 : Eval( "parameterValueID") %>'
runat="server" TabIndex="9"></asp:TextBox>
etc....
protected
void
parameterList_SelectedIndexChanged(
object
sender, EventArgs e)
{
DropDownList parametersList = (DropDownList)sender;
string
value = parametersList.SelectedValue;
GridEditableItem edititem = (GridEditableItem)parametersList.NamingContainer;
TextBox TextBoxID = (TextBox)edititem.FindControl(
"TextBoxID"
);
TextBoxID.Text = parametersList.SelectedValue;
TextBox TextBoxParameterValueID = (TextBox)edititem.FindControl(
"TextBoxParameterValueID"
);
TextBoxParameterValueID.Text = parametersList.SelectedValue;
TextBox TextBoxCodeName = (TextBox)edititem.FindControl(
"TextBoxCodeName"
);
TextBoxCodeName.Text = parametersList.SelectedValue;
TextBox parameterDescription = (TextBox)edititem.FindControl(
"parameterDescription"
);
parameterDescription.Text = parametersList.SelectedValue;
TextBox TextBoxDefaultValue = (TextBox)edititem.FindControl(
"TextBoxDefaultValue"
);
TextBoxDefaultValue.Text = parametersList.SelectedValue;
TextBox TextBoxMinValue = (TextBox)edititem.FindControl(
"TextBoxMinValue"
);
TextBoxMinValue.Text = parametersList.SelectedValue;
TextBox TextBoxMaxValue = (TextBox)edititem.FindControl(
"TextBoxMaxValue"
);
TextBoxMaxValue.Text = parametersList.SelectedValue;
}
Sorry I've been working on something else over past week. I'm still having trouble with this query.
Above is my asp code and c# - it populates each text box with what is displayed in the dropdown list i.e. if the dropdown list contained A-Z and I chose A then all textboxes would display A. Do I need to put the name of the field that I want to display inside of the brackets? If so, I've tried this and it brings up an error.
Do I need to change the query that I use to populate the dropdown list from the database?
Any help would be much appreciated.
Thanks
I reviewed the code, and it looks correct. If an exception is raised, or the logic does not execute properly, then you can open a formal support ticket, and send us a small working project for additional review and testing.
Best wishes,
Yavor
the Telerik team