Responding to LinkButton within a Gridview

C

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()
 
L

Larry Bud

[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.
 
C

CJM

Larry Bud said:
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
 

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

Top