Wrox Programmer Forums
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old December 15th, 2012, 05:28 PM
Registered User
 
Join Date: Dec 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default Gridview output display

i have a gridview. i want to put value in one of my grid view cell in row updating method. i tried the following code. it does not give any error but it also does not display the output in label2 which in cell[7] of gridview.

here is my gridview code
Code:
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataKeyNames="EmpID" DataSourceID="SqlDataSource1"
            onrowupdating="GridView1_RowUpdating">
            <Columns>
                <asp:BoundField DataField="EmpID" HeaderText="EmpID" InsertVisible="False"
                    ReadOnly="True" SortExpression="EmpID" />
                <asp:BoundField DataField="EmpName" HeaderText="EmpName"
                    SortExpression="EmpName" />
                <asp:BoundField DataField="Department" HeaderText="Department"
                    SortExpression="Department" />
                <asp:BoundField DataField="Address" HeaderText="Address"
                    SortExpression="Address" />
                <asp:BoundField DataField="Age" HeaderText="Age" SortExpression="Age" />
                <asp:BoundField DataField="************" HeaderText="************" SortExpression="************" />
                <asp:TemplateField HeaderText="ProjectName" SortExpression="ProjectName">
                    <EditItemTemplate>
                        <asp:DropDownList ID="DropDownList1" runat="server" AutoPostBack="True"
                            SelectedValue='<%# Bind("ProjectName") %>'>
                            <asp:ListItem>1</asp:ListItem>
                            <asp:ListItem>2</asp:ListItem>
                            <asp:ListItem>3</asp:ListItem>
                            <asp:ListItem>4</asp:ListItem>
                            <asp:ListItem></asp:ListItem>
                        </asp:DropDownList>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("ProjectName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:TemplateField HeaderText="SupervisorName" SortExpression="SupervisorName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("SupervisorName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label2" runat="server" Text='<%# Bind("SupervisorName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:CommandField ShowEditButton="True" />
            </Columns>
        </asp:GridView>

and here is my row updating method code

Code:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        DropDownList ddl = (DropDownList)GridView1.Rows[e.RowIndex].FindControl("DropDownList1");
        string selectedvalue = ddl.SelectedItem.ToString();
     
    switch (selectedvalue)
        {
            
            case "1":
               ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text = "A";
                break;
            case "2":
               ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text = "B";
                break;
            case "3":
               ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text = "C";
                break;
            case "4":
             ((Label)GridView1.Rows[e.RowIndex].FindControl("Label2")).Text = "D";
                break;
            
        }
}
i don't understand why the output is not displaying in label2 which is in cell[7].
please help
 
Old February 26th, 2015, 09:54 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

Based on your behind code I would not be surprised if you were not experiencing null reference exceptions. The find control method unfortunately will not find controls that are not direct descendants of the root; for finding controls that are rooted deeper in the control stack you need create a new method that will search grand children as well. Check out this post

Checkbox in GridView

Last edited by mmorgan30; February 26th, 2015 at 09:56 PM..
 
Old March 3rd, 2015, 12:45 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

This is happening because the RowDataBound event runs AFTER the RowUpdating event. You have to change the lable(s) in the RowDataBound event
 
Old March 3rd, 2015, 08:29 PM
Friend of Wrox
 
Join Date: Feb 2014
Posts: 136
Thanks: 1
Thanked 10 Times in 10 Posts
Default

That's not exactly true. Unless the DataBind is called then RowDataBond event will not fire. If you want to update the grid based a selection from a drop down list in the grid. You would either 1 update it during the on selected index change of the drop down list or the update command of the grid. Since in the on updating event he is not calling DataBind on the grid the RowDataBound even would not fire prior to rendering back to the client.
 
Old March 4th, 2015, 10:31 AM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,348
Thanks: 0
Thanked 5 Times in 5 Posts
Default

Yes you are correct, unless databind is called, the RowDataBound will not fire. However, I cannot ever think of a time when I did an update from the gridview and then did NOT refresh the grid. When would you not reset the index and rebind? I can't think of an example.

I assumed that he just did not post all of the code including the databind.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to display the GridView. kcsathish ASP.NET 2.0 Basics 1 July 23rd, 2008 04:08 AM
GridView doesn't display bnorg ASP.NET 2.0 Professional 1 December 1st, 2006 10:14 PM
Can display output as Element name HareshVani XSLT 2 March 22nd, 2006 03:19 AM
Cannot display/return SQL Query Output from a Vari sjm SQL Server DTS 1 September 9th, 2005 11:40 AM
Display a msg when waiting 4 output from database Sach Classic ASP Basics 2 April 29th, 2004 08:24 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.