Getting at buttonField text in a gridview

G

GaryDean

I have a button field in a gridview defined as follows:
<asp:ButtonField ButtonType="Button"
CommandName="AcceptOrder" Text="Accept Whole Order">
<ControlStyle Font-Size="X-Small" />
</asp:ButtonField>

In the RowCommand event I have the following code:
if ((string)e.CommandName.ToLower() == "acceptorder")
{
int myRow = int.Parse(e.CommandArgument.ToString());
WebControl myBF =
(WebControl)gvOrders.Rows[myRow].Cells[acceptbutton].Controls[0];

I'm trying to get ahold of the control to test for what the text value is.
When myBF is displayed in the watch window it value is { Text="Accept Whole
Order }

But, I can't find that text value anywhere in the object. There is not text
values anywhere.

How can I find the text (and change it)?
 
S

Steven Cheng[MSFT]

Hello Gary,

As for ButtonField, since it has been well encapsulated and the internal
control structure and naming rule is not documented, it will be
inconvenient to access nested controls in it.(We have to rely on control
index which is always not safe). I would always recommend you consider
converting it to template field if possible.

If you do need to access the Button and property values of the inner button
of ButtonField, you can use the control index. One means I usually use is
turn on the trace on page and view the control structure on the page, this
will help detect the correct control hierarchy on ASP.NET page.

For the Button instance, it is of "DataControlButton" or
"DataControlLinkButton" type based on the "ButtonType" you configured in
the ButtonField. You can simply use their super class(Button and
LinkButton) to reference them. Here is some code snippet demonstrate on
this:
==============================
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs
e)
{
if (e.CommandName == "MyCommand")
{
Response.Write("<br/>Argument: " + e.CommandArgument + ",
source: " + e.CommandSource);

int index = int.Parse((string)e.CommandArgument);

Control control = GridView1.Rows[index].Cells[3].Controls[0];
string text = string.Empty;
if (((ButtonField)GridView1.Columns[3]).ButtonType ==
ButtonType.Link)
{
LinkButton btn = control as LinkButton;
text = btn.Text;
}
else
{
Button btn = control as Button;
text = btn.Text;
}


Response.Write("<br/>control: " + text);

}
}
====================================

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no rights.
 
G

GaryDean

Steven,
It turns out that if I just cast it as a "Button" everything works fine
because myButton.text has the text.

--
Regards,
Gary Blakely
Steven Cheng said:
Hello Gary,

As for ButtonField, since it has been well encapsulated and the internal
control structure and naming rule is not documented, it will be
inconvenient to access nested controls in it.(We have to rely on control
index which is always not safe). I would always recommend you consider
converting it to template field if possible.

If you do need to access the Button and property values of the inner
button
of ButtonField, you can use the control index. One means I usually use is
turn on the trace on page and view the control structure on the page, this
will help detect the correct control hierarchy on ASP.NET page.

For the Button instance, it is of "DataControlButton" or
"DataControlLinkButton" type based on the "ButtonType" you configured in
the ButtonField. You can simply use their super class(Button and
LinkButton) to reference them. Here is some code snippet demonstrate on
this:
==============================
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs
e)
{
if (e.CommandName == "MyCommand")
{
Response.Write("<br/>Argument: " + e.CommandArgument + ",
source: " + e.CommandSource);

int index = int.Parse((string)e.CommandArgument);

Control control = GridView1.Rows[index].Cells[3].Controls[0];
string text = string.Empty;
if (((ButtonField)GridView1.Columns[3]).ButtonType ==
ButtonType.Link)
{
LinkButton btn = control as LinkButton;
text = btn.Text;
}
else
{
Button btn = control as Button;
text = btn.Text;
}


Response.Write("<br/>control: " + text);

}
}
====================================

Hope this helps some.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead



==================================================

Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.



Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.

==================================================



This posting is provided "AS IS" with no warranties, and confers no
rights.
 
S

Steven Cheng[MSFT]

Thanks for your followup Gary,

Yes, if your ButtonField is configured as "Push Button" style, you can cast
the button into a standard "Button" class instance since that is the super
class. Anyway, glad that you've made it working. If you have any further
questions, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


This posting is provided "AS IS" with no warranties, and confers no rights.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,113
Latest member
Vinay KumarNevatia
Top