Add ALL in Bound DropDownList - c#

I currently developing a webpage using C#. I have a DropdownList data bound with the data from my sql database. After the dropdownlist bind with my database, the item inside the dropdownlist are userA, userB, and userC. If i select any of the item inside the dropdownlist, the data of the particular user will show in the gridview.
So, what I am trying to do now is I want to add an ALL into the dropdownlist. When I click on the ALL, the data of each user will be shown in the gridview. How can I achieve that? Any advice? Thanks.
P/S: I dont want to add any extra button in order to show all the user data. I want to make it in the dropdownlist.
This is my code:
WebApp.aspx:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
This is what i have edited for a new Webpage. I do not call the data binding function in code behind.
Thanks for any helps.
This is the answer i am looking for:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username" AppendDataBoundItems="True">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
SelectCommand="SELECT [Username] FROM [Accounts]">
</asp:SqlDataSource>
Thanks everyone who is trying to help me.

You can achieve this:
Use this code after you bind the DropdownList with SQL data
ddlUsers.Items.Add(new ListItem("All", "all"));
ddlUsers.SelectedValue = "all";
Once this is done, you can make your select all user query based on this condition:
if(ddlUsers.SelectedValue == "all")
{
// your SQL query to select all users goes here.
}
In HTML Markup you can add this like:
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlDataSource1" DataTextField="Username"
DataValueField="Username">
<asp:ListItem Text="All" Value ="all" Selected="True"></asp:ListItem>
</asp:DropDownList>
You can remove Selected="True" if you don't want this to be selected by default.

You can do few things to achieve this.
1)ddlUsers.Items.Add(new ListItem("All", "all")); add this line where ever you are binding the drop-down list.
2) use SelectedIndexChanged event of drop down like...
protected void ddlUsers_SelectedIndexChanged(object sender, EventArgs e)
{
if(ddlUsers.SelectedValue == "all")
{
//Call your SQL query from here and bind the result set with your grid.
//if you need the id's of all the items in the drop down then write a loop and form a //string with , separated valued and pass it along.
}
}

Related

How to make a Bind in SqlDataSource - Dropdownlist

This is how I should be able to update a product on the page, it must grasp the category which I have chosen, so I will be free to save my choice every time.
I chose to do it via SqlDataSource dropdownlist.
I have written the code like this:
<asp:DropDownList ID="DropDownList1"
runat="server"
CssClass="form-control"
DataSourceID="SqlDataSource1"
DataTextField="navn"
DataValueField="Id"
SelectedValue='<%# Bind("kategori") %>'
></asp:DropDownList>
<%--datasource--%>
<asp:SqlDataSource runat="server" ID="SqlDataSource1" ConnectionString='<%$ ConnectionStrings:ConnectionString %>'
SelectCommand="SELECT kategori.id, kategori.navn, produkter.fk_kategori FROM kategori INNER JOIN produkter on kategori.id=produkter.fk_kategori ORDER BY [Id] DESC"></asp:SqlDataSource>
It appears this one error:
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:DropDownList ID="DropDownList1"
What should I do to the grasp it I've chosen category.

Get parameters for SelectParameters from another dropdown in c# .net

Does anyone how can I get the parameters for SelectParameters from another dropdown in c# .net?
I check online , and I follow the steps that been told, but it's doesn't seem working for me.
Code:
If I have the following code to retrieved the data for a dropdown:
<asp:DropDownList ID="DropDownVisaType" runat="server" DataSourceID="dsDropDownVisaType" CssClass="dsDropDownVisaType"
DataValueField="VisaTypeID" DataTextField="VisaType" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaType" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_select" SelectCommandType="StoredProcedure">
</asp:SqlDataSource>
After I select the dropdown, I want to use the VisaTypeID from the dropdown to retrieve data from another dropdown as below:
<asp:DropDownList ID="DropDownVisaTypeSpecific" runat="server" DataSourceID="dsDropDownVisaTypeSpecific" CssClass="dsDropDownVisaTypeSpecific"
DataValueField="VisaTypeSpecificID" DataTextField="VisaTypeSpecific" AppendDataBoundItems="true" >
<asp:ListItem></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="dsDropDownVisaTypeSpecific" runat="server" ConnectionString="<%$ ConnectionStrings:SmartStaffConnectionString %>"
SelectCommand="app_visa_type_specific_select" SelectCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Property="SelectedValue" ControlID="DropDownVisaType" Name="VisaTypeID" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Does anyone know what is my code going wrong and how can I make it work?
There is an event SelectedIndexChanged for A dropdown by using that you can get the value of selected Item.
Like below:
protected void DropDownVisaType_SelectedIndexChanged(object sender, EventArgs e)
{
Int32 value = Convert.ToInt32(DropDownVisaType.SelectedItem.Value);
DropDownVisaTypeSpecific.DataSource=Method_Name(Value);
DropDownVisaTypeSpecific.DataBind();
}

