protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sCgyNam = ((Label)e.Row.Cells[1].FindControl("lbCgyNam")).Text.Trim();
DropDownList drop = (DropDownList)e.Row.FindControl("dropCgyNam"); // 找不到dropCgyNam
Function.BindDropDownList(drop, "nam", "id", "类别");
//drop.Items.FindByText(sCgyNam).Selected = true;
}
}
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sCgyNam = ((Label)e.Row.Cells[1].FindControl("lbCgyNam")).Text.Trim();
DropDownList drop = (DropDownList)e.Row.FindControl("dropCgyNam"); // 找不到dropCgyNam
Function.BindDropDownList(drop, "nam", "id", "类别");
//drop.Items.FindByText(sCgyNam).Selected = true;
}
}
解决方案 »
- 第一次写自定义控件还真是无奈,一堆问题。。万能的CSDN啊,指条明路吧,谢谢!
- asp.net注册后自动发送邮件程序
- 请教:想做一个网上审批的网站
- 100分求人给弄下dreamweaver中替换的正则表达式,在线等,急啊
- 一个错误
- [求助] 类型"system.data.oledb.oledbconnection"的值无法转换为"system.data.oledb.oledbconnection的1维数组"
- 急求可以输入任何字符但只能输10位的正则表达式
- aspx前台.aspx打包dll的问题,高手请进100分相送
- xml读取问题(vb.net)
- 您认为目前哪几本书是学习ASP。NET的那教材?
- 菜鸟问曰:Response.Cookies和HttpCookie 有区别吗?
- repeater中textbox的textchanged事件怎么触发?
这个是自己写的一个绑定DropDownList的函数----
关键是怎么获得EditItem中ID为dropCgyNam的DropDownList
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
string sCgyNam = ((Label)e.Row.Cells[1].FindControl("lbCgyNam")).Text.Trim();
DropDownList drop = (DropDownList)e.Row.FindControl("dropCgyNam"); // 找不到dropCgyNam if(drop!=null)
{
Function.BindDropDownList(drop, "nam", "id", "类别");
}
}
}
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
Width="337px" OnRowDataBound="GridView1_RowDataBound" OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
<Columns>
<asp:BoundField DataField="id" HeaderText="车号" />
<asp:TemplateField HeaderText="类型">
<EditItemTemplate>
<asp:DropDownList ID="dropCgyNam" runat="server" Style="position: relative" Width="107px"> <<----------在这里
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="lbCgyNam" runat="server" Text='<%# Bind("nam") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowEditButton="True" />
</Columns>
</asp:GridView>
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
//保存当前行的au_id的值
string au_id = this.GridView1.DataKeys[e.Row.RowIndex]["au_id"].ToString();
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp"); SqlConnection cn = new SqlConnection(@"server=.\SQLExpress;uid=sa;pwd=password;database=pubs");
string strSQL = "select au_id from authors";
SqlCommand cmd = new SqlCommand(strSQL, cn);
cn.Open(); dropTemp.DataSource = cmd.ExecuteReader();
dropTemp.DataTextField = "au_id";
dropTemp.DataBind(); //到DropDownList中根据au_id的值去找需要设置为选中状态的项目,将其设置为选中
ListItem item = dropTemp.Items.FindByText(au_id);
if(item != null)
{
item.Selected = true;
}
cn.Close();
}
}
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");-------这个也找不到EditItem里的DropDownList
DropDownList dropTemp = (DropDownList)e.Row.Cells[1].FindControl("dropTemp");
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
-----
这个是你的我的是:DropDownList drop = (DropDownList)e.Row.Cells[1].FindControl("dropCgyNam");
单步执行完这句后发现drop 为null
但是取不到EditTemplate中的drop
应该是:
(DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpCgyNam")