Multiple Select commands

D

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
 
G

Guest

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: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
 
D

David C

Yes, that helps. Thank you so much.
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: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
 

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

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top