Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Grid View Help 2

Status
Not open for further replies.

robertfah

Programmer
Mar 20, 2006
380
US
Hey all,

I've got a gridview on my page that displays release ntoes for out website. One of the issues I'm having is I have a Download button on each row to be able to download the release note....but if the release note does not contain a path for the file (path info stored in the DB) I do not want the download button to appear. Here's the grid:
Code:
            <asp:Panel ID="pnlFileDownloads" Width="100%" runat="server">
                <asp:GridView ID="gvWebsiteReleaseNotes" DataKeyNames="ReleaseNoteLink" 
                    runat="server" AutoGenerateColumns="false"
                    DataSourceID="objDS" AllowPaging="true" 
                    onrowcommand="gvWebsiteReleaseNotes_RowCommand" 
                    onrowcreated="gvWebsiteReleaseNotes_RowCreated">
                    <Columns>
                        <asp:BoundField DataField="Parameter" Visible="False"  />
                        <asp:ButtonField ButtonType="Button" Text="Download" CommandName="DLFile">
                            <ControlStyle CssClass="GridButtons" />
                        </asp:ButtonField>
                        <asp:TemplateField HeaderText="Release Date" ItemStyle-Wrap="true">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblReleaseDate"><%# DataBinder.Eval(Container.DataItem, "ReleaseDate") %></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>                                                
                        <asp:TemplateField HeaderText="Website Version" ItemStyle-HorizontalAlign="Center" HeaderStyle-HorizontalAlign="Center">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblWebsiteVersion"><%# DataBinder.Eval(Container.DataItem, "WebsiteVersion")  %> </asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                        <asp:TemplateField HeaderText="Brief Description">
                            <ItemTemplate>
                                <asp:Label runat="server" ID="lblDescription"><%# DataBinder.Eval(Container.DataItem, "Description")%></asp:Label>
                            </ItemTemplate>
                        </asp:TemplateField>
                    </Columns>
                </asp:GridView>
                <asp:ObjectDataSource runat="server" ID="objDS" TypeName="SCMaster" SelectMethod="GetWebsiteReleaseNotes" />

I probably have to do something in the OnRowCreated event or similar, but don't know where to being.

Any help is appreicated!
 
Use the RowDataBound event. Check the value of the path, if it doesn't exist, then use findcontrol() to find the button and disable it or make it not visible.
 
I have tried this but because the control is only defined once, The name DLFile is never found when I do a FindControl:

Code:
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.FindControl("DLFile");
            if (b != null)
                b.Visible = false;
        }

???

When I look at the source (in IE) ASP.NET generates a weird name for each "Download" button: DLFile$0, DLFile$1, etc. There's no way that I'd be able to figure out which row index I am on an append it to a literal string and search for that.....there has to be an easier way, right?
 
What you are doing is correct. The problem is that you are using a button field. You would have to figure out the correct syntax for that. I would keep your code and change the buttonfield to a templatefield with a button inside of it.
 
Thanks,

That solved most of the problem...but now, when I put the check in to make it enabled or not, it finds nothing in the cell, but when rendered, there is content in those cells....this is the code:
Code:
    protected void gvWebsiteReleaseNotes_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.FindControl("btnDLFile");
            if (b != null)
                if (e.Row.Cells[5].Text == string.Empty)
                    b.Enabled = false;
        }
    }

If I put e.Row.Cells[5].Text into a string, it shows nothing.....
 
Strange part is that if I do this:
Code:
e.Row.Cells[2].Text == "SOME TEXT"

It writes that out to the cell and I can see it. But when I do an Add Watch on that line (same index) and try to read from it (without setting it) it shows an empty string.
Code:
e.Row.Cells[2].Text
//If I highlight this line when it's on a breakpoint, I see "" in the Text property, when there's really data there

Could this be because the data hasn't been bound to the control yet?
 
text is different than controls. if you have raw text in the cell then the text property will contain this information. however if you use a label, text box, or other server control the text property will be empty. instead you need to get the control within the row and get the text value of the control.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
I put the code in the RowCreated event and it works....the data must not have been bound at the time when it was in the RowDataBound (but one would think the opposite).

Code:
    protected void gvWebsiteReleaseNotes_RowCreated(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            Button b = (Button)e.Row.FindControl("btndlfile");
            if (b != null)
                if (e.Row.DataItemIndex > -1)
                    if (DataBinder.Eval(e.Row.DataItem, "ReleaseNotesLink").ToString() == string.Empty)
                        b.Enabled = false;
        }
    }

Thanks to all that helped....much appreciated.
 
you could also do this in mark up
Code:
<asp:Button ... Enabled='<%#string.IsNullOrEmpty(Eval(DataItem, "ReleaseNotesLink") as string) == false %>' />
something like that anyway, I forget the exact webform syntax.

Jason Meckley
Programmer
Specialty Bakers, Inc.

faq855-7190
faq732-7259
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top