I am new to ASP.NET and currently having problem with dropdownlists in the DetailsView.
Exception error: System.InvalidOperationException: Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
I have this code my code behind to refresh the list for the dropdownlists in DetailsView
protected void ddlVendor_SelectedIndexChanged
(object sender, EventArgs e)
{
DropDownList ddlVendorBB =
(DropDownList)DetailsView1.FindControl("VendorBUName");
if (ddlVendorBB != null)
{
Response.Write("SelectChanged");
ddlVendorBB.DataBind();
}
}
protected void SqlDataSourceProd_Selecting
(object sender, SqlDataSourceSelectingEventArgs e)
{
DropDownList ddlVendor =
(DropDownList)DetailsView1.FindControl("VendorName");
if (ddlVendor != null)
{
e.Command.Parameters["#VendorID"].Value = ddlVendor.SelectedValue;
}
}
These two dropdownlists in the DetailsView
<EditItemTemplate>
<asp:DropDownList id="VendorName"
datasourceid="VendorSqlDataSource"
AutoPostBack="true"
datatextfield="VendorName"
DataValueField="VendorID"
SelectedValue='<%# Bind("VendorID") %>'
runat="server"
OnSelectedIndexChanged="ddlCategory_SelectedIndexChanged" />
<asp:SqlDataSource ID="VendorSqlDataSource"
ConnectionString="<%$Connectionstrings:ConnectionString%>"
SelectCommand="SELECT VendorID, VendorName from MDF_Vendor"
runat="server">
</asp:SqlDataSource>
</EditItemTemplate>
<EditItemTemplate>
<asp:DropDownList id="VendorBUName"
datasourceid="VendorBUSqlDataSource"
datatextfield="VendorBUName"
DataValueField="VendorBUID"
SelectedValue='<%# Bind("VendorBUID") %>'
runat="server"/>
<asp:SqlDataSource ID="VendorBUSqlDataSource"
runat="server"
ConnectionString="<%$Connectionstrings:ConnectionString%>"
selectcommand="SELECT VendorBUID, VendorBUName
from MDF_VendorBU
Where VendorID = #VendorID"
OnSelecting="SqlDataSourceProd_Selecting">
<SelectParameters>
<asp:Parameter Name="VendorID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
Problem is:
If I leave SelectedValue= there, the dropdownlists in Edit mode seletected the correct value in the items listed when I first click Edit, but when I select a new VendorName, it errors "Databining method such as Eval(), Xpath(), and Bind()... ".
Now, if I removed the Selectedvalued for the dropdownlists, it will work for refreshing the VendorBUName when select a new VendorName, but NOT not selected the default VendorID when I click "Edit". It just list the VendorName list without selected the current VendorID one.
Can someone please let me know what wrong in my codes? Thanks!
As the error states, you cannot use Bind where you are trying to use it. You should be able to use the DataBinder though
SelectedValue='<%# DataBinder.Eval (Container.DataItem, "VendorBUID") %>'
Edit: Since binding a value to the SelectedValue with DataBinder didn't work, you can try to set the value when binding data. Provided dataSource is some instance of a class that has a property called VendorBUID, something similar to this might work in
public override void OnLoad(EventArgs e) {
VendorBUName.DataBinding += dataBindDropDown;
}
private void dataBindDropDown(object sender, EventArgs e) {
VendorBUName.SelectedValue = dataSource.VendorBUID;
}
Related
I want to do some actions in my database when user change dropdown's selectedIndex.Now I have the following.
<td class="shop-item-qty">
<asp:DropDownList ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" runat="server"/>
<asp:HiddenField ID="ItemId" runat="server" Value='<%#Eval("GiftVoucher.ID") %>'/>
</td>
All I want is to get my hidden fields value in changeCount method. The problem is that I can't directly get hidden fields value, because this code is in Repeater element. How can I achieve that functionality?
protected void qtyDropDownList_SelectedIndexChanged(object sender, EventArgs e)
{
DropDownList control = (DropDownList)sender;
RepeaterItem rpItem = control.NamingContainer as RepeaterItem;
if (rpItem != null)
{
HiddenField hiddenField = ((HiddenField)rpItem.FindControl("ItemId"));
}
}
You can bind GiftVoucher.ID value to DropDown's custom attribute and skip HiddenField:
<asp:DropDownList runat="server" ID="qtyDropDownList" OnSelectedIndexChanged="changeCount" AutoPostBack="true" data-itemId='<%# Eval("ID") %>' />
protected void changeCount(object sender, EventArgs e)
{
var id = ((DropDownList)sender).Attributes["data-itemId"];
}
I have a DataList and inside it I have a DropDownList:
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
How can I get selectedindexchanged event of DropDownList on the server side? I tried this:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but it is not working.
You have defined the server side method:
public void ddlitem_selectedindexchanged (object sender, EventArgs e)
{
}
but you have not told client side that there is an event for you, so in html code tell it like:
onselectedindexchanged="ddlitem_selectedindexchanged"
and also set AutoPostBack property to true.
From the SelectedIndexChanged event the easiest is to cast the sender to the DropDownList
var ddl = (DropDownList)sender;
The sender is always the control that is the source of the event.
For the sake of completeness, from ItemDataBound of the DataList:
protected void dlconfigureItem_ItemDataBound(object sender, DataListItemEventArgs e)
{
DropDownList ddlitem = e.Item.FindControl("ddlitem") as DropDownList;
if (ddlitem != null)
{
// ...
}
}
Edit: Have you forgotten to register the event?
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
Note that you should not bind your DataList to it's DataSource on postbacks, otherwise events are not triggered. So check for the IsPostBack property of the page.
For example in page_load:
if(!IsPostBack)BindDataList();
Register the event and set AutoPostBack="true"
<asp:DropDownList CssClass="config-select"
ID="ddlitem"
AutoPostBack="true"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
runat="server">
</asp:DropDownList>
event (on selected index change you can get the selected value)
protected void ddlCategory_SelectedIndexChanged(object sender, EventArgs e)
{
var ddlList = (DropDownList)sender;
string selectedValue = ((DropDownList)ddlList.NamingContainer.FindControl("ddlitem")).SelectedValue;
}
Not sure if you can't get the selected item on the server or you can't find the way to handle the event. In case your problem is with the event handling, try this
<asp:DataList ID="dlconfigureItem" runat="server">
<ItemTemplate>
<asp:DropDownList CssClass="config-select" ID="ddlitem"
OnSelectedIndexChanged="ddlitem_selectedindexchanged"
AutoPostBack="true" runat="server"></asp:DropDownList>
</ItemTemplate>
</asp:DataList>
I have a dropdownlist that is populated from an SQL select statement. The select statement filters for items where the 'bit' is set to false and the items, although still in the database are hidden.
My problem is; when an item is out of stock or hidden (bit = false) the user may still have items that are now hidden so it throws an error. How and where can I catch this, show the original item or set the value to default?
protected void GradeDropDownList_DataBinding (object sender, EventArgs e)
{
var ddl = (DropDownList)(sender);
var a = ((Label)MyDetailsView.FindControl("GradeLabelEdit")).Text;
a = a.Trim();
if (a != "") { ddl.SelectedValue = a; }
}
The select statement;
<asp:SqlDataSource ID="getGrade" runat="server" ConnectionString="<%$ ConnectionStrings:CasesTimeConnection %>"
SelectCommand="SELECT [gradeID], [gradeText] FROM [user_grades] WHERE ([visibleState] = #visibleState)">
<SelectParameters>
<asp:Parameter DefaultValue="True" Name="visibleState" Type="Boolean" />
</SelectParameters>
</asp:SqlDataSource>
In page;
<EditItemTemplate>
<asp:DropDownList ID="GradeDropDownList" runat="server" DataSourceID="getGrade" DataTextField="gradeText" DataValueField="gradeID" OnDataBinding="GradeDropDownList_DataBinding" OnSelectedIndexChanged="GradeDropDownList_SelectedIndexChanged">
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="gradeLabel" runat="server" Text='<%# Bind("gradeText") %>'></asp:Label>
</ItemTemplate>
Retrieve LastModifiedDate while retrieving the records and while trying to update the record check the existing lastModifiedDate value with the one you retrieved. If they are different then throw an alert message / display new quantity.
Thanks
Shashi
I am having problem in EditItemTemplate of FormView.
When I use such code in InsertItemTemplate everything works:
<asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server"
SelectedValue='<%# Bind("Lic_PosiadaczLicencjiID") %>' />
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server"
TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod"
ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci">
</asp:CascadingDropDown>
But when I use exactly the same code in EditItemTemplate I am getting an error that SelectedValue is wrong cause it doesn't exists on the list of elements.
I think that the problem is that DropDownList is checked for the values before it is populated by the service. When I run debugger the error occured before breakpoint in the service method.
How to solve this problem?
<rant>I've found the CCD very clunky and full of poorly-documented workarounds</rant> but here is how you do something as simple as selecting a value when filling the ddl. Note that the selected value is not set on the DDL and that it is being passed to the web service where the selecting is done.
<asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
<asp:FormView ID="fv1" runat="server" DataSourceID="yourDataSource">
<EditItemTemplate>
<asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server" />
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server"
TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod"
ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci"
UseContextKey="true" ContextKey='<%# Bind("Lic_PosiadaczLicencjiID") %>'>
</asp:CascadingDropDown>
</EditItemTemplate>
</asp:FormView>
<asp:sqldatasource id="yourDataSource"
selectcommand="select Lic_PosiadaczLicencjiID FROM yourdatabase"
UpdateCommand="Update yourdatabase set Lic_PosiadaczLicencjiID = #newvalue WHERE Lic_PosiadaczLicencjiID = #Lic_PosiadaczLicencjiID"
connectionstring="<%$ ConnectionStrings:yourConnectionString %>"
runat="server"
onupdating="yourDataSource_Updating">
<UpdateParameters>
<asp:Parameter Name="newvalue" DbType="String" />
</UpdateParameters>
</asp:sqldatasource>
code behind:
protected void yourDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#newvalue"].Value = ((DropDownList)fv1.FindControl("Lic_PosiadaczLicencjiIDDropDownList")).SelectedValue;
}
and in your web service where you are getting your data from you need to add the context key to the signature exactly as shown as it is case sensitive. You then check your returned values for the selected value and set selected = true. If you want selected value instead of selected text then check for x.value instead of x.name.
[WebMethod]
public CascadingDropDownNameValue[] GetKontrahenci(string knownCategoryValues, string category, string contextKey)
{
CascadingDropDownNameValue[] results = getdata();
CascadingDropDownNameValue selectedVal = (from x in results where x.name == contextKey select x).FirstOrDefault();
if (selectedVal != null)
selectedVal.isDefaultValue = true;
return results;
}
Hope this helps!
I am having problem in EditItemTemplate of FormView.
When I use such code in InsertItemTemplate everything works:
<asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server"
SelectedValue='<%# Bind("Lic_PosiadaczLicencjiID") %>' />
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server"
TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod"
ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci">
</asp:CascadingDropDown>
But when I use exactly the same code in EditItemTemplate I am getting an error that SelectedValue is wrong cause it doesn't exists on the list of elements.
I think that the problem is that DropDownList is checked for the values before it is populated by the service. When I run debugger the error occured before breakpoint in the service method.
How to solve this problem?
<rant>I've found the CCD very clunky and full of poorly-documented workarounds</rant> but here is how you do something as simple as selecting a value when filling the ddl. Note that the selected value is not set on the DDL and that it is being passed to the web service where the selecting is done.
<asp:ScriptManager ID="sm1" runat="server"></asp:ScriptManager>
<asp:FormView ID="fv1" runat="server" DataSourceID="yourDataSource">
<EditItemTemplate>
<asp:DropDownList ID="Lic_PosiadaczLicencjiIDDropDownList" runat="server" />
<asp:CascadingDropDown ID="CascadingDropDown1" runat="server"
TargetControlID="Lic_PosiadaczLicencjiIDDropDownList" Category="Knt_Kod"
ServicePath="~/ManagerLicencjiService.asmx" ServiceMethod="GetKontrahenci"
UseContextKey="true" ContextKey='<%# Bind("Lic_PosiadaczLicencjiID") %>'>
</asp:CascadingDropDown>
</EditItemTemplate>
</asp:FormView>
<asp:sqldatasource id="yourDataSource"
selectcommand="select Lic_PosiadaczLicencjiID FROM yourdatabase"
UpdateCommand="Update yourdatabase set Lic_PosiadaczLicencjiID = #newvalue WHERE Lic_PosiadaczLicencjiID = #Lic_PosiadaczLicencjiID"
connectionstring="<%$ ConnectionStrings:yourConnectionString %>"
runat="server"
onupdating="yourDataSource_Updating">
<UpdateParameters>
<asp:Parameter Name="newvalue" DbType="String" />
</UpdateParameters>
</asp:sqldatasource>
code behind:
protected void yourDataSource_Updating(object sender, SqlDataSourceCommandEventArgs e)
{
e.Command.Parameters["#newvalue"].Value = ((DropDownList)fv1.FindControl("Lic_PosiadaczLicencjiIDDropDownList")).SelectedValue;
}
and in your web service where you are getting your data from you need to add the context key to the signature exactly as shown as it is case sensitive. You then check your returned values for the selected value and set selected = true. If you want selected value instead of selected text then check for x.value instead of x.name.
[WebMethod]
public CascadingDropDownNameValue[] GetKontrahenci(string knownCategoryValues, string category, string contextKey)
{
CascadingDropDownNameValue[] results = getdata();
CascadingDropDownNameValue selectedVal = (from x in results where x.name == contextKey select x).FirstOrDefault();
if (selectedVal != null)
selectedVal.isDefaultValue = true;
return results;
}
Hope this helps!