GRIDVIEW:綁定、查找<EditTemplate>中DropDownList
前台:
<asp:TemplateField HeaderText="新上司">
<EditItemTemplate>
<asp:DropDownList ID="editN_PERSON_NO" runat="server" TabIndex="-1" CssClass="not-null" Width="200px" AutoPostBack="True" OnSelectedIndexChanged="editN_PERSON_NO_SelectedIndexChanged" >
</asp:DropDownList>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="itemN_PERSON_NO" runat="server" Text='<%# Bind("N_PERSON_NO") %>' TabIndex="-1"></asp:Label>
</ItemTemplate>
<ItemStyle Width="250px" CssClass="GVCellStyle" />
<HeaderStyle Width="250px" />
</asp:TemplateField>
=====================================
绑定DDL控件:
protected void GridView1_RowCreated(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowState == DataControlRowState.Edit || e.Row.RowState == (DataControlRowState.Alternate | DataControlRowState.Edit))
{
DropDownList ddl = (DropDownList)e.Row.FindControl("editN_PERSON_NO");
if (ddl != null)
{
DataTable dt = pacc.GetDDLPerson120();
WebUtil.FillDropDownList(ddl, dt, true);
}
}
}
==============================
点"编辑"按钮后,给绑定的DDL控件赋值:
//刚上有在DDL标签内加上SelectValue=<%#EVAL("PERSON_NO") %>, 再重新绑定GRIDVIEW,我没用成功,可能方法不对.
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
Label userId = (Label)this.GridView1.Rows[e.NewEditIndex].FindControl("userId");
Label pno = (Label)this.GridView1.Rows[e.NewEditIndex].FindControl("itemN_PERSON_NO");
DropDownList ddl = (DropDownList)this.GridView1.Rows[e.NewEditIndex].FindControl("editN_PERSON_NO");
if(ddl != null && pno !=null)
{
ddl.SelectedValue = pno.Text;
}
}
//可以用下面事件来给当前行的别的控件从DDL中获取值
protected void editN_PERSON_NAME_SelectedIndexChanged(object sender, EventArgs e)
{
Label lblPNO = (Label)this.GridView1.Rows[this.GridView1.EditIndex].FindControl("editPNO");//這是同行的其它列的控件
DropDownList ddl = (DropDownList)sender;
if (lblPNO != null || ddl != null)
{
lblPNO.Text = ddl.SelectedValue;
}
}
--------------------------------------------------
-猫不在口袋里不要谈猫.