开发者

ASP.NET - GridView - How to programmatically add and read new controls to the cell?

Here is the setup: I programmatically populate my gridview using LINQ to SQL query. Then I enter the edit mode and want to replace some standard TextBox controls with DropDownLists like this:

'excerpt from GridView1_RowEditing

    Dim ddlist1 As New DropDownList
    Dim res1 = From items1 In mydb.Items
             Select items1.Col10

    ddlist1.DataSource = res1
    ddlist1.DataBind()

    GridView1.Rows.Item(0).Cells(1).Controls.Add(ddlist1)

At this 开发者_运维技巧moment I have my gridview showing standard textbox control and new DDList control (in the Column 1). The problem is -- I cant read the value from DDList in the RowUpdating method. It seems like DDList control is not in the GridView1.Rows.Item(0).Cells(1).Controls collection. RowUpdating method sees only standard TextBox control and can read it's value. Any suggestions are welcome. I just don't understand something here :(


Use TemplateField, and then you can locate your dropdown using FindControl

<asp:GridView ID="GridView1" runat="server">
    <Columns>
        <asp:TemplateField>
            <ItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
            </ItemTemplate>
            <EditItemTemplate>
                <asp:DropDownList ID="DropDownList1" runat="server">
                </asp:DropDownList>
            </EditItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

...

Protected Sub GridView1_RowUpdating(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles GridView1.RowUpdating

    Dim ddl = CType(GridView1.Rows(e.RowIndex).Cells(1).FindControl("DropDownList1"), DropDownList)

End Sub
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