Answered by:
Can't get the parameter/value of gridview control for dropdownlist in detailsview control

Question
-
User1214317495 posted
Hello all,
I am struggling with getting a detailsview control to work in the way I need it to. The detailsview control is populated with info when a row in a gridview is selected. I want to edit one field, and am trying to use a dropdown list in the detailsview to edit the information. I can't get this dropdownlist to populate correctly.
I am using an SQL query to populate the ddlist, and it almost works. The problem is when I try to filter the results. I need to "select * from table where 'role' = a specific gridview columnName. I can't correctly get the gridview columnName as a parameter value to populate the dropdownlist located in the detailsview control.
The dropdownlist is an itemTemplate field in the detailsview.
When I configure the SQLDataSource that populates the dropdownlist, and use 'control' as the parameter source I get this statement...
"SELECT [ID], [lastName], [firstName], [role], [group] FROM [employees] WHERE ([role] = @theDiscipline)
theDiscipline is the column name in the gridview.
As I configure the SQLDataSource and test the query, when I supply a default value for the parameter, it works. At runtime, it doesn't. The dropdown list is always empty. It will work if I remove the where clause that gets the parameter from the gridview, but I need to filter the results. I can't get the parameter set correctly when using the gridview control to set it.
I hope that makes sense. Please let me know if that explanation is not clear. Any help will be appreciated. Thank you all.
Thursday, February 28, 2019 12:23 PM
Answers
-
User-1174608757 posted
Hi kjpell
The column of Detailsview is based on the sqldatasource control . So , you mean different sqldatasource for DetailsView and Gridview?
I have tried on my side ,when they don't have the same column names , you just need to make sure the selected column of Gridview exists same value as column of detailsview and change the DataKeyNames of Gridview. You don't need to change the name of SelectParameters.
Here is the demo , it will help you understand.
First , there are two tables for Gridview and Detailsview:
Teacher table for Gridview:
Students table for Detailsview:
Now I first bind teacher table to Gridview in aspx.cs
public partial class detailsview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from teacher"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } }
Then in aspx:
I should change is Gridview datakeynames , because the source has changed, I must the change the datakeynames to column name in Teacher table.
<asp:GridView ID="GridView1" AutoGenerateSelectButton="true" runat="server" DataKeyNames="number" > </asp:GridView>
Now in sqldatacontrol for detailsview, we don't need to change anything, Name= "IDnumber" in fact corresponds to @IDnumber in Select command and PropertyName="Selected value" has already changed because the Datakeynames of Gridview1 has changed.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT * FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters>
So you could see as below :
For aspx:
<head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1"> <Fields> <asp:BoundField DataField="IDnumber" HeaderText="IDnumber" SortExpression="IDnumber" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:CheckBoxField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> <asp:BoundField DataField="SCORE" HeaderText="SCORE" SortExpression="SCORE" /> <asp:BoundField DataField="Math" HeaderText="Math" SortExpression="Math" /> <asp:BoundField DataField="Chinese" HeaderText="Chinese" SortExpression="Chinese" /> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="Name" DataSourceID="SqlDataSource2"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div> <asp:GridView ID="GridView1" AutoGenerateSelectButton="true" runat="server" DataKeyNames="number" > </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT * FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT [Name] FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </form> </body> </html>
You could see as below:
Best Regards
Wei Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 1, 2019 5:36 AM
All replies
-
User-1174608757 posted
Hi Kjpell,
According to your description, I have made a sample here.I have used a sqldatasource control for the data source of Detailsview control and I have used gridview columnName as a parameter value to populate the dropdownlist located in the detailsview control.Below is my code , I hope it could help you.
If it is not what you want , I hope you could post your code to show what you want.
Aspx:
<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1"> <Fields> <asp:BoundField DataField="IDnumber" HeaderText="IDnumber" SortExpression="IDnumber" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:CheckBoxField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> <asp:BoundField DataField="SCORE" HeaderText="SCORE" SortExpression="SCORE" /> <asp:BoundField DataField="Math" HeaderText="Math" SortExpression="Math" /> <asp:BoundField DataField="Chinese" HeaderText="Chinese" SortExpression="Chinese" /> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="Name" DataSourceID="SqlDataSource2"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div> <asp:GridView ID="GridView1" AutoGenerateSelectButton="true" runat="server" DataKeyNames="IDnumber"> </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT * FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT [Name] FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </form> </body> </html>
Aspx.cs:
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) {
// bind datasource to Gridview, string sql = "select * from students"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } }It shows as below:
Best Regards
Wei Zhang
Friday, March 1, 2019 3:29 AM -
User1214317495 posted
Thank you so much!
A couple of quick questions.
If the "IDnumber" column in the GridView does not have the same name as the ID column in the DetailsView (also named "IDnumber" in your example), how would things change?
Let's say the GridView the column is named "employeeID" and the matching column in the DetailsView is named "IDnumber"
Would I just change the code for the GridView from DataKeyNames="IDnumber" to DataKeyNames="employeeID" and change the SqlDataSource1 and SqlDataSource2 ControlParameter Names to Name="employeeID"
Friday, March 1, 2019 4:34 AM -
User-1174608757 posted
Hi kjpell
The column of Detailsview is based on the sqldatasource control . So , you mean different sqldatasource for DetailsView and Gridview?
I have tried on my side ,when they don't have the same column names , you just need to make sure the selected column of Gridview exists same value as column of detailsview and change the DataKeyNames of Gridview. You don't need to change the name of SelectParameters.
Here is the demo , it will help you understand.
First , there are two tables for Gridview and Detailsview:
Teacher table for Gridview:
Students table for Detailsview:
Now I first bind teacher table to Gridview in aspx.cs
public partial class detailsview : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { string sql = "select * from teacher"; GridView1.DataSource = SqlHelper.ExecuteDataTable(sql); GridView1.DataBind(); } } }
Then in aspx:
I should change is Gridview datakeynames , because the source has changed, I must the change the datakeynames to column name in Teacher table.
<asp:GridView ID="GridView1" AutoGenerateSelectButton="true" runat="server" DataKeyNames="number" > </asp:GridView>
Now in sqldatacontrol for detailsview, we don't need to change anything, Name= "IDnumber" in fact corresponds to @IDnumber in Select command and PropertyName="Selected value" has already changed because the Datakeynames of Gridview1 has changed.
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT * FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters>
So you could see as below :
For aspx:
<head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <asp:DetailsView ID="DetailsView1" runat="server" Height="50px" Width="125px" AutoGenerateRows="False" DataSourceID="SqlDataSource1"> <Fields> <asp:BoundField DataField="IDnumber" HeaderText="IDnumber" SortExpression="IDnumber" /> <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" /> <asp:CheckBoxField DataField="Gender" HeaderText="Gender" SortExpression="Gender" /> <asp:BoundField DataField="SCORE" HeaderText="SCORE" SortExpression="SCORE" /> <asp:BoundField DataField="Math" HeaderText="Math" SortExpression="Math" /> <asp:BoundField DataField="Chinese" HeaderText="Chinese" SortExpression="Chinese" /> <asp:TemplateField HeaderText="Name"> <ItemTemplate> <asp:DropDownList ID="DropDownList1" runat="server" DataValueField="Name" DataSourceID="SqlDataSource2"></asp:DropDownList> </ItemTemplate> </asp:TemplateField> </Fields> </asp:DetailsView> </div> <asp:GridView ID="GridView1" AutoGenerateSelectButton="true" runat="server" DataKeyNames="number" > </asp:GridView> <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT * FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:mssqlserver %>" SelectCommand="SELECT [Name] FROM [students] WHERE ([IDnumber] = @IDnumber)" > <SelectParameters> <asp:ControlParameter ControlID="GridView1" Name="IDnumber" PropertyName="SelectedValue" Type="Int32" /> </SelectParameters> </asp:SqlDataSource> </form> </body> </html>
You could see as below:
Best Regards
Wei Zhang
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, March 1, 2019 5:36 AM -
User1214317495 posted
Thank you for your help!
Wednesday, March 6, 2019 1:45 AM