MoshangYanYu 于 2016-06-01 16:42:53 编辑
本人在Datagridview的模板列中有两个dropdownlist,一个是接收人单位,一个是接收人。两个下拉框需要联动。
现在数据已经加载出来了,但是接收人这个dropdownlist的selectindexchanged事件里面却获取不到下拉框的对象
请教大家,怎么获取下拉框对象?
SqlDataBase sd = new SqlDataBase(); string sql = ""; DataTable dt = new DataTable(); DropDownList jsrdw=new DropDownList(); DropDownList jsr=new DropDownList(); protected void DataGrid1_ItemDataBound(object sender, DataGridItemEventArgs e) { if (e.Item.ItemIndex > -1) { jsrdw = (DropDownList)e.Item.Cells[10].Controls[1]; jsr = (DropDownList)e.Item.Cells[11].Controls[1]; sql = "select distinct xueyuan from userinfo2 where xueyuan is not null"; dt = sd.DT(sql); jsrdw.DataSource = dt; jsrdw.DataTextField = "xueyuan"; jsrdw.DataValueField = "xueyuan"; jsrdw.DataBind(); sql = "select xingming from userinfo2 where xingming is not null"; dt = sd.DT(sql); jsr.DataSource = dt; jsr.DataTextField = "xingming"; jsr.DataValueField = "xingming"; jsr.DataBind(); } } protected void jieshoudanwei_SelectedIndexChanged(object sender, EventArgs e) { jieShouRenDanWei = jsrdw.SelectedItem.Text; //这里报空指针异常,请教大家应该怎么改为好?谢谢大家! sql = "select xingming from userinfo2 where xingming is not null and xueyuan =""+jieShouRenDanWei+"""; dt=sd.DT(sql); jsr.DataSource = dt; jsr.DataValueField = "xingming"; jsr.DataTextField = "xingming"; jsr.DataBind(); }
<asp:DataGrid ID="DataGrid1" runat="server" AllowPaging="True" AutoGenerateColumns="False" PageSize="50" Width="100%" OnItemCommand="DataGrid1_ItemCommand" OnItemDataBound="DataGrid1_ItemDataBound" OnDeleteCommand="DataGrid1_DeleteCommand"> <PagerStyle Mode="NumericPages" Visible="False" /> <Columns> <asp:BoundColumn DataField="id" HeaderText="序号" ReadOnly="True"></asp:BoundColumn> <asp:BoundColumn DataField="tiaoxingma" HeaderText="条形码"></asp:BoundColumn> <asp:BoundColumn DataField="mingcheng" HeaderText="药品名称"></asp:BoundColumn> <asp:BoundColumn DataField="leixing" HeaderText="类型"></asp:BoundColumn> <asp:BoundColumn DataField="guige" HeaderText="规格"></asp:BoundColumn> <asp:BoundColumn DataField="shengchanshang" HeaderText="生产商"></asp:BoundColumn> <asp:BoundColumn DataField="shuliang" HeaderText="购入数量"></asp:BoundColumn> <asp:BoundColumn DataField="cunfangdd" HeaderText="存放位置"></asp:BoundColumn> <asp:BoundColumn DataField="riqi" HeaderText="购入日期"></asp:BoundColumn> <asp:BoundColumn DataField="lingyongr" HeaderText="购买人"></asp:BoundColumn> <asp:TemplateColumn HeaderText="接收人单位"> <ItemTemplate> <asp:DropDownList ID="jieshoudanwei" runat="server" AutoPostBack="True" OnSelectedIndexChanged="jieshoudanwei_SelectedIndexChanged"> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="接收人"> <ItemTemplate> <asp:DropDownList ID="jieshouren" runat="server"> </asp:DropDownList> </ItemTemplate> </asp:TemplateColumn> <asp:TemplateColumn HeaderText="接收数量"> <ItemTemplate> <asp:TextBox ID="jieshouNum" runat="server" Width="100px"></asp:TextBox> </ItemTemplate> </asp:TemplateColumn> <asp:ButtonColumn CommandName="tiaoji" HeaderText="调剂" Text="调剂"></asp:ButtonColumn> <asp:TemplateColumn HeaderText="删除"> <ItemTemplate> <asp:LinkButton runat="server" CausesValidation="false" CommandName="Delete" Text="删除" OnClientClick="return confirm("确定要删除吗?")"></asp:LinkButton> </ItemTemplate> </asp:TemplateColumn> </Columns> <HeaderStyle BackColor="#00CCCC" Font-Bold="True" Font-Italic="False" Font-Overline="False" Font-Strikeout="False" Font-Underline="False" /> </asp:DataGrid> </div>
解决方案
20
jieshoudanwei_SelectedIndexChanged中是认不出DropDownList jsrdw的.
你试试:
((sender as DropDownList).Parent.Controls.FindControl(“jieshoudanwei”) as DropDownList).Text
差不多是这样
你试试:
((sender as DropDownList).Parent.Controls.FindControl(“jieshoudanwei”) as DropDownList).Text
差不多是这样
20
jieShouRenDanWei = jsrdw.SelectedItem.Text;
你在调用jsrdw.SelectedItem.之前需要保证selecteditem不为空,目测你代码里面没有选择item的地方。
你在调用jsrdw.SelectedItem.之前需要保证selecteditem不为空,目测你代码里面没有选择item的地方。