I have a DropDownList with values that I get from a SQL database. Depending on the selection made from the DropDownList, I populate a GridView, also from a SQL database.
When I set AutoPostBack=true on the DropDownList I get a 'Input string not in correct format' error.
My DropDownList:
<html>
<asp:DropDownList ID="ddlClient" runat="server" DataSourceID="dsClientList" AppendDataBoundItems="True" DataTextField="Name" DataValueField="Name" TabIndex="0" Font-Names="Verdana" Font-Size="11px" ForeColor="#2D2D2D" AutoPostBack="true">
<asp:ListItem Value="">--- Select ---</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="dsClientList" runat="server" SelectMethod="GetList" TypeName="Class.Client">
</asp:ObjectDataSource>
</html>
In my ObjectDataSource for my GridView I use the following SelectParameters:
<SelectParameters>
<asp:ControlParameter ControlID="ddlClient" Name="pClientID" PropertyName="SelectedValue"
Type="Int32" />
</SelectParameters>
Any help?
Thanks
It is probably trying to convert the value of this list item to an int
<asp:ListItem Value="">--- Select ---</asp:ListItem>
An empty string cannot be converted to an integer, try using -1 or 0.
You have to set numeric column for DataValueField="Name". May be an ID or Number.
<asp:DropDownList ID="ddlClient" runat="server" DataSourceID="dsClientList"
AppendDataBoundItems="True"
DataTextField="Name"
DataValueField="pCientID"
.....
You can try by setting default value for --- Select ---, if you set blank "" which can't parse to Int32, you might use below code block:
<html>
<asp:DropDownList ID="ddlClient" runat="server" DataSourceID="dsClientList" AppendDataBoundItems="True" DataTextField="Name" DataValueField="yourClientIDField" TabIndex="0" Font-Names="Verdana" Font-Size="11px" ForeColor="#2D2D2D" AutoPostBack="true">
<asp:ListItem Value="0">--- Select ---</asp:ListItem>
</asp:DropDownList>
<asp:ObjectDataSource ID="dsClientList" runat="server" SelectMethod="GetList" TypeName="Class.Client">
</asp:ObjectDataSource>
</html>
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'm testing a simple thing where I have a database of video cards and motherboards. The first DropDown List only pulls unique Brands
<asp:DropDownList ID="ddlfirst" runat="server" DataSourceID="SqlDataSource1" DataTextField="Brand" DataValueField="Brand" OnSelectedIndexChanged="ddlfirst_SelectedIndexChanged" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand] FROM [Computer_Motherboards]"></asp:SqlDataSource>
And the second dropdown list is populated by the first one. The Model is the display field and the Power requirement is the value.
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand], [Model], [PowerDraw] FROM [Computer_Motherboards] where Brand = #SessionVar">
<SelectParameters>
<asp:SessionParameter Name="SessionVar" SessionField="Mobo_Brand_Selection" ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
The session variable is set when the index of the first ddl is changed:
protected void ddlfirst_SelectedIndexChanged(object sender, EventArgs e)
{
//sets this session variable to whatever the selected brand is
Session["Mobo_Brand_Selection"] = ddlfirst.SelectedValue;
}
The problem is that when do the same as above, except for my motherboard information, the index of the Model/Power Requirement changes on any sort of click event or postback. So if I pick EVGA and Model 5, and then go to my Motherboard ddls and pick ASUS and Model 4, the EVGA Model 5 selection will go to index 1 or something. I've tried playing around with various if (!postback) statements but I just cant figure it out.
Place the second set of drop downs for the video card and model inside an update panel and it will work:
<asp:DropDownList ID="ddlfirst" runat="server" DataSourceID="SqlDataSource1" DataTextField="Brand" DataValueField="Brand" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand] FROM [Computer_Motherboards]"></asp:SqlDataSource>
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2" DataTextField="Model" DataValueField="PowerDraw">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:HardwareConnectionString %>" SelectCommand="SELECT DISTINCT [Brand], [Model], [PowerDraw] FROM [Computer_Motherboards] where Brand = #Brand">
<SelectParameters>
<asp:ControlParameter ControlID="ddlfirst" PropertyName="SelectedValue" Name="Brand" />
</SelectParameters>
</asp:SqlDataSource>
<asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<asp:DropDownList runat="server" ID="videoCardBrand" AutoPostBack="true">
<asp:ListItem Text="Video card one" Value="1" />
<asp:ListItem Text="Video card two" Value="2" />
<asp:ListItem Text="Video card 3" Value="3" />
</asp:DropDownList>
<asp:DropDownList runat="server" ID="videoCardModel" AutoPostBack="true">
<asp:ListItem Text="Video card model1" Value="1" />
<asp:ListItem Text="Video card model2" Value="2" />
<asp:ListItem Text="Video card model3" Value="3" />
</asp:DropDownList>
</ContentTemplate>
</asp:UpdatePanel>
I am trying to set the default text into the dropdown list that should appear on top
And i have bind the dropdown to sql table graphically
here is the code
<asp:DropDownList ID="countryDropDownList" runat="server" DataSourceID="countrySqlDataSource" DataTextField="CountryName" DataValueField="CountryId" Height="25px" Width="187px" AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource ID="countrySqlDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:123FlashChat %>" SelectCommand="SELECT * FROM [tblCountry]"></asp:SqlDataSource>
Try to change your SelectCommand to:
Select -1 as Value, '--Select--' as Text
union
SELECT * FROM [tblCountry]
or add it as ListItem
<asp:DropDownList ID="countryDropDownList" runat="server" DataSourceID="countrySqlDataSource" DataTextField="CountryName" DataValueField="CountryId" Height="25px" Width="187px" AutoPostBack="True">
<asp:ListItem Text="--Select--" Value ="-1"></asp:ListItem>
</asp:DropDownList>
you will need to use Jquery:
try something like this inside of a script tag on your page:
$("#<%=countryDropDownList.ClientID%>").val("thevalue");
where "thevalue" is the value of the item you are trying to set the selected index to
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 have an editable gridview with many columns. Two of which are 100mPlot and SubPlot. SubPlot's are not unique (1A, 1B, 1C, and 1D) but 100mPlot + SubPlot make a unique set of values.
When a user clicks "Edit", they will see a dropdown list in the SubPlot column. This list needs to be based on what the value of 100mPlot is. So in order to display the correct values for SubPlot, I need to pass in a value from 100mPlot column as a parameter to the sqldatasource that would be bound to the SubPlot dropdownlist. How would I go about this?
<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="SiteID" DataValueField="SiteID"
SelectedValue='<%# Bind("SiteID") %>'
AppendDataBoundItems="True">
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsSubPlotNames" runat="server"
ConnectionString="<%$ ConnectionStrings:WERCMTX %>" SelectCommand="exec [339_PPM].usp_SubPlotNames_Select #100mPlotSiteID;">
<SelectParameters>
<asp:Parameter Name="100mPlotSiteID" />
</SelectParameters>
</asp:SqlDataSource>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("SubPlot")%>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
As you can see, the parameter name "100mPlotSiteID" needs to be taken from 100mPlot column. I'm not sure how else to describe this anymore clearly, let me know if you have any questions. Thanks for your help.
EDIT with newly revised code. So close now!
<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.
You need to put a label and set the text property to '<%# Eval("100mPlot") %>' and use a control paremeter in dthe datasource.
If the label's id is "label1" (must be inside edit item template) then use this
<asp:ControlParameter ControlID="label1" PropertyName="Text" Name="100mPlotSiteID" />