I know this is old hat, but after recently having a conversation with one of the ASP.NET Team guys about how easy Web Forms is to use, I thought I’d just post a simple example of that.  Here is the scenario.  You have a simple GridView with just three columns (Select,Id,TagName).  You want to be able to capture what happens when the user pressed the Select command and capture the Id.  It looks like this:

 

image

 

And has an aspx page like this:

 

<asp:GridView ID="GridViewNotSelected" runat="server" AllowSorting="True"         AutoGenerateColumns="False"         DataSourceID="SqlDataSourceTagsNotSelected"         onrowcommand="GridViewNotSelected_RowCommand">    <Columns>        <asp:TemplateField ShowHeader="False">            <ItemTemplate>                <asp:LinkButton ID="LinkButton1" runat="server"                     CausesValidation="False" CommandArgument='<%# Eval("Id") %>'                    CommandName="Select" Text="Select"></asp:LinkButton>            </ItemTemplate>        </asp:TemplateField>        <asp:TemplateField             HeaderText="&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;">        </asp:TemplateField>        <asp:TemplateField>            <ItemTemplate>                <asp:Label ID="Label1" Text='<%# Eval("TagName") %>' runat="server"></asp:Label>            </ItemTemplate>        </asp:TemplateField>    </Columns></asp:GridView>

Notice that in the TemplateField there is LinkButton with a CommandArgument that simply extracts the TagName with the current Id of the SqlDataSource.  Now, all you need is the RowCommand event and you are done.  Here it is in C#.

 

protected void GridViewNotSelected_RowCommand(object sender, GridViewCommandEventArgs e){    int tagsId = Convert.ToInt32(e.CommandArgument);}

And finally, the proof!

 

image

 

Very very simple.  HTH’s.

So, this is kind of embarrassing, that it took me a while to figure this out.  I have not been doing pure asp.net server control programming for a while, but I figure since it took me a while, maybe there is someone else in the same boat.

So, you have a GridView or DetailsView that has standard “Edit” “Update” “New” type command buttons on them.  The way they get there is by having the declaration something like this:

(more…)


© 2012 PeterKellner.net. All Rights Reserved
Follow

Get every new post delivered to your Inbox

Join other followers: