I have this asp in a formview insert template.
<asp:FormView ID="FormViewReport" runat="server">
<InsertItemTemplate>
GAME_ID:
<asp:DropDownList ID="DropDownListReportIns" runat="server" AutoPostBack="true"
DataSourceID="SqlDataSourceGetGame"
DataTextField="GAME" DataValueField="ID" SelectedValue='<%# Bind("GAME_ID") %>' AppendDataBoundItems="True">
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" />
</asp:DropDownList>
HOME_PLAYER:
<asp:DropDownList ID="DropDownListRepo" runat="server"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" SelectedValue='<%# Bind("HOME_PLAYER_ID") %>' >
</asp:DropDownList>
The sql datasource of the second dropdownlist:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="FormViewReport" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
In the second Dropdownlist the query needs the GAME_ID parameter and this is the DatavalueField of the first dropdownlist.How I can get the selected value from the first dropdownlist and give it to the second dropdownlist?
Put your SqlDatasource control inside the InsertItemTemplate of your FormView (with the DropDownLists), and then change it to this:
<asp:SqlDataSource ID="SqlDataSourceGetPlayers" runat="server" ConnectionString='<%$ ConnectionStrings:BasketballConnectionString1 %>' SelectCommand="SELECT HP.SURNAME blah blah blah WHERE (GAMES.ID = #GAME_ID)">
<SelectParameters>
<asp:ControlParameter ControlID="DropDownListReportIns" PropertyName="SelectedValue" Name="GAME_ID"></asp:ControlParameter>
</SelectParameters>
</asp:SqlDataSource
That way your ControlParameter is referencing the selected value of the 1st drop down.
Finally I did it using asp for the second dropdownlist:
<asp:DropDownList ID="DropDownListRepIns" runat="server" AppendDataBoundItems="True" EnableViewState="false"
DataSourceID="SqlDataSourceGetPlayers"
DataTextField="HOME_PLAYER_S" DataValueField="HP_ID" >
<asp:ListItem Text="ΠΑΡΑΚΑΛΩ ΕΠΙΛΕΞΤΕ ΑΓΩΝΑ" Value="0" />
</asp:DropDownList><br />
and a c# code for event item inserting that binds manually:
protected void FormViewReport_ItemInserting(object sender, FormViewInsertEventArgs e)
{
e.Values["HOME_PLAYER_ID"] = ((DropDownList)((FormView)sender).FindControl("DropDownListRepIns")).SelectedValue;}
Also the solution this solution works without needing code behind:
SelectedValue='<%# DataBinder.Eval (Container.DataItem, "HOME_PLAYER_ID") %>'
You have to use this in the second dropdownlist in asp.
Related
related to this post Fill ComboBox Dynamically in asp.net.. help required I follow the code from that post, but when I tried to binding data, I've got error
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
<asp:TemplateField HeaderText="Step" SortExpression="ID"><InsertItemTemplate>
: <asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource3" AutoPostBack="true"
DataTextField="Name" DataValueField="code" SelectedValue='<%# Bind("ID") %>' AppendDataBoundItems="true"
Width="300px" Height="25px">
<asp:ListItem Value=" " Text="---Select---"></asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="KKL Step" SortExpression="ID">
<InsertItemTemplate>
: <asp:DropDownList ID="DropDownList6s" runat="server" DataSourceID="SqlDataSource5"
DataTextField="KKL_Name" DataValueField="ID" AppendDataBoundItems="true"
SelectedValue='<%# Bind("ID") %>' Width="300px" Height="25px">
<asp:ListItem Value="" Text="---Select---"></asp:ListItem>
</asp:DropDownList>
<asp:SqlDataSource ID="SqlDataSource5" runat="server"
ConnectionString="<%$ ConnectionStrings:Koneksi %>"
SelectCommand="SELECT * FROM [TBL_KKL] WHERE (code=#code)">
<SelectParameters>
<asp:controlParameter controlID="DropDownList2"
propertyname="SelectedValue" Name="code" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
</InsertItemTemplate>
</asp:TemplateField>
Its better to bind values to dropdown lists using C# code. Its ease and clear.
So in the RowDataBound event of the gridview you can write a code similar to this according to your syntax.
protected void dtgItemDetails_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
DataTable dtDetails = GetDetailsFromDataBase();
DropDownList DropDownList2= (DropDownList)e.Row.FindControl("DropDownList2");
DropDownList2.DataTextField = "Name";
DropDownList2.DataValueField = "ID";
DropDownList2.DataSource = dtDetails ;
DropDownList2.DataBind();
}
}
This is a sample code and you should change it according to yours.
Instead of DB calls inside the RowDataBound method, you can have Sessions or ViewStates with already filled with your data.
I have a Repeater with a ListView and its Datapager inside.
The information is populated correctly but, the datapager does not refresh the listview when clicking the page numer.
How do I refresh he list view based on datapager selection
<asp:Repeater runat="server" ID="Rptr" DataSourceID="Categories" >
<asp:HiddenField runat="server" id="Hidden" Value='<%# Eval("Category") %.' />
<ItemTemplate>
<asp:DataPager runat="server" ID="DtPgr_Top" PagedControlID="List" PageSize="40">
...
</asp:DataPager>
<asp:ListView runat="server" ID="List" DataSourceID="Items" >
<ItemTemplate>
...
</ItemTemplate>
</asp:ListView>
<asp:SqlDataSource runat="server" ID="Items" ConnectionString="<%$ ConnectionStrings:SAP_B1 %>"
SelectCommand=" SELECT Item FROM Items WHERE Category = #Category " >
<SelectParameters>
<asp:ControlParameter ControlID="Hidden" Name="Category" PropertyName="Value" />
</SelectParameters>
</asp:SqlDataSource>
</ItemTemplate>
" SelectCommand=" SELECT Category FROM Categories " />
enter code here
Solution 1 /
You have to put the listview outside the repeater.
Solution 2 /
Do not determine the datasource of repeater inline. Populate it from codebehind only once and only on load with - if not ispostback - directive.
I have a main 'Products' table and a 'Products_Recommended' table. I want the users to be able to select several products from a GridView using checkboxes and then insert those Product IDs (prodid) into the Products_Recommended table in such a way that a main Product ID is entered (coming from query string) and potentially several recommended ProdIDs get entered. So far so good.
But I need to be able to show the checkboxes to be checked if there were already prodids in the Products_Recommended table previously.
The code below show a 'sqldatasource1' which gets the data from the Products_Recommended table based on query string. I just don't know how the checkboxes can get checked because the GridView has a different sqldatasource binding it.
Thanks!
Meengla
<form id="form1" runat="server">
<asp:GridView ID="Products" runat="server" AutoGenerateColumns="False" DataKeyNames="prodid"
DataSourceID="alldata" EnableModelValidation="True">
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="ProductSelector" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="itemnumber" HeaderText="Item Number" SortExpression="itemnumber" />
<asp:BoundField DataField="itemtitle" HeaderText="itemtitle" SortExpression="itemtitle" />
</Columns>
</asp:GridView>
<p>
<asp:Button ID="SelectedProducts" runat="server" Text="Recommend" OnClick="SelectedProducts_Click" />
</p>
<p>
<asp:Label ID="lblProdSelected" runat="server" EnableViewState="False" Visible="False"></asp:Label>
</p>
<asp:SqlDataSource ID="alldata" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM Products">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:dbconnection %>"
SelectCommand="SELECT * FROM dbo.products_recommended WHERE prodid = #itemid)">
<SelectParameters>
<asp:QueryStringParameter DefaultValue="14" Name="itemid" QueryStringField="itemid"
Type="Int32" />
</SelectParameters>
</asp:SqlDataSource>
Handle the RowDataBound event, and in the method find the checkbox and set its Checked value.
<asp:GridView ID="Products" OnRowDataBound="GridViewRowEventHandler">
void CustomersGridView_RowDataBound(Object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
var ProductSelector = e.Row.FindControl("ProductSelector") as CheckBox;
ProductSelector.Checked = true;
}
}
You can use the Select method of DataSource to retrieve the data you need
Ok guys, so I read this:
How to set SelectedValue of DropDownList in GridView EditTemplate
and I'm having the same issue. However, I don't want to bind the selected value to the displayed text, but instead the values. They are different attributes selected from a SQLDataSource. Here is my DDL code with its SQLDataSource:
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List"
DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID"
SelectedValue='<%# Bind("PlantMaterial_FoodID") %>' >
</asp:DropDownList>
<asp:SqlDataSource ID="Plant_Carton_Food_List" runat="server"
ConnectionString="<%$ ConnectionStrings:OMSConnectionString %>"
SelectCommand="SELECT P.PlantMaterial_FoodID,
M.Material_SAP_Value + ' - ' + MD.SAP_Long_Description AS Food_Description
FROM Plant_Carton_Food AS P
LEFT OUTER JOIN Material_Descriptions AS MD
ON P.PlantMaterial_FoodID = MD.Material_ID
LEFT OUTER JOIN Materials AS M
ON P.PlantMaterial_FoodID = M.Material_ID">
</asp:SqlDataSource>
Here is the (abridged) SQLDataSource of the GridView:
<asp:SqlDataSource ID="Plant_Carton_Table" runat="server"
OldValuesParameterFormatString="old_{0}"
ConnectionString="<%$ ConnectionStrings:DBConnectionString %>"
OnInserting="Plant_Carton_Food_Table_Inserting"
OnInserted="Plant_Carton_Food_Table_Inserted"
InsertCommand="spPlantCartonInsert" InsertCommandType="StoredProcedure"
SelectCommand="spPlantCartonSelect" SelectCommandType="StoredProcedure"
UpdateCommand="spPlantCartonUpdate" UpdateCommandType="StoredProcedure">
<UpdateParameters>
<asp:Parameter Name="Active_Case" Type="Boolean" />
<asp:Parameter Name="PlantMaterial_FoodID" Type="String" />
<asp:Parameter Name="PlantMaterial_CaseID" Type="String" />
...
</UpdateParameters>
...
</asp:SqlDataSource>
And, finally, my exception:
DataBinding: 'System.Data.DataRowView'
does not contain a property with the
name 'PlantMaterial_FoodID'.
I really don't have much knowledge on how databinding through the GridView Edit templates work, but I was able to see that the correct values pass through the OnRowCommand event handler for updates. How do I propagate these values to the SQLDataSource without getting NULL?
Right now, I guess any explanation on how databinding with GridView templates work in general would be beneficial as well. Thanks!
This isn't really an answer to the question, but instead a workaround... but it works for me.
I added PlantMaterial_FoodID as a hidden column into the GridView and changed the SelectedValue binding on the DropDownList to reference this new column, as shown below.
New Column
<asp:TemplateField HeaderText="Food ID" SortExpression="Food_ID">
<EditItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Eval("Food_ID") %>'</asp:Label>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="FoodIDLabel" runat="server" Text='<%# Bind("Food_ID") %>'</asp:Label>
</ItemTemplate>
</asp:TemplateField>
...and here is the new binding
<asp:DropDownList ID="FoodCodeDropDownList" runat="server"
DataSourceID="Plant_Carton_Food_List" DataTextField="Food_Description"
DataValueField="PlantMaterial_FoodID" SelectedValue='<%# Bind("Food_ID") %>'
</asp:DropDownList>
This effectively sets the selected dropdownlist index to the correct value.
Have you checked that PlantMaterial_FoodID is spelled exactly as in the SqlDataSource SelectCommand?
Using Visual Web Developer Express 2010 with ASP.NET 4.0.
I have a FormView and I want to set a default value from the database. I can't get it to bind to the value in the database. My FormView looks like this:
<asp:FormView
ID="frmOrderDetails"
DataSourceID="sdsFormOrderDetails"
runat="server"
DataKeyNames="orderId">
<EditItemTemplate>
<h3>Edit Order Details</h3>
<asp:Label ID="lblStrategy" Text="Strategy:" AssociatedControlID="ddlStrategies" runat="server" />
<asp:DropDownList SelectedValue='<%# Bind("strategyId") %>'
ID="ddlStrategies"
runat="server"
DataTextField="strategy"
DataValueField="strategyId"
DataSourceID="sdsStrategies"
/>
<asp:LinkButton
id="lnkUpdate"
Text="Update Order"
CommandName="Update"
Runat="server" />
|
<asp:LinkButton
id="lnkCancel"
Text="Cancel"
CommandName="Cancel"
Runat="server" />
</EditItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="sdsFormOrderDetails" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetOrderDetails" SelectCommandType="StoredProcedure"
UpdateCommand="usp_UpdateOrder" UpdateCommandType="StoredProcedure">
<SelectParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" PropertyName="SelectedDataKey.Value" />
</SelectParameters>
<UpdateParameters>
<asp:ControlParameter Name="orderId" ControlID="grdOrders" />
</UpdateParameters>
</asp:SqlDataSource>
<asp:SqlDataSource ID="sdsStrategies" runat="server"
ConnectionString="<%$ ConnectionStrings:LocalSQLServer %>"
ProviderName="<%$ ConnectionStrings:LocalSQLServer.ProviderName %>"
SelectCommand="usp_GetStrategiesDropDown">
</asp:SqlDataSource>
The EditItemTemplate does not even load when I click the edit button on my FormView control, but I don't get an error message either.
You need to do the following to bind a DropDownList to the EditItemTemplate:
Add the DropDownList and its DataSource to the EditItemTemplate.
Set the DropDownList parameters:
DataSourceID="SqlDataSourceDropDownlist" SelectedValue=<%# Bind("ValueToBind") %>
DataTextField="ValueToDisplay" DataValueField="ValueToBind"
Set the ListView / FormView DataSource <UdateParameters>: <asp:Parameter Name="RankID" Type="Int32" />
you need to use formview Databound event like
protected void frmOrderDetails_DataBound(object sender, EventArgs e)
{
if (frmOrderDetails.CurrentMode == FormViewMode.Edit)
{
DropDownList ddlStrategies = (DropDownList)frmOrderDetails.FindControl("ddlStrategies");
ddlStrategies.SelectedValue = Your DB Value Goes here;
}
}