如何获取gridview中定义的dropdownlist.
我现在拖了一个控件gridview,在里边有自定义的几个控件,例如:textbox,dropdownlist等,现在点击某个按钮的时候,能够取出第一行dropdownlist的选择的内容,我的代码一直有错误,一直提示"未将对象引用到实例".
前台代码为:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication5.WebForm1" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Panel ID="Panel1" runat="server">
<asp:Label ID="lblTableEnglishName" runat="server" Text="表格英文名称:" Font-Size="Medium"></asp:Label>
<asp:TextBox ID="txtTableEnglishExplain" runat="server" AutoPostBack="True" Width="150px"></asp:TextBox>
<asp:Label ID="lblTableChineseName" runat="server" Text="表格中文名称:"></asp:Label>
<asp:TextBox ID="txtTableChineseExplain" runat="server"></asp:TextBox>
</asp:Panel>
</div>
<asp:GridView ID="gdvTableField" runat="server" AutoGenerateColumns="false"
OnRowDataBound="gdvTableField_RowCommand">
<Columns>
<asp:TemplateField HeaderText="中文名称">
<ItemTemplate>
<asp:TextBox ID="txtColumnChineseName" runat="server" AutoPostBack="true"
OnTextChanged="txtColumnChineseName_TextChanged">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="英文名称">
<ItemTemplate>
<asp:TextBox ID="txtColumnEnglishName" runat="server" AutoPostBack="true" OnTextChanged="txtColumnEnglishName_TextChanged">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="字段类型">
<ItemTemplate>
<asp:DropDownList ID="ddlFieldType" runat="server" AutoPostBack="true" OnSelectedIndexChanged="ddlFieldType_SelectedIndexChanged">
<asp:ListItem>字符串类型</asp:ListItem>
<asp:ListItem>数字类型</asp:ListItem>
<asp:ListItem>时间类型</asp:ListItem>
<asp:ListItem>文本类型</asp:ListItem>
<asp:ListItem>小数类型</asp:ListItem>
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="长度">
<ItemTemplate>
<asp:TextBox ID="txtIntegerCount" runat="server" AutoPostBack="true" OnTextChanged="txtIntegerCount_TextChanged">
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="小数位">
<ItemTemplate>
<asp:TextBox ID="txtDecimalCount" runat="server" AutoPostBack="true" Enabled="false" OnTextChanged="txtDecimalCount_TextChanged" >
</asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="删除">
<ItemTemplate>
<asp:Button ID="btnDeleteRow" runat="server" Text="删除" CommandName="aa" CausesValidation="false"
CommandArgument='<%# Container.DataItemIndex %>' OnClientClick="return confirm('确认删除吗?')" />
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<asp:LinkButton ID="lbtnAdd" runat="server" Text="添加内容"
onclick="lbtnAdd_Click"></asp:LinkButton>
<asp:LinkButton ID="lbtnDelete" runat="server" Text="删除内容"
onclick="lbtnDelete_Click"></asp:LinkButton>
<asp:Button ID="btnCreateTable" runat="server" onclick="btnCreateTable_Click"
Text="创建表格" />
</form>
</body>
</html>
后台取dropdownlist得代码为:
DropDownList ddl= gvr.FindControl("ddlFieldType") as DropDownList;
Response.Write(ddl.SelectedValue);
不知道哪里出错了,请各位大神给下建议.
[解决办法]