Reputation: 561
I have a dropdownlist control in a gridview control. The grid view control and the dropdown are connected to a datasource.
On submit, the dropdown control only picks the value of the first index.
Here's the ASPX Code:
<asp:GridView ID="gvQuestions" runat="server" AutoGenerateColumns="False" CellPadding="4"
ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:BoundField DataField="qID" HeaderText="QID" />
<asp:BoundField DataField="Question" HeaderText="Questions" />
<asp:TemplateField HeaderText="Response">
<ItemTemplate>
<asp:DropDownList ID="ddlResponse" runat="server" AppendDataBoundItems="True" AutoPostBack="false"
DataSourceID="ResponseSDS" DataTextField="response" DataValueField="responseID"
CssClass="ddlStyle" OnSelectedIndexChanged="ddlResponse_SelectedIndexChanged">
<%--<asp:ListItem Enabled="True" Selected="false"><--Selected Value--></asp:ListItem>--%>
</asp:DropDownList>
<asp:SqlDataSource ID="ResponseSDS" runat="server" ConnectionString="<%$ ConnectionStrings:HSELeadershipSurveyConnectionString %>"
SelectCommand="SELECT * FROM [tblResponse]"></asp:SqlDataSource>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#0085C9" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#0085C9" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#F5F7FB" />
<SortedAscendingHeaderStyle BackColor="#6D95E1" />
<SortedDescendingCellStyle BackColor="#E9EBEF" />
<SortedDescendingHeaderStyle BackColor="#4870BE" />
</asp:GridView>
And here's the code behind for the button click event:
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Get the responses for the questions
foreach (GridViewRow masterData in gvQuestions.Rows)
{
qID = int.Parse(masterData.Cells[0].Text);
//qID = (int)gvQuestions.DataKeys[(masterData.RowIndex)].Value;
//response = ((DropDownList)masterData.FindControl("ddlResponse")).SelectedValue.ToString();
DropDownList ddl = (DropDownList)gvQuestions.Rows[qID].FindControl("ddlResponse");
responses = ddl.SelectedValue.ToString();
}
}
Upvotes: 0
Views: 1663
Reputation: 417
The row Index obtained from the qID is incorrect. Values of qId could start from 1 but the row index of the grid view starts from 0. So when the qID ( row index as per your code) is 1 you think it is accessing the first row but instead it accesses the second row ( with row index as 1) ending up picking the selected value of the drop down in second row
So you are using a wrong value to obtain the row index. If you are not convinced with my answer try testing your code with the below it will give you the selected value of drop down in first row.
DropDownList ddl = (DropDownList)gvQuestions.Rows[0].FindControl("ddlResponse");
responses = ddl.SelectedValue.ToString();
FIX: Use the below code behind for the button click event, hope it helps:
protected void btnSubmit_Click(object sender, EventArgs e)
{
// Get the responses for the questions
foreach (GridViewRow masterData in gvQuestions.Rows)
{
// the actual way to get your row index
int rowIndex = masterData.RowIndex;
//qID = (int)gvQuestions.DataKeys[(masterData.RowIndex)].Value;
//response = ((DropDownList)masterData.FindControl("ddlResponse")).SelectedValue.ToString();
DropDownList ddl = (DropDownList)gvQuestions.Rows[rowIndex ].FindControl("ddlResponse");
responses = ddl.SelectedValue.ToString();
}
}
Upvotes: 1
Reputation: 795
The button click event has triggered a postback so the dropdownlist value has been reset to the default first index. Also note that the Page_Load event will happen before your button click event.
Upvotes: 1