473,396 Members | 2,018 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Responding to LinkButton within a Gridview

CJM
[code snippets at the end]

I have a page that allows the user to search a DB by querying one of 3
fields. When results are returned, I want the user to be able to click a
value in one of three columns (that directly relate to the 3 searchable
fields) and have the page postback and requery given the selected criterion.
For example, the user searches for retainer #1, which has a Tip Width of 5.
So he clicks on the 5 and it searches again for all retainers that have a
similar Tip Width.

To achieve this, I have a Gridview with several BoundFields and one
LinkButton (within a Template) for each of the 3 searcable columns. So far
so good...

I'm strugglin however to make the next move. I don't know how to code it so
that by clicking on a LinkButton, the SearchVal field is repopulated with
the selected value, the SearchType radio button is set to the appropriate
type, and the btnSearch_Click sub is called. I'm not sure which event of
which control I need to create a handler for, nor what I do thereafter.

I imagine this is quite simple, but it's my first proper ASP.NET
application, so I'm still rather out of my depth. I've searched for
examples, but I haven't found one that matches this scenario, though there
have been plenty of hints that this is a commonly used approach.

Can anyone point me in the rifght direction?

Thanks in advance.

CJM

Snippets:

<fieldset>
<legend>Search Criteria</legend>
<table id="search">
<tr>
<td>Search Value:</td>
<td><asp:TextBox ID="SearchVal" TextMode="SingleLine" MaxLength="20"
runat="server" Columns="20" /></td>
<td><asp:RadioButton GroupName="SearchField" ID="rdoRetainer"
Text="Retainer No:" Checked runat="server" /></td>
</tr>
<tr>
<td colspan="2">&nbsp;</td>
<td><asp:RadioButton GroupName="SearchField" ID="rdoTipWidth"
Text="Tip Width:" runat="server" /></td>
</tr>
<tr>
<td>&nbsp;</td>
<td><asp:Button ID="btnSearch" Text="Search" runat="server" /></td>
<td><asp:RadioButton GroupName="SearchField" ID="rdoFlange"
Text="Ideal Flange" runat="server" /></td>
</tr>
</table>
<asp:Label ID="lblFeedback" Text="feedback" runat="server"/>
</fieldset>

<asp:GridView ID="grdRetainers" AutoGenerateColumns="False" runat="server">
<Columns>
<asp:TemplateField HeaderText="Retainer">
<ItemTemplate>
<asp:LinkButton CommandName="ViewRetainer" ID="btnRetainer"
runat="server"><%#Eval("RetNo")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Tip Width">
<ItemTemplate>
<asp:LinkButton CommandName="SearchByTipWidth" ID="btnTipWidth"
runat="server"><%#Eval("TipWidth")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField HeaderText="Thick" DataField="Thickness" />
<asp:BoundField HeaderText="Depth" DataField="Depth" />
<asp:BoundField HeaderText="Style" DataField="Style" />
<asp:BoundField HeaderText="Angle" DataField="InternalAngle" />
<asp:BoundField HeaderText="Dev Width" DataField="DevelopedWidth" />
<asp:TemplateField HeaderText="Flange">
<ItemTemplate>
<asp:LinkButton o OnCommand="" CommandName="SearchByFlange"
ID="btnFlange" runat="server"><%#Eval("IdealFlangeTip")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>

Protected Sub btnSearch_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles btnSearch.Click

Dim oConn As New SqlConnection("..etc...")
Dim oCmd As New SqlCommand
Dim drRetainers As SqlDataReader

oConn.Open()
With oCmd
.Connection = oConn
.CommandType = Data.CommandType.StoredProcedure
.Parameters.Clear()

If rdoRetainer.Checked Then
.CommandText = "mnd_ListRetainersByRetNo"

.Parameters.Add("RetainerNo", Data.SqlDbType.VarChar, 10).Value =
SearchVal.Text
End If
If rdoTipWidth.Checked Then
'lblFeedback.Text = lblFeedback.Text & " 1"
.CommandText = "mnd_ListRetainersByTipWidth"
.Parameters.Add("TipWidth", Data.SqlDbType.VarChar, 10).Value =
SearchVal.Text
End If
If rdoFlange.Checked Then
.CommandText = "mnd_ListRetainersByFlange"
.Parameters.Add("Flange", Data.SqlDbType.VarChar, 10).Value =
SearchVal.Text
End If
End With

drRetainers = oCmd.ExecuteReader
With grdRetainers
'lblFeedback.Text = lblFeedback.Text & " 2"
.DataSource = drRetainers
.DataBind()
.GridLines = GridLines.None
.CellSpacing = 1

End With

oConn.Close()
drRetainers.Close()

Jul 6 '07 #1
2 2479
On Jul 6, 8:00 am, "CJM" <cjmnew...@newsgroup.nospamwrote:
[code snippets at the end]

I have a page that allows the user to search a DB by querying one of 3
fields. When results are returned, I want the user to be able to click a
value in one of three columns (that directly relate to the 3 searchable
fields) and have the page postback and requery given the selected criterion.
For example, the user searches for retainer #1, which has a Tip Width of 5.
So he clicks on the 5 and it searches again for all retainers that have a
similar Tip Width.

