使用EditItemTemplate在Gridview中进行DropDown [英] DropDown in Gridview with EditItemTemplate

查看:87
本文介绍了使用EditItemTemplate在Gridview中进行DropDown的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Gridview中使用EditItemTemplate绑定一个下拉列表。它可以与ItemTemplate一起使用。



这里是代码。



I want to bind a dropdown inside a gridview with EditItemTemplate .It works fine with ItemTemplate .

here is the code.

<asp:GridView ID="gvUser" runat="server" AutoGenerateColumns="false"

                                DataKeyNames="UserId" >
                                <Columns>
                                    <asp:TemplateField>
                                        <ItemTemplate>
                                            <asp:DropDownList ID="ddlUsers" runat="server"  DataSource="<%# BindUsers() %>" DataTextField="UserName" DataValueField="UserId" SelectedValue='<%# Eval("UserId").ToString() %>'>
                                            </asp:DropDownList>
                                            </ItemTemplate>
                                    </asp:TemplateField>
                                </Columns>
                            </asp:GridView>









受保护的DataTable BindUsers()

{



DataSet dsUser = getUser();



返回dsUser.Tables [0];

}



它无效如果我使用EditItemTemplate而不是ItemTemplate。可能是什么原因?



谢谢..





protected DataTable BindUsers()
{

DataSet dsUser = getUser();

return dsUser.Tables[0];
}

it is not working if I use EditItemTemplate instead of ItemTemplate. What can be the reason?

Thanks..

推荐答案



这个概念非常简单。当网格处于正常模式时,将考虑 ItemTemplate ,并且 EditItemTemPlate 编辑模式时,将考虑$ c>。因此,如果将下拉列表放在EditItemTemPlate中,则必须在网格处于编辑模式时绑定下拉列表。即,您应该绑定在网格的 RowDataBound事件中,并检查如下所示的条件:

Hi,
The concept is very simple. ItemTemplate will be considered when the grid is in normal mode, and EditItemTemPlate will be considered when the grid is in edit mode. So, if you place the dropdown list in EditItemTemPlate, then you must bind the dropdownlist when the grid is in edit mode. i.e, you should bind that in RowDataBound Event of the gird and checking the condition like below:
if (e.Row.RowState == DataControlRowState.Edit)
{
   //bind the dropdown here
   //may be call the function BindUsers for binding the dropdown list.
}





希望它有所帮助。



hope it helps.


这篇关于使用EditItemTemplate在Gridview中进行DropDown的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