Using a dropdownlist control inside a formview - asp.net

I am new to asp.net and i have a problem using a dropdownlist control inside a formview and passing its value to the related sqldatasource. When i use the code below i get the following exception
Exception Details: System.InvalidOperationException: Could not find control 'ddlCategory' in ControlParameter 'categoryId'.
The Dropdownlist inside a formview.
<asp:DropDownList ID="ddlCategory" DataSourceID="ObjectDataSourceCategory" DataTextField="NAME" DataValueField="ID" runat="server" />
The SQL Data Source
<asp:ObjectDataSource ID="sqlDataSourceItem" TypeName="Item" runat="server"
SelectMethod="getItem"
InsertMethod="insertItem"
UpdateMethod="updateItem">
<SelectParameters>
<asp:QueryStringParameter QueryStringField="id" Name="id" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter ControlID="ddlCategory" Name="categoryId" PropertyName="selectedValue" />
</InsertParameters>
</asp:ObjectDataSource>
And i have found the solution to this problem. I have changed the ID of the DDL in the control parameter. It works like below since this is the final generated id of that control. But i think there must be an easier and better way. Any help would be appriciated.
<asp:ControlParameter ControlID="ctl00$main$frmViewItem$ddlCategory" Name="categoryId" PropertyName="selectedValue" />

This answer will provide a solution to your problem:
You need a recursive findcontrol() method.

It is because of your ddlCategory is inside formview and you are using the master page. The best way is to overwrite the 'FindControl' function of master page.
pls see the following link for detail:
http://geekswithblogs.net/AzamSharp/archive/2006/08/27/89475.aspx

Related

SqlDataSource with DropDownList Control Parameter doesn't read value

I have a drop down list that bound to a SqlDataSource.
I have another drop down list that's bound to a different SqlDataSource.
The second SqlDataSource has the first drop down as a Control Parameter.
I'm trying to do this...
<asp:SqlDataSource ID="sqlDataSource2" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
SelectCommand="SELECT * FROM Test WHERE Param = #param;"
CancelSelectOnNullParameter="true">
<SelectParameters>
<asp:ControlParameter ControlID="dropDown1" Name="param"
PropertyName="SelectedValue"
ConvertEmptyStringToNull="true" />
</SelectParameters>
</asp:SqlDataSource>
dropDown1.SelectedValue = "someValue"
dropDown2.DataBind()
but I don't get any results. However, if I set the second SqlDataSource's Control Parameter to a text box, it works. For example, this works:
<asp:ControlParameter ControlID="txt" Name="param"
PropertyName="Text"
ConvertEmptyStringToNull="true" />
txt.Text = "someValue"
dropDown2.DataBind()
Any ideas why this is?
I ended up figuring this one out. The problem was that the drop down was attempting to bind twice, much like the problem in this question.
I used the suggestion made by Joel Etherton, and now it works perfectly. Although I used a hidden control rather than a label.

populating dropdownlist from another dropdown selection in ASP.Net Web Form

I am trying to Populate data into the Company DropDownList after selecting the Country from the CountryDropDownList. Complete newbiew on ASP.Net Web Form. So please excuse little knowledge.
I have been trying to do it only on the .aspx page which I am not entirely sure if I could do it. My code is as follows, which doesn't populate anything on the CompanyDropDownList at all. The problem is I have been writing the code looking at different sources but I guess I am not entirely sure where the problem is and what am I doing wrong. If anyone can explain a little bit and spot my errors would be really helpful.
<asp:DropDownList
ID="CompanyDropDownList"
runat="server"
OnSelectedIndexChanged="CompanyDropDownList_SelectedIndexChanged"
DataSourceID="CompanyNameSqlDataSource"
DataTextField="CompanyName"
DataValueField="CompanyID"
AppendDataBoundItems="True"
AutoPostBack="True">
</asp:DropDownList>
<asp:SqlDataSource
ID="CompanyNameSqlDataSource"
runat="server"
ConnectionString="<%$ ConnectionStrings:BIGroupCostConnectionString %>"
OnSelecting="CountryNameSqlDataSource_Selecting"
SelectCommand="SELECT CompanyName, countryID FROM Company Where CompanyID = #CompanyID">
<SelectParameters>
<asp:ControlParameter
Name="CountryID"
ControlID="CountryDropDownList"
PropertyName="SelectedValue"
Type= "String"/>
<asp:Parameter Name="CountryID" />
</SelectParameters>
</asp:SqlDataSource>
Thanks.
I think the problem is that you are passing the company drop-down as parameter instead of the country you mention.
<SelectParameters>
<asp:ControlParameter Name="CompanyID" ControlID="CompanyDropDownList"
PropertyName="SelectedValue" Type= "String"/>
<asp:Parameter Name="CompanyID" />
Shouldn't it be your Country control?
Ademar
Found the problem. The problem was the following line of code. Removing the line showed the data correctly.
<asp:Parameter Name="CountryID" />

How do I bind dependent drop down lists in ASP.NET