To achieve this, I have a Gridview with several BoundFields and one
LinkButton (within a Template) for each of the 3 searcable columns. So far
so good...

I'm strugglin however to make the next move. I don't know how to code it so
that by clicking on a LinkButton, the SearchVal field is repopulated with
the selected value, the SearchType radio button is set to the appropriate
type, and the btnSearch_Click sub is called. I'm not sure which event of
which control I need to create a handler for, nor what I do thereafter.

I imagine this is quite simple, but it's my first proper ASP.NET
application, so I'm still rather out of my depth. I've searched for
examples, but I haven't found one that matches this scenario, though there
have been plenty of hints that this is a commonly used approach.

Can anyone point me in the rifght direction?
First, you need to set the CommandArgument for the button.

Then you need to set a CommandName for the button.

Then in the code behind, you need to capture the RowCommand event for
your Gridview. Your argument will be passed, along with the
commandName. In the codebehind, check to see if it's the proper
commandName (even if you have only 1 command right now, I think it's
good practice to always check, so if you add another command later,
you don't have to recode). Then do your business with populating your
search criteria, setting up your parameters, rebinding, etc.

Hope that gives you an overview.

Jul 6 '07 #2
CJM

"Larry Bud" <la**********@yahoo.comwrote in message
news:11**********************@n2g2000hse.googlegro ups.com...
>
First, you need to set the CommandArgument for the button.

Then you need to set a CommandName for the button.

Then in the code behind, you need to capture the RowCommand event for
your Gridview. Your argument will be passed, along with the
commandName. In the codebehind, check to see if it's the proper
commandName (even if you have only 1 command right now, I think it's
good practice to always check, so if you add another command later,
you don't have to recode). Then do your business with populating your
search criteria, setting up your parameters, rebinding, etc.
Thanks for that... Since I'd posted I'd actually figured this out....
except....

If I set the CommandArgument using EVAL()....

<asp:TemplateField HeaderText="Tip Width">
<ItemTemplate>
<asp:LinkButton CommandName="SearchByTipWidth"
CommandArgument="<%#Eval("TipWidth")%>" ID="btnTipWidth"
runat="server"><%#Eval("TipWidth")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

....it doesn't render correctly. Rather than producing the expected HTML, it
renders the cell as follows:

<td><asp:LinkButton CommandName="SearchByTipWidth" CommandArgument="5.46"
ID="btnTipWidth" runat="server">5.46</asp:LinkButton></td>

If I manually set the CommandArgument to an arbitrary value, e.g....

<asp:TemplateField HeaderText="Retainer">
<ItemTemplate>
<asp:LinkButton CommandName="btnRetainer_Click" CommandArgument="150"
ID="btnRetainer" runat="server"><%#Eval("RetNo")%></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>

....it seems to behave more as expected:

<td><a id="grdRetainers_ctl02_btnRetainer"
href="javascript:__doPostBack('grdRetainers$ctl02$ btnRetainer','')">123</a></td>

Is there some little gem of knowledge that I'm missing here?

Thanks

Chris

Jul 6 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Ram | last post by:
Hi All, I am binding a GridView control using object data source. I have included a linkbutton with in the gridview and using its commandname property , I am passing few values( id and name...
5
by: graphicsxp | last post by:
Hi, I've added a linkbutton field to my gridview. Now I would like that when the user clicks on it for a particular row, a server-side function should be executed, which takes as parameters the id...
3
by: Gabe | last post by:
Hi there, The other day I tried to programmatically create an asp.net linkbutton during my Page_Load() event and attach a click event handler to the linkbutton, then add that linkbutton control...
0
by: jm | last post by:
I have a Gridview with a LinkButton. The button has text from my database. It is not the datakey of the database, just free form text. I want to populate another gridview based upon the data in...
13
by: rn5a | last post by:
In a shopping cart app, suppose a user has placed 5 orders, I want to show him 5 LinkButtons (one for each order) so that when he clicks the first LinkButton, he would be shown the details of his...
3
by: Ben | last post by:
Hi, i try to reach in code-behind a linkbutton embedded into an ItemTemplate element of a gridview. But i'm stuck .... <Columns> <asp:TemplateField><ItemTemplate> <asp:LinkButton ID="lb1"...
5
by: Gabe Matteson | last post by:
I would like the linkbutton when it is clicked to exectute a delete sql statement on the current page. etc. onclick="btndelete_onclick" how do i get the linkbutton to pass the database id...
4
by: Jeff | last post by:
Hi, I have a ASP.NET 2.0 Web Application. Many of the pages use the ASP.NET GridView with paging and sorting. One of the columns of this Gridview is a template column (LinkButton). The data being...
5
by: Eric | last post by:
Hi, there are several gridviews all with a linkbutton which all trigger the same procedure. I need to know from which gridview the linkbutton has been clicked. Thanks Eric Here the code:
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.