How to make a Bind in SqlDataSource - Dropdownlist - c#

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.

Related

One button for CheckBox items in ItemTemplate

How can i put only one button for ChceckBox?
here is what i get:
enter image here
As you can see i have 10 button for each checkbox value but i need group them by "week" "Day" and in this case put only one for each of them. That "day weak" label come from sql and is dynamic value.
Here is code:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:PS_TestConnectionString %>" SelectCommand="SELECT * FROM [Prize] ">
<ItemTemplate>
<asp:CheckBox ID="CheckBox1" runat="server" Text='<%# Eval("Prize") %>' OnCheckedChanged="CheckBox1_CheckedChanged1" AutoPostBack="True" />
<asp:Label ID="Label1" runat="server" Text='<%# Eval("Name") %>' /><br /> <asp:Button ID="Button1" runat="server" Text="Receive the prize" Visible="True" CommandName="foo" /></ItemTemplate>
I need this witch CheckBox in itemtemplate becouse i have to put image in
I solved this problem by adding a new GridView to ItemTemplate

asp:ListView SelectedIndexChanging not firing

I've searched in google and bing and seen all posts and even in stackoverflows search but couldn't find the solution.
To be sure about the error I've created a new project it has 1 page which has:
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label><br/>
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="SqlDataSource" SelectCommand="Select * FROM Users" ConnectionString="<%$ConnectionStrings:DefaultConnection %>"></asp:SqlDataSource>
Output:
Because I've 2 users with name A and B.
Now the problem is that events OnSelectedIndexChanged and OnSelectedIndexChanging are not firing when I click on A or B. How to get the selected index where user is clicking?
Complete Project if anyone wants to look, actually there is no need to: https://app.box.com/s/bhs682q6fqai784kao0zv1tcxgxecr4s
ListView.SelectedIndexChanging Event occurs when an item's Select button is clicked, but before the ListView control handles the select operation.
(A Select button is a button control whose CommandName property is set to "Select".)
Change your code to
<asp:ListView runat="server" ID="ListView" DataSourceID="SqlDataSource" OnSelectedIndexChanged="ListView_OnSelectedIndexChanged" OnSelectedIndexChanging="ListView_OnSelectedIndexChanging" AutoPostBack="true">
<ItemTemplate>
<asp:Label runat="server" ID="Label" Text='<%#Eval("UserName") %>' ></asp:Label>
<asp:LinkButton runat="server" ID="ButtonSelect" CommandName="Select" Text="SelectButton"/><br/>
</ItemTemplate>
</asp:ListView>

Add ALL in Bound DropDownList

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.
}
}

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 implement nested DropDownLists insid a GridView in asp.NET?

I have two nested DropDownLists (the value selected in the 1st ddl will limit the content of the 2nd ddl).
The example attached here runs fine outside of a GridView. As soon as I try to insert this into a GridView, I get an error because I cannot identify the ControlID to use in the ControlParameter.
The code outside of the GridView looks like this:
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Description" DataValueField="ID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
And the DataSources look like this:
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [Items]">
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:MyDataBase %>"
ProviderName="<%$ ConnectionStrings:MyDataBase.ProviderName %>"
SelectCommand="SELECT [ID], [Description] FROM [SubItems] WHERE [itemId]=:v_ItemId">
<SelectParameters>
<asp:ControlParameter Name="v_ItemId" ControlID="ACTION1_DROPDOWNLIST" PropertyName="SelectedValue" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
Thanks!
I have tried something similar and it works fine. I can't tell from your post whether the data sources are together with the dropdowns inside the ItemTemplate of the GridView. At least the second one should be inside the ItemTemplate so that you will have a data source for each line in the grid.
I have posted my page below. It's using different tables, but it's the same idea.
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" DataSourceID="SqlDataSource1">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST"
AutoPostBack="true" ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource1" DataTextField="Surname" DataValueField="FamilyID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:DropDownList ID="ACTION1_DROPDOWNLIST2"
ToolTip="Dropdown List" runat="server" CssClass="Select"
DataSourceID="SqlDataSource2" DataTextField="Name" DataValueField="PersonID" >
<asp:ListItem Value=""></asp:ListItem>
</asp:DropDownList>
<asp:Panel ID="HiddenFields" runat="server">
<asp:TextBox ID="DRAFT" runat="server" Visible="false"></asp:TextBox>
<asp:SqlDataSource ID="SqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Person] WHERE ([FamilyId] = #FamilyId)">
<SelectParameters>
<asp:ControlParameter Name="FamilyId" ControlID="ACTION1_DROPDOWNLIST"
PropertyName="SelectedValue" Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
</asp:Panel>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinqDataSource ID="LinqDataSource1" runat="server">
</asp:LinqDataSource>
</div>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:tunedinConnectionString %>"
ProviderName="<%$ ConnectionStrings:TunedInConnectionString2.ProviderName %>"
SelectCommand="SELECT * FROM [Family]">
</asp:SqlDataSource>
</form>
The actual id's are build based on where they are, so you will see something like $GridView1 inserted in the id's.
To solve this you can set ClientIDMode="Static" on your page properties (first line of your .aspx file).
Or use "javascript:var a = document.getElementById('" + ACTION1_DROPDOWNLIST.ClientID + "');

Categories

Resources