<asp:TemplateField HeaderText="机型">
<ItemTemplate>
<asp:DropDownList runat="server" ID="drop1" DataSourceID="ObjectDataSource2" DataTextField="M_type" DataValueField="id" SelectedValue='<%# Bind("m_id") %>' Enabled="false" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
在asp.net 中 怎么才能取到asp:GridView 控件中asp:DropDownList 的值?
string zhi = drop.selectValue;
==
要看你打算在什么控件的什么事件中得到
但是程序说的是 未将对象引用设置到对象的实例。
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NullReferenceException: 未将对象引用设置到对象的实例。
asp:DropDownList 的 Enabled="false" 我要在点了更新之后给他改为 true 但不会成功
protected void Button3_Click(object sender, EventArgs e)
{
string strDropDownListSelectValue = string.Empty;
int x = GridView1.Rows.Count;
for (int i = 0; i < x; i++)
{
strDropDownListSelectValue += ((DropDownList)GridView1.Rows[i].Cells[0].FindControl("DropDownList1")).SelectedValue+"("+i.ToString()+")"+"---------";
}
Response.Write(strDropDownListSelectValue);
//Response.Write(x.ToString());
Response.End();
}
我的 asp:DropDownList 的 Enabled="false" 我要在点了更新之后给他改为 true
string zhi = drop.selectValue;
可以取到的!
要是要改变Enabled,利用Ajax更简单
你的事件顺序错误,你要变更DropDownlist的值要在editing事件中
第一步:先在GridView的行editing事件中使((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1")).enable=true;
第二步:在GridView的行update事件中获取DropDownList的值((DropDownList)GridView1.Rows[e.RowIndex].Cells[0].FindControl("drop1")).SelectedValue;
GridView1.Rows[i].Cells[0].Enabled = False
这样做了,但不不能生效的,不能给他赋值呀.
<asp:TemplateField HeaderText="机型">
<ItemTemplate>
<asp:DropDownList runat="server" ID="drop1" DataSourceID="ObjectDataSource2" DataTextField="M_type" DataValueField="id" SelectedValue='<%# Bind("m_id") %>' Enabled="false" ></asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].FindControl("drop1") as DropDownList;
ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
}
protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
{ DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].FindControl("drop1") as DropDownList;
ddlBigClass.Enabled = false;
}
应该这样:protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
}
protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
{ DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
ddlBigClass.Enabled = false;
}
protected void CustomersGridView_RowUpdating(Object sender, GridViewUpdateEventArgs e)
{
DropDownList ddlBigClass = GridView1.Rows[e.RowIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
ObjectDataSource1.UpdateParameters["M_id"].DefaultValue = ddlBigClass.SelectedValue;
}
protected void CustomersGridView_RowEditing(Object sender, GridViewEditEventArgs e)
{ DropDownList ddlBigClass = GridView1.Rows[e.NewEditIndex].Cells[你的单元格索引].FindControl("drop1") as DropDownList;
ddlBigClass.Enabled = false;
}