Gridview: embedding a texbox and button in each row, passing info from that row to a function?

K

Ken Fine

I'm making an administrative interface that lists records in a GridView.

For *each* row in the gridview, I would there to be two interface elements
in addition to some information associated with the record. Those interface
elements are a Button and a TextBox. The idea is that the administrator
using the interface will fill in an e-mail address and press the button if
they want to send that particular record to someone by e-mail. (I know how
to manage all of the e-mailing stuff.)

I know how to do a variation of this using a commandname on a simple link in
a repeater, but I have never done this with a textbox.

I've made many interfaces similar to what's described above using Classic
ASP.

How would I do this in a GridView? I imagine I'd build it using an
ItemTemplate. But I'm fuzzy on how to make the UI elements repeatable, how
to determine which row has been invoked, and how to grab the necessary
information out of the dynamically rendered/named UI elements or otherwise
pass the necessary info to the function I build to invoke the e-mailing
functionality.

Thanks,
-KF
 
S

Steven Cheng [MSFT]

Hi KF,

thanks for posting and sharing us the information.

I haven't an account on thet www.experts-exchange.com/ so that I cannot see
that implementation. Here are some of my thoughts on this for your
information:

1. Use a TemplateField as an additional column in the GridView and put
Email TextBox and Button in that Templatefield's ItemTemplate. e.g.

===============
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSendEmail" runat="server"
Text="SendTo: "
CommandName="Send" CommandArgument='<%#
((GridViewRow)Container).RowIndex %>' />
<asp:TextBox ID="txtEmail"
runat="server"></asp:TextBox>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
===============

2. We need to set CommandName and CommandArgument properties for the Button
in template. So that we can reference it in the Gridview's RowCommand event
later. Register the "RowCommand" event of GridView.

3. Here we add the code in "Rowcommand" event. We first ensure that the
CommandName is "Send", and then, get RowIndex from CommandArgument.
And Find controls from the GridViewRow and do the email sending stuff ...


=====================
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if(e.CommandName=="Send")
{
int rowindex = int.Parse(e.CommandArgument.ToString());

GridViewRow row = GridView1.Rows[rowindex];

Response.Write("<br/>Row: " + rowindex);

TextBox txt = row.FindControl("txtEmail") as TextBox;

Response.Write("<br/>Will send email to " + txt.Text);


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

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

==================================================
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.


--------------------
From: "Ken Fine" <[email protected]>
References: <[email protected]>
Subject: solution Re: Gridview: embedding a texbox and button in each row,
passing info from that row to a function?
 
S

Steven Cheng [MSFT]

Hi KF,

How are you doing? Does the suggestion in my last reply help?
If there is any further question on this, please feel free to post here.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead


Delighting our customers is our #1 priority. We welcome your comments and
suggestions about how we can improve the support we provide to you. Please
feel free to let my manager know what you think of the level of service
provided. You can send feedback directly to my manager at:
(e-mail address removed).

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

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


--------------------
From: (e-mail address removed) (Steven Cheng [MSFT])
Organization: Microsoft
Date: Wed, 09 Jul 2008 02:55:30 GMT
Subject: RE: solution Re: Gridview: embedding a texbox and button in each
row, passing info from that row to a function?
Hi KF,

thanks for posting and sharing us the information.

I haven't an account on thet www.experts-exchange.com/ so that I cannot see
that implementation. Here are some of my thoughts on this for your
information:

1. Use a TemplateField as an additional column in the GridView and put
Email TextBox and Button in that Templatefield's ItemTemplate. e.g.

===============
<asp:TemplateField>
<ItemTemplate>
<asp:Button ID="btnSendEmail" runat="server"
Text="SendTo: "
CommandName="Send" CommandArgument='<%#
((GridViewRow)Container).RowIndex %>' />
<asp:TextBox ID="txtEmail"
runat="server"></asp:TextBox>

</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
===============

2. We need to set CommandName and CommandArgument properties for the Button
in template. So that we can reference it in the Gridview's RowCommand event
later. Register the "RowCommand" event of GridView.

3. Here we add the code in "Rowcommand" event. We first ensure that the
CommandName is "Send", and then, get RowIndex from CommandArgument.
And Find controls from the GridViewRow and do the email sending stuff ...


=====================
protected void GridView1_RowCommand(object sender,
GridViewCommandEventArgs e)
{
if(e.CommandName=="Send")
{
int rowindex = int.Parse(e.CommandArgument.ToString());

GridViewRow row = GridView1.Rows[rowindex];

Response.Write("<br/>Row: " + rowindex);

TextBox txt = row.FindControl("txtEmail") as TextBox;

Response.Write("<br/>Will send email to " + txt.Text);


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

Hope this also helps.

Sincerely,

Steven Cheng

Microsoft MSDN Online Support Lead
 

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

Staff online

Members online

Forum statistics

Threads
473,992
Messages
2,570,220
Members
46,805
Latest member
ClydeHeld1

Latest Threads

Top