请问怎样对GridView模板列EditItem中的DropDownList进行数据绑定

anison 2007-05-12 01:30:38
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;

}
}
...全文
502 16 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
nychanglingfeng 2008-10-01
  • 打赏
  • 举报
回复
可要细心呀!
DEC_RULA 2008-07-19
  • 打赏
  • 举报
回复
楼主,对不起,我看错事件了。
DEC_RULA 2008-07-19
  • 打赏
  • 举报
回复
当然访问不到
应该是:
(DropDownList)GridView1.Rows[e.RowIndex].FindControl("drpCgyNam")
anison 2007-05-21
  • 打赏
  • 举报
回复
问题是可以访问ItemTemplate中的drop
但是取不到EditTemplate中的drop
anison 2007-05-18
  • 打赏
  • 举报
回复
TO:amandag(高歌)
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
-----
这个是你的

我的是:DropDownList drop = (DropDownList)e.Row.Cells[1].FindControl("dropCgyNam");
单步执行完这句后发现drop 为null
amandag 2007-05-18
  • 打赏
  • 举报
回复
你要学会看位置啊

你的第一个是 <asp:BoundField DataField="id" HeaderText="车号" />

所以现在就是
DropDownList dropTemp = (DropDownList)e.Row.Cells[1].FindControl("dropTemp");
lowrain 2007-05-17
  • 打赏
  • 举报
回复
这几天被这个困扰了,绑定
anison 2007-05-17
  • 打赏
  • 举报
回复
TO:amandag(高歌)
//对DropDownList做数据绑定
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");

-------

这个也找不到EditItem里的DropDownList
amandag 2007-05-16
  • 打赏
  • 举报
回复
你关键的问题在这里,没有指明单元格
DropDownList dropTemp = (DropDownList)e.Row.Cells[0].FindControl("dropTemp");
amandag 2007-05-16
  • 打赏
  • 举报
回复
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
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();
}
}
yuanzhihua520 2007-05-16
  • 打赏
  • 举报
回复
up
anison 2007-05-14
  • 打赏
  • 举报
回复
帮帮忙~~谢谢
anison 2007-05-13
  • 打赏
  • 举报
回复
好伤心啊 ~哪位高手帮忙解答一下吧?
anison 2007-05-12
  • 打赏
  • 举报
回复
aspx:
<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>
smile9961 2007-05-12
  • 打赏
  • 举报
回复
把你的.aspx中这个模版列的定义也贴出来,另外:
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", "类别");
}
}
}
anison 2007-05-12
  • 打赏
  • 举报
回复
Function.BindDropDownList(drop, "nam", "id", "类别");
这个是自己写的一个绑定DropDownList的函数

----
关键是怎么获得EditItem中ID为dropCgyNam的DropDownList

62,243

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