How to bind dropdownlist in EditItemTemplate in FormView control? - asp.net

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

Related

Cannot Find Control ID in ControlParameter

I am trying to insert values from a textbox but I get an error that it cannot find the controlid in the controlparameter. The TextBox is inside a formview, which is inside a listview. The SqlDataSource is outside the ListView.
My InsertButton_Click Method
protected void InsertButton_Click(object sender, EventArgs e)
{
var ctrl = (Control)sender;
var formview = (FormView)ctrl.NamingContainer;
formview.ChangeMode(FormViewMode.Insert);
SectionListView.DataSource = SectionDataSource;
SectionListView.DataBind();
}
The InsertItem Template
<InsertItemTemplate>
<tr style="">
<td>
<div style="font-size: .8em;">
<asp:FormView ID="SectionFormView" runat="server" DataKeyNames="SectionItemID" DataSourceID="SectionDataSource">
<ItemTemplate>
<asp:Button ID="InsertButton" runat="server" CommandName="Insert" Text="Insert" OnClick="InsertButton_Click" Font-Size="1.2em" />
<asp:Button ID="CancelButton" runat="server" CommandName="Cancel" Text="Clear" Font-Size="1.2em" />
<asp:Label ID="SectionItemLabel" runat="server" Text="SectionItem" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemTextBox" runat="server" />
<asp:Label ID="SectionItemSubLabel" runat="server" Text="SectionItem Label" Font-Bold="true" Font-Size="1.2em" />
<asp:TextBox ID="SectionItemLabelTextBox" runat="server"/>
</ItemTemplate>
</asp:FormView>
</div>
</td>
</tr>
</InsertItemTemplate>
My SqlDataSource
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>
When you want to use the child controls as control parameters, you need to use the qualified id as control id. Try changing the SQL data source as below
<asp:SqlDataSource ID="SectionDataSource" runat="server" ConnectionString="<%$ ConnectionStrings:ORHP_Dev03182014ConnectionString %>" SelectCommand="SELECT DISTINCT SectionItem,SectionItemLabel,SectionItemID FROM Core.SectionItem_Lkup"
InsertCommand="INSERT INTO Core.SectionItem_Lkup(SectionItem, SectionItemLabel) VALUES (#SectionItem, #SectionItemLabel)">
<InsertParameters>
<asp:ControlParameter ControlID="SectionFormView$SectionItemTextBox" Name="SectionItem" PropertyName="Text" />
<asp:ControlParameter ControlID="SectionFormView$SectionItemLabelTextBox" Name="SectionItemLabel" PropertyName="Text" />
</InsertParameters>
</asp:SqlDataSource>

Cascading One DropdownList with Another in Insert command on asp

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.

ASP.NET GridView inside UpdatePanel won't get out of Edit Mode

I have a pretty standard GridView with an SqlDataSource complete with SelectCommand/UpdateCommand/DeleteCommand.
I am utilising the Gridview's built-in commands for TemplateField'ed 's.
CommandName="Update"
CommandName="Edit"
CommandName="Cancel"
CommandName="Delete"
Everything works OK until I put the GridView inside an UpdatePanel.
1. When I click on 'edit' it goes into edit mode but won't get out of edit mode when you click Cancel or Update.
2. When I click on 'delete' performs as it should, however, I won't be able to get into edit mode anymore even if I click 'edit'
What's the matter here?
Example code:
<asp:UpdatePanel ID="UpdatePanel1" runat="server">
<ContentTemplate>
<div class="BoxFloat">
<h4>Aliases</h4>
<hr />
<asp:SqlDataSource ID="sqlPersonAlias" runat="server"
ConnectionString="<%$ ConnectionStrings:myConnectionString %>"
ProviderName="<%$ ConnectionStrings:myConnectionString.ProviderName %>"
DeleteCommand="DELETE FROM PersonAlias WHERE PersonAliasId=?PersonAliasId"
UpdateCommand="UPDATE PersonAlias SET AliasName=?AliasName WHERE PersonAliasId=?PersonAliasId"
SelectCommand="SELECT * FROM PersonAlias E INNER JOIN Person P ON P.PersonId=E.PersonId WHERE Username=?Username">
<DeleteParameters>
<asp:Parameter Name="PersonAliasId" />
</DeleteParameters>
<UpdateParameters>
<asp:Parameter Name="PersonAliasId" />
<asp:Parameter Name="AliasName" />
</UpdateParameters>
<SelectParameters>
<asp:QueryStringParameter Name="Username" QueryStringField="Username" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
<asp:GridView ID="gridviewPersonAlias" runat="server"
AutoGenerateColumns="False"
DataKeyNames="PersonAliasId"
DataSourceID="sqlPersonAlias">
<Columns>
<asp:TemplateField ShowHeader="False">
<EditItemTemplate>
<asp:ImageButton ID="imagebuttonCancel" ToolTip="Cancel" CommandName="Cancel" ImageUrl="~/images/cancel16.png" runat="server" />
<asp:ImageButton ID="imagebuttonUpdate" ToolTip="Apply" OnClientClick="return confirm('Are you sure?');" CommandName="Update" ImageUrl="~/images/apply16.png" runat="server" />
</EditItemTemplate>
<ItemTemplate>
<asp:ImageButton ID="imagebuttonDelete" ToolTip="Delete" OnClientClick="return confirm('Are you sure?');" CommandName="Delete" ImageUrl="~/images/delete16.png" runat="server" />
<asp:ImageButton ID="imagebuttonEdit" ToolTip="Edit" CommandName="Edit" ImageUrl="~/images/edit16.png" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="AliasName" HeaderText="Alias" SortExpression="AliasName" />
</Columns>
</asp:GridView>
<hr />
<asp:TextBox ID="textboxPersonAlias" runat="server" />
<asp:Button ID="buttonPersonAliasAdd" runat="server" Text="Add" onclick="buttonPersonAliasAdd_Click" />
</div>
</ContentTemplate>
</asp:UpdatePanel>
When "Update" is clicked, the database gets updated. It seems that the problem is just with the client-side.
Note: I'm using "?" instead of "#" for the parameters because that SqlDataSource is using MySq instead of MSSQL (I love VS2010).
I think what you need is full Postback or refresh the update Panel since it is most likely the gridview doesnt rebind
First try to put the sqldatasource outside the update panel. See if that works
I had the same problem in my project, and solved it.
here is my solution:
protected void gvPayments_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
//here is the edit code
//......
//at the end add these lines:
gvPayments.EditIndex = -1;
gvPayments.DataBind();
e.Cancel = true;
}

asp .net checkbox inside gridview checked

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

ASPx Display data in textbox from SQL source

<asp:SqlDataSource ID="textdata" runat="server"
ConnectionString="<%$ ConnectionStrings:TextConnectionString %>"
SelectCommand="SELECT SUM(pkNot) FROM [Not]">
<SelectParameters>
<asp:ControlParameter ControlID="notTotal" Name="pkNot" PropertyName="Text" Type="String" />
</SelectParameters>
</asp:SqlDataSource>
And the textbox is:
<asp:TextBox runat="server" ID="notTotal"></asp:TextBox>
How can I get that value to show up in the textbox? It's not working.
What about something alike this :
DataView beh = (DataView)SqlDataSource1.Select(DataSourceSelectArguments.Empty);
TextBox1.Text = beh[0][0].ToString();
A other way might be :
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind ('activitydate') %>'></asp:TextBox>
In the code behind. I wrote this in c# as you did not point out what you are using.

Resources