Dropdownlist in Detailsview, not listing the items

Hello Everyone
I have a detailsview, where there are 3 bound fields and a template field.
The template field has a DropDownList, which I have connected to an AccessDataSource.
But when I run, the dropdownlist has just the "System.Data.DataRowView" as it's items.
I wish to get the items from DB to be listed down in DropDownList
This is my code
asp:TemplateField HeaderText="State/Province" SortExpression="State/Province">
<EditItemTemplate>
<asp:DropDownList ID="ddlState" runat="server" DataSourceID="AccessDataSource1"
onselectedindexchanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
<asp:AccessDataSource ID="AccessDataSource1" runat="server"
DataFile="~/App_Data/db1.mdb"
SelectCommand="SELECT [State/Province_name] FROM [State/Province_List ]">
</asp:AccessDataSource>
</EditItemTemplate>
</asp:TemplateField>
Should I add "DataBinding" or "DataBound" event for DropDownList?? to make it perfect?
Help me regarding this issue
Thanks,
Arjun
Define the filed to use in the drop down list
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="State/Province_name"
DataValueField="State/Province_name"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>
If you have problems with this non-standard column name, try to use an alias
SELECT [State/Province_name] AS StateProv FROM [State/Province_List ]
(Is the space in [State/Province_List ] OK?)
Then use this one
<asp:DropDownList ID="ddlState" runat="server"
DataSourceID="AccessDataSource1"
DataTextField="StateProv"
DataValueField="StateProv"
OnSelectedIndexChanged="DropDownList1_SelectedIndexChanged">
</asp:DropDownList>

How to assign values to items in a databound ddl?

I'm running a dropdown list populated by column "CustName" from table "Customer", and each customer has a unique id I want to use to run the queries; obviously it's best to have the end user select by name rather than id, because it's friendlier that way. How do I assign values to items in this databound ddl? Preferably, I'd like to assign their CustId values from the table itself. I tried simply placing the CustId field into the DataValueField attribute, but no go. Ideas?
<asp:DropDownList ID="DropDownList1" runat="server"
DataSourceID="SqlCustNameSource" DataTextField="CustName"
DataValueField="CustName" AppendDataBoundItems="true">
<asp:ListItem Value="0" Text="Choose Customer" Selected="True"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlCustNameSource" runat="server"
ConnectionString="<%$ ConnectionStrings:MyConnectionString %>"
SelectCommand="SELECT [CustName] FROM [Customer]"></asp:SqlDataSource>
What exactly does "No Go" mean?
...DataValueField="CustId"...
...SelectCommand="SELECT [CustName], [CustId] FROM [Customer]"...

How to pass TextBox value to DynamicPopulateExtender query?

Hi I am trying to do DynamicPopulate on DropDownList2 when the value of textbox2 changes, how can I pass the value to TextBox2 to the sql query of DynamicPopulate.
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1"
onselectedindexchanged="DropDownList2_SelectedIndexChanged">
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MYDB %>"
SelectCommand="SELECT Time FROM Tour WHERE (Date = TextBox2 )">
</asp:SqlDataSource>
<asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender"
runat="server" Enabled="True" PopulateTriggerControlID=""
TargetControlID="DropDownList2">
</asp:DynamicPopulateExtender>
add this javascript code to your page
<script type="text/javascript">
function UpdateControl(value) {
var behavior = $find('dp1');
if (behavior) {
behavior.populate(value);
}
}
</script>
and use this modified code:
<asp:DynamicPopulateExtender ID="DropDownList2_DynamicPopulateExtender"
runat="server" Enabled="True" PopulateTriggerControlID=""
TargetControlID="DropDownList2" BehaviorID="dp1">
</asp:DynamicPopulateExtender>
in code behind of your vb or C# page make sure to add the attribute
DropDownList2.Attributes.Add("onChange", "UpdateControl3(this.value);")
This will trigger the WebMethod for your DynamicPopulateExtender and will set the contextKey to the value of your dropdown.
Hope this helps.
I think you may be confused about what a DynamicPopulateExtender is used for. It's meant to return HTML from a call to a webservice when you change the value of a control on the page. It works with straight HTML, you don't use it to update other server controls on your page. I think what you're looking for is the CascadingDropDown, see http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/CascadingDropDown/CascadingDropDown.aspx for more details.

Categories

Resources