Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Findcontrol withtin Editrow of Gridview , last question promise

    I have a gridview and a dropdown that appears in my custom edit row ( i added it to the edititemtemplate).

    However I cant reference it/ find it. The dropdown is in the first columnd and the below is located within :

    public void rowedit(Object sender, GridViewEditEventArgs e)




    DropDownList s = (DropDownList)GridView1.Rows[e.NewEditIndex].Cells[0].FindControl("ddlPersonCode");
    Response.Write("ID IS " + s.ID);



    This results in a Null reference exception (it hasnt been found) Please any ideas?

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Please post your edit item template

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #3
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    <asp:GridView OnRowUpdating="rowupdate" OnRowEditing="rowedit" OnRowCommand="gridChange" ShowFooter="True" DataKeyNames="ID" ID="GridView1"
    runat="server" AllowPaging="True" AllowSorting="True" DataSourceID="SqlDataSource1"
    CellPadding="4" ForeColor="#333333" GridLines="None" AutoGenerateColumns="False"
    Font-Size="X-Small" Height="356px" Width="2696px" >
    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" Width="10px"
    Wrap="False" />


    <Columns>
    <asp:TemplateField HeaderText="PersonCode" >
    <ItemTemplate>
    <asp:literal ID="Personcodedb" runat="server" Text='<%# Eval("PersonCode") %>' />
    </ItemTemplate>
    <EditItemTemplate>
    <aspropDownList ID="ddlPersonCode" runat="server"
    DataTextField="PersonCode" DataValueField="PersonCode" >
    </aspropDownList>
    </EditItemTemplate>
    </asp:TemplateField>

  • #4
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    Is it generating the edit button in the first column (default)?
    If it is, cell 0 has that, not your item template.

    Edit: never mind, this is in the row editing event - the cell hasn't generated the edit template yet I don't think.
    I'm testing to confirm that notion.

    Edit#2: confirmed with test code.

    Why aren't you binding that drop down? Then you wouldn't have to worry about it...
    Last edited by nikkiH; 11-14-2007 at 03:46 PM.

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • #5
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts
    I was getting very odd behaviour - then suddnely this worked - the good old -1 array fix, thanks for your time dudes


    solved = GridView1.Rows[e.NewEditIndex - 1].Cells[0].FindControl("ddlPersonCode")

  • #6
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    This is full test code, using Northwind, showing binding the drop down list and showing that the event RowEditing is actually still looking at the item template, not edit item template.
    I thought that might be the case when I noticed (breakpoints and tracing) that the GridView did not yet have an edit row set. It was -1.

    ASPX:
    Code:
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FunWithGridView.aspx.cs" Inherits="FunWithGridView" %>
    
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>Grid View Basics</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <h1>Grid View Basics</h1>
        <div>
            <asp:SqlDataSource ID="sqldsProducts" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWIND %>"
                SelectCommand="SELECT Products.ProductID, Products.ProductName, Products.CategoryID, Categories.CategoryName FROM Products INNER JOIN Categories ON Products.CategoryID = Categories.CategoryID ORDER BY Products.ProductName" UpdateCommand="update products set productname=@productname, categoryid=@categoryid where productid=@productid">
                <UpdateParameters>
                    <asp:Parameter Name="productname" />
                    <asp:Parameter Name="categoryid" />
                    <asp:Parameter Name="productid" />
                </UpdateParameters>
            </asp:SqlDataSource>
        
        </div>
            <asp:SqlDataSource ID="sqldsCategories" runat="server" ConnectionString="<%$ ConnectionStrings:NORTHWIND %>"
                SelectCommand="SELECT [CategoryID], [CategoryName] FROM [Categories] ORDER BY [CategoryName]">
            </asp:SqlDataSource>
            &nbsp;
            <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
                DataKeyNames="ProductID" DataSourceID="sqldsProducts" ForeColor="#333333" GridLines="None" OnRowEditing="GridView1_RowEditing">
                <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <Columns>
                    <asp:CommandField ShowEditButton="True" />
                    <asp:BoundField DataField="ProductID" HeaderText="Product ID" InsertVisible="False"
                        ReadOnly="True" SortExpression="ProductID" />
                    <asp:BoundField DataField="ProductName" HeaderText="Product Name" SortExpression="ProductName" />
                    <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" SortExpression="CategoryID"
                        Visible="False" />
                    <asp:TemplateField HeaderText="Category Name" SortExpression="CategoryName">
                        <EditItemTemplate>
                            &nbsp;<asp:DropDownList ID="ddlCategory" runat="server" DataSourceID="sqldsCategories"
                                DataTextField="CategoryName" DataValueField="CategoryID" SelectedValue='<%# Bind("CategoryId") %>'>
                            </asp:DropDownList>
                        </EditItemTemplate>
                        <ItemTemplate>
                            <asp:Label ID="Label1" runat="server" Text='<%# Eval("CategoryName") %>'></asp:Label>
                        </ItemTemplate>
                    </asp:TemplateField>
                </Columns>
                <RowStyle BackColor="#EFF3FB" />
                <EditRowStyle BackColor="#2461BF" />
                <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
                <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
                <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
                <AlternatingRowStyle BackColor="White" />
            </asp:GridView>
        </form>
    </body>
    </html>
    code behind:
    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class FunWithGridView : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
        {
            object o = GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("Label1"); // this works
            object o2 = GridView1.Rows[e.NewEditIndex].Cells[4].FindControl("ddlCategory"); // this doesn't
    
            int i = 0; // put breakpoint here to test
        }
    }

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/

  • Users who have thanked nikkiH for this post:

    hamsterchaos (11-14-2007)

  • #7
    New Coder
    Join Date
    Nov 2007
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Thumbs up

    thanks fo ryour time nikkiH



    infact i just spent an hour readin gyour website and about how to train dogs heh = ) all good stuff, c u around

  • #8
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts


    I found this, and it addresses the issue with handling edit, why it doesn't work, and getting the edit item template controls via the RowCreated event.
    http://forums.asp.net/p/1010076/1661205.aspx

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •