I have 2 dropdown menus which are populated using tableAdapters, the list_model dropdown menu is fully dependent on the list_make dropdown menu as it determines which models to show depending on which make has been clicked on, here is the dropdown menus:
<asp:DropDownList ID="list_make" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="makeODS" DataTextField="make" DataValueField="ID" onselectedindexchanged="list_make_SelectedIndexChanged" SelectedValue='<%# Bind("makeID") %>'></asp:DropDownList>
<asp:ObjectDataSource ID="makeODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="getMakes" TypeName="list_dataTableAdapters.MakesTableAdapter"></asp:ObjectDataSource>
<asp:DropDownList ID="list_model" class="form-control padding" runat="server" AutoPostBack="True" DataSourceID="modelODS" DataTextField="model" DataValueField="ID" SelectedValue='<%# Bind("modelID") %>'>
</asp:DropDownList>
<asp:ObjectDataSource ID="modelODS" runat="server" OldValuesParameterFormatString="original_{0}" SelectMethod="GetModelsByMakeID" TypeName="list_dataTableAdapters.ModelsTableAdapter">
<SelectParameters>
<asp:ControlParameter ControlID="list_make" Name="make_id" PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
Note that these dropdown lists are in my EditItemTemplate and when the edit button is clicked the SelectedValue is correctly retrieved however when I select a new item in the list_make I get this error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control
Does anyone know why I am getting this error?
Also if it helps I have this code in the list_make_SelectedIndexChanged event:
protected void list_make_SelectedIndexChanged(object sender, EventArgs e)
{
var listView = listview_car.Items[0].FindControl("list_model") as DropDownList;
listView.DataBind();
}
Related
Why can't I use SelectedValue on my DropDownList !?
I tried SelectedValue="<% BindItem.current_contact.status_id %>" but the error says it is not a valid parameter. And Bind("status_id") does not work either: "The server tag is not correct." on my DropDownList.
<asp:SqlDataSource runat="server" ID="SdsStatus"
SelectCommand="SELECT SP.status_id, SP.sp_label, SP.sp_order FROM tr_status SP WHERE SP.groupe_id = #groupe_id ORDER BY SP.sp_order ; " ConnectionString="<%$ ConnectionStrings:DBLOGIPRO %>">
<SelectParameters>
<ctrl:ClassPropertyParameter Name="groupe_id" ClassName="User" PropertyPath="CurrentNego.groupe_id" ConvertEmptyStringToNull="true" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ListView runat="server" ID="LvContact"
RenderOuterTable="false"
SelectMethod="GetAccountWithContacts" ItemType="AccountWithContactsDTO"
UpdateMethod="UpdateAccountWithContacts">
<EditItemTemplate>
...
<asp:DropDownList runat="server" ID="DdlStatus" EnableViewState="true"
DataSourceID="SdsStatus"
DataTextField="sp_label" DataValueField="status_id"
DataTextFormatString="{0}" AppendDataBoundItems="true"
OnSelectedIndexChanged="DdlStatus_SelectedIndexChanged"
AutoPostBack="true">
<asp:ListItem Value="" Text="" Selected="True"></asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
</asp:ListView>
You can try set the SelectValue in ListView's ItemDataBound event. There you can handle controls' attribute values.
I am not sure if you can handle this kind of attribute with Bind() method directly in aspx.
I have a dropdown list (doc_rvw_sub_recip_list) that is populated with names from tbl_ad_users. tbl_ad_users contains a field titled "domain_user". I would like to populate an asp:hiddenfield (hdn_domain_user) with the "domain_user" based on the selected value in the doc_rvw_sub_recip_list dropdown list. For testing I used a dropdown list (domain_user_ddl) instead of a hidden field, and it works as needed. But I don't know how to get the value into a hidden field as opposed to using a dropdown list.
The following is doc_rvw_sub_recip_list:
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
The following is the sql data source for domain_user_ddl:
<asp:SqlDataSource ID="sdc_domain_user_ddl" runat="server" ConnectionString='<%$ ConnectionStrings:idrfConnectionString %>' SelectCommand="SELECT [domain_user] FROM [tbl_ad_users] WHERE ([email_address] = #email_address)">
<SelectParameters>
<asp:ControlParameter ControlID="doc_rvw_sub_recip_list" PropertyName="SelectedValue" Name="email_address" Type="String"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource>
The following is domain_user_ddl:
<asp:DropDownList ID="domain_user_ddl" runat="server" DataSourceID="sdc_domain_user_ddl" DataTextField="domain_user" DataValueField="domain_user" AutoPostBack="true"></asp:DropDownList>
How do I get this to work for hdn_domain_user just as it works for domain_user_ddl?
ASPX
Add a OnSelectedIndexChanged event to your dropdown...
<asp:DropDownList ID="doc_rvw_sub_recip_list" runat="server" DataSourceID="sdc_ad_user_list" DataTextField="name" DataValueField="email_address" AppendDataBoundItems="true" AutoPostBack="true" OnSelectedIndexChanged="doc_rvw_sub_recip_list_SelectedIndexChanged"><asp:ListItem Value="">Please Select</asp:ListItem></asp:DropDownList>
Add your hidden field...
<asp:HiddenField runat="server" ID="hdn_domain_user" />
Code Behind
Handle the SelectedIndexChanged event...
protected void doc_rvw_sub_recip_list_SelectedIndexChanged(Object sender, EventArgs e)
{
// Populate the hidden field if the dropdown has a selected value
if (doc_rvw_sub_recip_list.SelectedValue != null)
hdn_domain_user.Value = doc_rvw_sub_recip_list.SelectedValue;
}
I'm stuck into a problem using a DropDownList item into a form.
The main form is feed by an SQLDataSource ... all form widget are bound to a specific record field etc.
I changed one of the form widget (a TextBox) with a DropDownList to allow the user to only select between a number of values... I feed this control trough another SQLDataSource. While the form is in Insert mode there are no problems but when I switch into Edit mode I'm not able to set the DropDownList selected item with the value coming from the main record. Ho can I fix this?
Here's a sample of my .aspx code:
<asp:FormView ID="_frmData" runat="server" DataKeyNames="id" DataSourceID="_sdsTable1" DefaultMode="Insert">
<InsertItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</InsertItemTemplate>
<EditItemTemplate>
<asp:TextBox ID="_txtField0" runat="server" Text='<%#Bind("field0")%>'/>
<asp:DropDownList ID="_ddlField1" CssClass="input_medium" runat="server" DataSourceID="_sdsTable2" DataTextField="field_text" DataValueField="field_value" AppendDataBoundItems="true">
<asp:ListItem Value="" Text="None" Selected="True" />
</asp:DropDownList>
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="_sdsTable1" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field0, field1
FROM
table1
WHERE
id=#id;"
UpdateCommand=" UPDATE
table1
SET
`field0` = #field0
`field1` = #field1
WHERE
id = #id;""
InsertCommandType="StoredProcedure"
InsertCommand="create_table1_entry">
<InsertParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</InsertParameters>
<UpdateParameters>
<asp:ControlParameter Name="field1" ControlID="_frmData$_ddlField1" Type="String" PropertyName="SelectedItem.Text" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="_sdsTable2" runat="server" ConnectionString="<%$ ConnectionStrings:_db %>"
ProviderName="<%$ ConnectionStrings:_db.ProviderName %>"
SelectCommand=" SELECT
field_value, field_text
FROM
table2;" />
Try This
myDropDown.Items.FindByValue("Your Main Record Value").Selected=true;
OR
myDropDown.Items.FindByText("Your Main Record Text").Selected=true;
I CAN JUST use SelectedValue='<%#Bind("field1")%>' property in DDL to achieve the result, just as I was guessing. The only issue here is the why Intellisense is not suggesting SelectedValue as a propery for DropDownList ... thnx everyone.
I'm using gridview with edit/delete. When the user clicks on "Edit" I need to pass a value from one column (100mPLot) to a datasource that would populate a dropdownlist of another column (SubPlot). This is what I have:
<asp:BoundField DataField="100mPlot" HeaderText="100m plot"
SortExpression="100mPlot" ReadOnly="True" />
<asp:TemplateField HeaderText="SubPlot" SortExpression="SubPlot">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="dsSubPlotNames"
DataTextField="SiteName" DataValueField="SiteID"
SelectedValue='<%# Bind("SiteID") %>'
>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSubPlotNames" runat="server"
ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand='exec [339_PPM].usp_SubPlotNames_Select null, #100mPlotName;'
CancelSelectOnNullParameter="False">
<SelectParameters>
<asp:ControlParameter ControlID="GridView1" DefaultValue='<%# Eval("100mPlot") '
Name="100mPlotName" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Unfortunately, I get this error with the Eval("100mPlot"):
Databinding expressions are only supported on objects that have a DataBinding event. System.Web.UI.WebControls.ControlParameter does not have a DataBinding event.
How do I fix this? Thanks,
EDIT:
I moved it to code behind and handled it in RowDataBound after creating a hidden label tag containing the value of the previous column.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow && (e.Row.RowState & DataControlRowState.Edit) == DataControlRowState.Edit)
{
Label lbl100mPlot = (Label)e.Row.FindControl("lbl100mPlot");
SqlDataSource dsPlotNames = (SqlDataSource)e.Row.FindControl("dsSubPlotNames");
dsPlotNames.SelectParameters.Clear();
dsPlotNames.SelectParameters.Add("100mPlotSiteID", null);
dsPlotNames.SelectParameters.Add("100mPlotName", lbl100mPlot.Text);
}
}
My next problem is this error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Exception Details: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Source Error:
[No relevant source lines]
I'm not sure what's causing it.
I dont think you can do it this way. You can handle RowEditing event in code behind and manually adjust your SqlDataSource to display correct data. Also, move SqlDataSoruce out of your grid.
I have a gridview with column that have droplist when enter edit mode:
<asp:TemplateField HeaderText="genre" SortExpression="genre">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("genre") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
Now i want to take it to UpdateParameters of SqlDataSource:
<UpdateParameters>
<asp:ControlParameter ControlID="DropDownList2" Type="string" PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
But when i pressed he give me Error msg
Could not find control 'DropDownList2' in ControlParameter 'genre'.
Any idea why?
DropDownList2 is a nested control located under the Grid; therefore, your SqlDataSource control does not have visibility at all of the DropDownList2.
You could try assigning the value on the code behind by using the Updating event:
protected void SqlDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#genre"].Value = GetDropDownListValue();
}
Note: You will need to use FindControl("DropDownList2") in GetDropDownListValue()