Home All Groups Group Topic Archive Search About

Multiple Select commands

Author
29 Jun 2009 8:44 PM
David C
I have an asp.net GridView and I would like to have more than 1 "Select"
button that selects the current row.  I want all of the Select links to do a
PostBack to the current page and then do something based on the
CommandArguement.  Is that possible, and if so, what event do I use to check
the command arguement?  Thanks.

David

Author
2 Jul 2009 2:01 PM
Alexey Smirnov
On Jun 29, 10:44 pm, "David C" <dlch***@lifetimeinc.com> wrote:
> I have an asp.net GridView and I would like to have more than 1 "Select"
> button that selects the current row.  I want all of the Select links to do a
> PostBack to the current page and then do something based on the
> CommandArguement.  Is that possible, and if so, what event do I use to check
> the command arguement?  Thanks.
>
> David
>
> ---------------------------------------------------------------------------­--
> Our Peering Groups change
> Visit :http://spacesst.com/peerin

Hi David,

maybe you can use TemplateField for this. Here's little example

<asp:GridView DataKeyNames="CategoryID" ID="GridView1"
       runat="server" AutoGenerateColumns="False"
       OnRowCommand="GridView1_RowCommand"
       OnRowDataBound="GridView1_RowDataBound"
       OnRowDeleted="GridView1_RowDeleted"
OnRowDeleting="GridView1_RowDeleting">
  <Columns>
   <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
   <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /
>
   <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
       <asp:LinkButton ID="LinkButton1"
         CommandArgument='<%# Eval("CategoryID") %>'
         CommandName="Delete" runat="server">
         Delete</asp:LinkButton>
     </ItemTemplate>
   </asp:TemplateField>
  </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender,
                         GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    l.Attributes.Add("onclick", "javascript:return " +
    "confirm('Are you sure you want to delete this record " +
    DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
  }
}

protected void GridView1_RowCommand(object sender,
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the categoryID of the clicked row
    int categoryID = Convert.ToInt32(e.CommandArgument);
    // Delete the record
    DeleteRecordByID(categoryID);
    // Implement this on your own :)
  }
}

As you can see, you can put as many linkbuttons you need using
TemplateField and get command using
GridViewCommandEventArgs.CommandName

Hope this helps
Are all your drivers up to date? click for free checkup

Author
2 Jul 2009 6:34 PM
David C
Yes, that helps. Thank you so much.
Show quoteHide quote
"Alexey Smirnov" <alexey.smir***@gmail.com> wrote in message
news:cbd00af3-5625-4aff-80d4-aab0b2ee2b75@b15g2000yqd.googlegroups.com...
On Jun 29, 10:44 pm, "David C" <dlch***@lifetimeinc.com> wrote:
> I have an asp.net GridView and I would like to have more than 1 "Select"
> button that selects the current row. I want all of the Select links to do
> a
> PostBack to the current page and then do something based on the
> CommandArguement. Is that possible, and if so, what event do I use to
> check
> the command arguement? Thanks.
>
> David
>
> ---------------------------------------------------------------------------­--
> Our Peering Groups change
> Visit :http://spacesst.com/peerin

Hi David,

maybe you can use TemplateField for this. Here's little example

<asp:GridView DataKeyNames="CategoryID" ID="GridView1"
       runat="server" AutoGenerateColumns="False"
       OnRowCommand="GridView1_RowCommand"
       OnRowDataBound="GridView1_RowDataBound"
       OnRowDeleted="GridView1_RowDeleted"
OnRowDeleting="GridView1_RowDeleting">
  <Columns>
   <asp:BoundField DataField="CategoryID" HeaderText="CategoryID" />
   <asp:BoundField DataField="CategoryName" HeaderText="CategoryName" /
>
   <asp:TemplateField HeaderText="Select">
     <ItemTemplate>
       <asp:LinkButton ID="LinkButton1"
         CommandArgument='<%# Eval("CategoryID") %>'
         CommandName="Delete" runat="server">
         Delete</asp:LinkButton>
     </ItemTemplate>
   </asp:TemplateField>
  </Columns>
</asp:GridView>

protected void GridView1_RowDataBound(object sender,
                         GridViewRowEventArgs e)
{
  if (e.Row.RowType == DataControlRowType.DataRow)
  {
    LinkButton l = (LinkButton)e.Row.FindControl("LinkButton1");
    l.Attributes.Add("onclick", "javascript:return " +
    "confirm('Are you sure you want to delete this record " +
    DataBinder.Eval(e.Row.DataItem, "CategoryID") + "')");
  }
}

protected void GridView1_RowCommand(object sender,
                         GridViewCommandEventArgs e)
{
  if (e.CommandName == "Delete")
  {
    // get the categoryID of the clicked row
    int categoryID = Convert.ToInt32(e.CommandArgument);
    // Delete the record
    DeleteRecordByID(categoryID);
    // Implement this on your own :)
  }
}

As you can see, you can put as many linkbuttons you need using
TemplateField and get command using
GridViewCommandEventArgs.CommandName

Hope this helps

Bookmark and Share