Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > ASP .Net Web Controls > Responding to LinkButton within a Gridview

Reply
Thread Tools

Responding to LinkButton within a Gridview

 
 
CJM
Guest
Posts: n/a
 
      07-06-2007
[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()



 
Reply With Quote
 
 
 
 
Larry Bud
Guest
Posts: n/a
 
      07-06-2007
On Jul 6, 8:00 am, "CJM" <(E-Mail Removed)> wrote:
> [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.

 
Reply With Quote
 
 
 
 
CJM
Guest
Posts: n/a
 
      07-06-2007

"Larry Bud" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Databind on Linkbutton within GridView frenika ASP .Net 1 03-30-2009 08:44 PM
Responding to LinkButton within a Gridview CJM ASP .Net 2 07-06-2007 04:01 PM
Table within Table not responding to ElementById call Camet Javascript 0 07-06-2006 01:44 PM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net 3 09-08-2005 09:41 AM
Linkbutton does not look like a linkbutton Sathyaish ASP .Net Datagrid Control 1 09-08-2005 08:44 AM



Advertisments