I need a way to bind multiple drop down lists that are populated based on their parent node.
RootDDL <- child <- grandchild[]
The code looks something similar to
<FormView DataSoureceID="rootDatasource">
<DropDownList ID="RootDLL" AutoPostBack="true">
<!--items-->
</DropDownList>
<DropDownList ID="child" AutoPostBack="true" DataSourceID="ChildDataSource" />
<SqlDataSource ID="ChildDataSource" />
<DropDownList ID="grandchild" DataSourceID="GrandChildDataSource" SelectedValue='<%# Bind('SomeFieldInRootDatasource') %>' />
<SqlDataSource ID="GrandChildDataSource">
<SelectParameters>
<ControlParameter ControlID="child" PropertyName="SelectedValue" />
</SelectParameters>
</SqlDataSource>
<Button Command="Update" />
</FormView>
<SqlDataSource ID="rootDatasource">
<InsertParameters>
<asp:Parameter Name="SomeFieldInRootDatasource" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="SomeFieldInRootDatasource" />
</UpdateParamters>
</SqlDataSource>
Changing the rootDDL works, unfortunately when the Child selectedvalue changes the grand child <%# Bind %> tries to rebind but cannot as it the FormView is no longer acting as a container.
It fails with
Databinding methods such as Eval(), XPath(), and Bind() can only be used in the context of a databound control.
Is there any solution to this problem that will not require me to add event (multiple) handlers?
I ended up creating a hiddenfield for each child dropdownlist that I then bound with the value I needed. I created a generic indexchange handler to then update the hiddenfeild with the desired result. This results in the bound field not being updated by a datasource (avoiding the rebind system call that was causing the issue in the first place).
Not sure if this is the best solution but it should work for most cases.

GridView and DropDownlist

I have a gridview that when enter to edit mode one of the column change to dropdownlist:
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server"
DataSourceID="SqlDataSource1" DataTextField="name" DataValueField="name">
</asp:DropDownList>
</EditItemTemplate>
Now i have a SqlDataSource with update method that define in this aspx file:
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:ControlParameter ControlID="DropDownList2" Type="string"
PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
now i want to get the selected value and insert it but when i press the Update button in the row in the gridview i get this error:
Could not find control 'DropDownList2' in ControlParameter 'genre'
any idea why it happen?
Yes. ControlParameters only work when the DOM can find the control you're referring to that's within the same branch of the GridView. The problem is that Gridviews are a poor way of handling DOM controls because as soon as you go into a "mode" of the GridView like the EDIT mode, the entire DOM structure changes. The DOM by the way, is the Document Object Model. Because it's changed, therefore ASP cannot find the control you're referring to.
I've overcome this by doing one of two things.
First see if it works by simply tailing the control name with the '$' character.
<UpdateParameters>
<asp:Parameter Name="name" Type="String" />
<asp:ControlParameter ControlID="MyGridView$DropDownList2" Type="string"
PropertyName="SelectedValue" Name="genre" />
</UpdateParameters>
This sometimes works if you're lucky.
If not, then you'll need to find the control, get its value and pass it into the SQL parameter programmatically in code behind.
Try something like this (I use C#) ...
protected void MyGridView_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
...
GridViewRow gvRow = (GridViewRow)sender;
DropDownList myDDL = (DropDownList)gvRow.FindControl("DropDownList2");
if (myDDL != null)
{
//assuming your datasource is outside the gridview, you have to find it!
SqlDataSource sds1 = (SqlDataSource)page.FindControl("myDataSource");
if (sds1 != null)
{
sds1.UpdateParameter["genre"].DefaultValue = myDDL.SelectedValue;
... and do your databinding etc
sds1.Update();
}
}
}
I always rely on the client-side code (ASPX) to do most of the work for me, like Bind(), Eval(), etc, but over time you'll realise that to really control your program, you'll have to rely on JavaScript or code behind to do the finer bits. ASP is not always "clever" in doing what you expect it to do.

How Grid paging works?

The paging is working fine here, the problem is I don't know how it works, because when I put a break point and a logger in the GetCustomers method, I found that parameters maximumRows and startRowIndex are always 0, 0.
I have no clue why StartRowIndexParameterName, and MaximumRowsParameterName are created and how they are used. If they are used in custom mode, so how to enter that mode?
Thanks
<body>
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectCountMethod="Count"
SelectMethod="GetCustomers" TypeName="Pilots.BLL.Customer">
<SelectParameters>
<asp:Parameter Name="maximumRows" Type="Int32" />
<asp:Parameter Name="startRowIndex" Type="Int32" />
</SelectParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" AllowPaging="true" PageIndex="0" PageSize="10"
DataSourceID="ObjectDataSource1">
</asp:GridView>
</div>
</form>
</body>
Here's a pretty thorough MSDN Article on GridView and Paging. Should be able to answer most all of your questions.
Those 2 parameters are used for custom paging on the gridview. That will be passed to SQL server to determine at what index to start the page (startRowIndex) and how many items on one gridview page (maximumRows).
The 'startRowIndex' will change depending on what the GridView.PageIndex is.
The number of rows returned from the method depends on what value set on 'maximumRows'.
It seems that I had to use EnablePaging as descried here,
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.objectdatasource.enablepaging.aspx

Resources