GridView中怎的得到DropDownList 选择中的值
GridView中怎样得到DropDownList 选择中的值
前台代码:
后台获取选中值的代码:
------解决方案--------------------
这个问题已经有了 你可以看这里http://bbs.csdn.net/topics/350109648
------解决方案--------------------
你写错了
string txt = ((DropDownList)GridView1.Rows[i].FindControl("DropDownList1")).SelectedItem.Value;
应该是
你怎么能把控件类型直接复制给string呢?
------解决方案--------------------
你索引的i是什么? 你自己写的循环?
GridView1.Rows[e.ItemIndex]
------解决方案--------------------
我是这样实现的。你可以对照改一下(如果有隐藏的列也算一个Cell)。
------解决方案--------------------
请把上面的 <strong>和</strong>去掉,不支持加粗显示
------解决方案--------------------
findcontor就是了
------解决方案--------------------
在RowDataBound事件中处理,然后通过FindControl找到该控件。
------解决方案--------------------
需要注意的是,判断一下为Row的类型
前台代码:
<asp:templatefield headertext="单品促销">
<itemtemplate>
<asp:dropdownlist id="DropDownList1" runat="server" AutoPostBack="true" OnTextChanged="DropDownList1_TextChanged" />
</itemtemplate>
</asp:templatefield>
后台获取选中值的代码:
string txt = ((DropDownList)GridView1.Rows[i].FindControl("DropDownList1")).SelectedItem.Text.ToString();//这里错误,提示:System.NullReferenceException: 未将对象引用设置到对象的实例。
string txt = ((DropDownList)GridView1.Rows[i].FindControl("DropDownList1")).SelectedItem.Value; //同样错误。
((DropDownList)GridView1.Rows[i].FindControl("DropDownList1")).SelectedValue.ToString(); //返回空值
DropDownList
GridView
String
------解决方案--------------------
这个问题已经有了 你可以看这里http://bbs.csdn.net/topics/350109648
------解决方案--------------------
你写错了
string txt = ((DropDownList)GridView1.Rows[i].FindControl("DropDownList1")).SelectedItem.Value;
应该是
DropDownList ddl = (DropDownList)GridView1.Rows[i].FindControl("DropDownList1");
string txt = ddl.SelectedValue;
你怎么能把控件类型直接复制给string呢?
------解决方案--------------------
你索引的i是什么? 你自己写的循环?
GridView1.Rows[e.ItemIndex]
------解决方案--------------------
((DropDownList)this.GridView1.Rows[e.RowIndex].Cells[4].FindControl("DropDownList1")).SelectedItem.Text
我是这样实现的。你可以对照改一下(如果有隐藏的列也算一个Cell)。
------解决方案--------------------
请把上面的 <strong>和</strong>去掉,不支持加粗显示
------解决方案--------------------
findcontor就是了
------解决方案--------------------
在RowDataBound事件中处理,然后通过FindControl找到该控件。
------解决方案--------------------
需要注意的是,判断一下为Row的类型