3

I am trying to get a gridview to populate text from the database call to my Label as shown The Results have been tested and are returning the correct names

protected void Page_Load(object sender, EventArgs e)
{
    DataTable t = DBProductLink.ListWithOptions(ProductId, LinkType, null);
    TestList.DataSource = t ;
    TestList.DataBind();
}

The labels are created in the Gridview like this:

<asp:GridView ID="TestList" runat="server" OnRowDataBound="testDataBound" AutoGenerateColumns="false" DataKeyNames="Id">
    <Columns>
        <asp:TemplateField HeaderText="Sizes">
            <asp:ItemTemplate>
                <asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
            </asp:ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

I then tried to loop through the gridview and access the label using the ondatarowbound, however in this it is null.

protected void testDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;

    Label sizeLabel = e.Row.FindControl("sizeLabel") as Label;
    sizeLabel.Text = "test";
}

I am using the exact same set up with 2 drop boxes and 2 labels on another gridview with a different name on the same page which is not having this problem. Anyone got an idea on this? The other Gridview is as follows:

<asp:GridView ID="SearchList" runat="server" AutoGenerateColumns="False"
    DataKeyNames="Id"  OnRowDataBound="SearchList_RowDataBound"
    OnRowCommand="SearchList_RowCommand" Width="100%"  PageSize="20" >
    <Columns>
        <asp:BoundField DataField="Code" HeaderText="Code" SortExpression="Code" />
        <asp:BoundField DataField="Name" HeaderText="Name" SortExpression="Name" />
        <asp:TemplateField HeaderText="Price" SortExpression="Price">
            <ItemTemplate>
                <robo:MoneyLabel ID="MoneyLabel2" runat="server" 
                    Value='<%# Eval("Price") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Type">
            <ItemTemplate>
                <asp:Label ID="typeLabel" runat="server" Text='<%# Eval("Type") %>' />
                <asp:HiddenField ID="productId" runat="server" Value='<%# Eval("Id") %>' />
                <asp:HiddenField ID="isFabric" runat="server" Value='<%# Eval("IsFabric") %>' />
                <asp:HiddenField ID="isOldWizard" runat="server" Value='<%# Eval("IsOldWizard") %>' />
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Options/Color/Size">
            <ItemTemplate>
                <asp:LinkButton runat="server" ID="GetOptions" Text="Get Options" CausesValidation="false" CommandName="Options"  />
                <asp:Label ID="OptionLabel" Visible="false" runat="server" Text="Option: " />
                <asp:DropDownList ID="ProductOptions" runat="server" Visible="false" />
                <asp:Label ID="ColorLabel" Visible="false" runat="server" Text="Color: "  />
                <asp:DropDownList  ID="RibbonColors" runat="server" Visible="false" AutoPostBack="true" />&nbsp;&nbsp;&nbsp;&nbsp;
                <asp:Label ID="SizeLabel" Visible="false" runat="server" Text="Size: " />
                <asp:DropDownList ID="RibbonSizes" runat="server" Visible="false" AutoPostBack="true"  />
            </ItemTemplate>
            <ItemStyle HorizontalAlign="Center" />
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Add">
            <ItemStyle Width="60px" />
            <ItemTemplate>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="Add" CommandName="Add" CommandArgument='<%# Eval("Id") %>' />
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

.cs is

