Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Professional
|
ASP.NET 2.0 Professional If you are an experienced ASP.NET programmer, this is the forum for your 2.0 questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Professional 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 January 4th, 2008, 11:13 AM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default Dynamic changing value of a gridview button text

Hi all,
I'm having problems dynamically setting the value of a grideview button text. Basically, I'd like for it to say somthing like "Edit John's details"

I've hooked up the gridview to a dictionary and can display the data fine. However, if I try and change the value of the button text, based on the value in a cell (which I know contains data because it displays it at runtime), I get a blank value back from the cell. Here's my code:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
        <Columns>
        <asp:TemplateField HeaderText="Name">
            <ItemTemplate>
            <%#Container.DataItem.Name%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Surname">
            <ItemTemplate>
            <%#Container.DataItem.Surname%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Town">
            <ItemTemplate>
            <%#Container.DataItem.Town%>
            </ItemTemplate>
        </asp:TemplateField>
            <asp:TemplateField HeaderText="Dept">
            <ItemTemplate>
            <%#Container.DataItem.Dept%>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField>
        <ItemTemplate>
            <asp:Button ID="Button1" runat="server" Text="Button" CommandName="EditBtn" CommandArgument='<%# Container.DataItem.ID %>' />
        </ItemTemplate>
        </asp:TemplateField>
        </Columns>
        </asp:GridView>

Here's my code-behind code:

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If Not Page.IsPostBack Then
            Me.GridView1.DataSource = DataSrc().Values
            Me.GridView1.DataBind()
        End If

    End Sub

    Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated
        If e.Row.RowType = DataControlRowType.DataRow Then
            Dim btn As Button = CType(e.Row.Cells(4).Controls(1), Button)
            btn.Text = e.Row.Cells(2).Text
        End If
    End Sub

The button is found fine, but when I try and do btn.Text = e.row.Cells(2).text, the value returned from the cell is ""? There's definitely data in cell(2) by the way as the gridview displays it perfectly. I've even tried it using the RowDataBound event with no luck. Any help would be appreciated....
Thank you.
 
Old January 4th, 2008, 12:00 PM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

I've just realised that I can set the text property on the textbox to this:

<asp:Button ID="Button1" runat="server" Text='<%# Container.DataItem.Name + chr(39) +"s details" %>' CommandName="EditBtn" CommandArgument='<%# Container.DataItem.ID %>' />

If anyone has a solution as to how you can dynamically change the text of a button in a gridview in the code behind, I'd be greatful to know how.

I'd also like to know how you can change gridview cell content dynamically in the GridView1_RowCreated event e.g:

e.row.cells(0).text = e.row.cells(1) + " some text added on"

I can't get this to work either.
Thanks again.
 
Old January 5th, 2008, 02:32 PM
Authorized User
 
Join Date: Sep 2007
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

A little code behind example...

Add DataBinding handler for Button control.

.aspx file
Code:
<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" CommandName="EditBtn" 
            CommandArgument='<%# Container.DataItem.ID %>' DataBinding="EditBtn_OnDataBinding" />
    </ItemTemplate> 
</asp:TemplateField>   
In code behind class use DataBinder to extract value from data item.

Code:
protected void EditBtn_OnDataBinding(object sender, EventArgs e)
{
    Button btn = sender as Button;
    GridViewRow container = btn.NamingContainer as GridViewRow;
    btn.Text = DataBinder.Eval(container, "Name");
}
You can do the same in aspx page like this:

Code:
<asp:TemplateField>
    <ItemTemplate>
        <asp:Button ID="Button1" runat="server" CommandName="EditBtn" 
            CommandArgument='<%# Container.DataItem.ID %>'
            Text='<%# Eval("Name") %>' />
    </ItemTemplate> 
</asp:TemplateField>   

EDIT: Same works with Label control of course (answer to your second question)
 
Old January 6th, 2008, 06:18 PM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Thanks for posting. I'm not sure if I was clear in the problem I was having in the code-behind.

I've solved the problem using what I posted above. But what I can't work out is why in the code behind solution I've tried, can't retreive values from a row when I fire the row created event.....

If the cell contains a button control I access the control by casting, but if the cell contains just plain text, I can't seem to extract the value, e.g. e.Row.Cells(2).Text does not return a value when I fire the row_created event.

When I run the following event, the button casts fine, but the cell (e.Row.Cells(2).Text) does not contain a value even though I know it does. It just returns "".

Protected Sub GridView1_RowCreated(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowCreated

If e.Row.RowType = DataControlRowType.DataRow Then
  Dim btn As Button = CType(e.Row.Cells(4).Controls(1), Button)
  btn.Text = e.Row.Cells(2).Text
End If

End Sub

I can't work out why?
Thank you in advance

 
Old January 6th, 2008, 09:43 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

Hi there..

where the cell contents is a text, in fact it has a label control if I don't remember wrong?? did you try it??

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========
 
Old January 7th, 2008, 05:52 AM
Authorized User
 
Join Date: Aug 2006
Posts: 41
Thanks: 1
Thanked 0 Times in 0 Posts
Default

Hi Gonzalo,
thanks you - I now understand what you mean! It uses a DataBoundLiteralControl not a Literal though, but you made me realise what I needed to do.

For some reason the DataBoundLiteralControl contains some extra characters, so I had to trim it Here's my solution :

Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles GridView1.RowDataBound

If e.Row.RowType = DataControlRowType.DataRow Then
  Dim btn As Button = CType(e.Row.Cells(4).Controls(1), Button)
  Dim lit As DataBoundLiteralControl = CType(e.Row.Cells(1).Controls(0), DataBoundLiteralControl)
  btn.Text = lit.Text.Trim
End If

End Sub


Thanks for all your help.
:)
 
Old January 7th, 2008, 07:39 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,189
Thanks: 5
Thanked 59 Times in 57 Posts
Send a message via MSN to gbianchi
Default

no problem.. glad we could help...

HTH

Gonzalo

================================================== =========
Read this if you want to know how to get a correct reply for your question:
http://www.catb.org/~esr/faqs/smart-questions.html
^^Took that from dparsons signature and he Took that from planoie's profile
================================================== =========
My programs achieved a new certification (can you say the same?):
WORKS ON MY MACHINE
http://www.codinghorror.com/blog/archives/000818.html
================================================== =========
I know that CVS was evil, and now i got the proof:
http://worsethanfailure.com/Articles...-Hate-You.aspx
================================================== =========





Similar Threads
Thread Thread Starter Forum Replies Last Post
To get text of Button Field column in GridView nitinp ASP.NET 2.0 Basics 1 November 3rd, 2006 10:57 AM
Changing row position of gridview at runtime rameshsamiappan ASP.NET 2.0 Basics 0 September 25th, 2006 10:02 AM
MySQL table not changing with Update in Gridview enfleshjsc BOOK: Professional ASP.NET 2.0 and Special Edition; ISBN: 978-0-7645-7610-2; ISBN: 978-0-470-04178-9 1 May 19th, 2006 08:31 AM
Changing between bold and plain text in a text box funkybuddha Access 2 January 3rd, 2006 10:15 AM
Changing format for Dynamic Text . joshil Access ASP 1 April 20th, 2004 08:19 AM





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