我在gridview控件中有一个下拉列表控件。网格视图控件和下拉列表连接到数据源。
在提交时,下拉控件仅选择第一个索引的值。
这是ASPX代码:
<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>
以下是按钮点击事件背后的代码:
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();
}
}
答案 0 :(得分:1)
按钮单击事件已触发回发,因此下拉列表值已重置为默认的第一个索引。另请注意,Page_Load事件将在按钮单击事件之前发生。
答案 1 :(得分:1)
从qID获取的行索引不正确。 qId的值可以从1开始,但网格视图的行索引从0开始。 因此,当qID(根据您的代码的行索引)为1时,您认为它正在访问第一行,而是访问第二行(行索引为1),最后选择第二行中下拉列表的选定值< / p>
因此,您使用了错误的值来获取行索引。如果您不相信我的答案,请尝试使用下面的代码测试您的代码,它将为您提供第一行中下拉列表的选定值。
DropDownList ddl = (DropDownList)gvQuestions.Rows[0].FindControl("ddlResponse");
responses = ddl.SelectedValue.ToString();
FIX:使用下面的代码隐藏按钮点击事件,希望它有所帮助:
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();
}
}