protected void SearchList_RowDataBound(object sender, GridViewRowEventArgs e)
{
    if (e.Row.RowType != DataControlRowType.DataRow)
        return;
    int productId = (int)SearchList.DataKeys[e.Row.RowIndex].Value;
    LinkButton GetOptions = e.Row.FindControl("GetOptions") as LinkButton;
    DropDownList RibbonColors = e.Row.FindControl("RibbonColors") as DropDownList;
    DropDownList  RibbonSizes = e.Row.FindControl("RibbonSizes") as DropDownList;
    DropDownList ProductOptions = e.Row.FindControl("ProductOptions") as DropDownList;
    Label typeLabel = e.Row.FindControl("typeLabel") as Label;
    HiddenField isFabric = e.Row.FindControl("isFabric") as HiddenField;
    HiddenField isOldWizard = e.Row.FindControl("isOldWizard") as HiddenField;

    ProductType typeValue = DBConvert.ToEnum<ProductType>(typeLabel.Text);
    bool isFabricValue = Convert.ToBoolean(isFabric.Value.ToString());
    bool isOldWizardValue = Convert.ToBoolean(isOldWizard.Value.ToString());       
}
nphx
  • 1,426
  • 3
  • 19
  • 30
RussHooker
  • 165
  • 3
  • 3
  • 10
  • The event is called `RowDataBound` not `datarowbound`, apart from that, you have not declared the event handler on the GridView. Sidenote: You should not databind the GridView in page_load on every postback(when ViewState is enabled, default). Instead you should do that only `if(!IsPostBack)`. – Tim Schmelter Jul 19 '12 at 22:27
  • @Tim Thanks Tim I have used a series of attempts with this and various times with different methods such as using the onrowdatabound as well as just doing a straight TestList.DataBind();and the OnRowDataBound="testDataBound". I will edit the post to show the event handler properly any other thoughts? – RussHooker Jul 19 '12 at 22:31
  • Could you paste the markup of the other gridviews that are working properly? – Andre Calil Jul 19 '12 at 22:38
  • @Andre I have posted the other gridview for you, as you can tell I am working with just a test gridview for the error issue I am having. – RussHooker Jul 19 '12 at 22:55
  • I cannot find anything wrong with your code, it should work, from the top of my head, are you sure you are not using the same handler for more than one `GridView`??? – Jupaol Jul 19 '12 at 22:57
  • The `TestList` gridview is set to `AutoGenerateColumns = false` and has no bound columns. I believe that no rows are being generated. Could you change it to `AutoGenerateColumns = true` to see what happens? – Andre Calil Jul 19 '12 at 23:00
  • @Jupaol ya I am sure I even moved the test gridview to a seperate page and ran it solo and had the same problem. I literally have a seperate page with a single gridview and new eventhandlers with test in the name to try this, still getting the same issue. – RussHooker Jul 19 '12 at 23:00
  • @Andre If I set it to true then it does generate all the data correctly all the fields work. I am bringing the data in from a stored procedure which when auto generated fills all columns correctly, but the stored procedure is the same for both gridviews. I am going to have three labels on the gridview once I get this fixed and they will only be visible if they are not null. So I really need these to not autogenerate. Great suggestion on this btw though. – RussHooker Jul 19 '12 at 23:05
  • @SometingkSneeky So, let it `AutoGenerateColumns = false` but create some bounded columns, as you have on the other gridviews. The problem is `AutoGenerateColumns = false` AND no bounded columns, so no row is being created. – Andre Calil Jul 19 '12 at 23:07

1 Answers1

3

I just found the problem

Your markup is wrong it was tricky... I admit it

This tag: <asp:ItemTemplate> should be <ItemTemplate>

Change this:

<asp:TemplateField HeaderText="Sizes">
    <asp:ItemTemplate>
         <asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
    </asp:ItemTemplate>
</asp:TemplateField>

Into

<asp:TemplateField HeaderText="Sizes">
    <ItemTemplate>
         <asp:Label ID="sizeLabel" runat="server" Text='<%# Eval("size") %>' />
    </ItemTemplate>
</asp:TemplateField>

This should raise an exception... but instead, the ItemTemplate was completely ignored

Jupaol
  • 21,107
  • 8
  • 68
  • 100
  • Jupaol can you take a look at my question as well... [it's just giving me x^x of pain and still at square 1...](http://stackoverflow.com/questions/24771214/gridview-findcontrol-issue-due-to-index-out-of-bound-findcontrol-returns-null) – bonCodigo Jul 16 '14 at 03:47