我有一个gridview,其中有一些下拉列表用于更新。我想在用户更新时获取新值。我已经使用了这里描述的方法http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating%28v=vs.110%29.aspx但是,我收到了这个错误:无法将'System.Web.UI.LiteralControl'类型的对象强制转换为'System.Web.UI.WebControls.DropDownList'类型。这行代码字符串状态=((DropDownList)(gvr.Cells [8] .Controls [0]))。文本;我试过改变索引[8],但没有运气。这有什么问题?提前感谢您的时间和精力。
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" BackColor="White" BorderColor="#3366CC" BorderStyle="Solid" BorderWidth="2px" CellPadding="4" DataKeyNames="id" DataSourceID="SqlDataSource1" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" />
<asp:BoundField DataField="id" HeaderText="id" InsertVisible="False" ReadOnly="True" SortExpression="id" Visible="False" />
<asp:BoundField DataField="userName" HeaderText="User Name" ReadOnly="True" SortExpression="userName" />
<asp:BoundField DataField="employeeName" HeaderText="Employee Name" ReadOnly="True" SortExpression="employeeName" />
<asp:BoundField DataField="division" HeaderText="Division" ReadOnly="True" SortExpression="division" />
<asp:TemplateField HeaderText="Start Date" SortExpression="startDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("startDate", "{0:D}") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="TextBox1_CalendarExtender" runat="server" Enabled="True" TargetControlID="TextBox1" Format="D">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("startDate", "{0:D}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="End Date" SortExpression="endDate">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("endDate", "{0:D}") %>'></asp:TextBox>
<ajaxToolkit:CalendarExtender ID="TextBox2_CalendarExtender" runat="server" ClearTime="True" Enabled="True" Format="D" TargetControlID="TextBox2">
</ajaxToolkit:CalendarExtender>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label4" runat="server" Text='<%# Bind("endDate", "{0:D}") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Type" SortExpression="type">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList1" runat="server" SelectedValue='<%# Bind("type") %>'>
<asp:ListItem>Personal</asp:ListItem>
<asp:ListItem>Vacation</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("type") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Status" SortExpression="status">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("status") %>'>
<asp:ListItem>Pending</asp:ListItem>
<asp:ListItem>Appoved</asp:ListItem>
<asp:ListItem>Declined</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("status") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<FooterStyle BackColor="#99CCCC" ForeColor="#003399" />
<HeaderStyle BackColor="#003399" Font-Bold="True" ForeColor="#CCCCFF" />
<PagerStyle BackColor="#99CCCC" ForeColor="#003399" HorizontalAlign="Left" />
<RowStyle BackColor="White" ForeColor="#003399" />
<SelectedRowStyle BackColor="#009999" Font-Bold="True" ForeColor="#CCFF99" />
<SortedAscendingCellStyle BackColor="#EDF6F6" />
<SortedAscendingHeaderStyle BackColor="#0D4AC4" />
<SortedDescendingCellStyle BackColor="#D6DFDF" />
<SortedDescendingHeaderStyle BackColor="#002876" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:WebPortal_Call4HealthReports_ConnectionString %>" DeleteCommand="usp_HR_TimeOffRequestsDelete" DeleteCommandType="StoredProcedure" InsertCommand="usp_HR_TimeOffRequestsInsert" InsertCommandType="StoredProcedure" SelectCommand="usp_HR_TimeOffRequestsSelectAll" SelectCommandType="StoredProcedure" UpdateCommand="usp_HR_TimeOffRequestsUpdate2" UpdateCommandType="StoredProcedure">
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
<InsertParameters>
<asp:Parameter Name="userName" Type="String" />
<asp:Parameter Name="employeeName" Type="String" />
<asp:Parameter Name="division" Type="String" />
<asp:Parameter DbType="Date" Name="startDate" />
<asp:Parameter DbType="Date" Name="endDate" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="status" Type="String" />
</InsertParameters>
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter DbType="Date" Name="startDate" />
<asp:Parameter DbType="Date" Name="endDate" />
<asp:Parameter Name="type" Type="String" />
<asp:Parameter Name="status" Type="String" />
</UpdateParameters>
</asp:SqlDataSource>
<ajaxToolkit:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
</ajaxToolkit:ToolkitScriptManager>
代码隐藏:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
GridViewRow gvr = (GridViewRow)GridView1.Rows[e.RowIndex];
string status = ((DropDownList)(gvr.Cells[8].Controls[0])).Text;
}
答案 0 :(得分:1)
使用FindControl查找您正在查找的控件,而不是对索引进行硬编码。
DropDownList ddl = (DropDownList)gvr.FindControl("DropDownList1");
string status = ddl.Text;