![]() |
|
|
|
#1 |
|
Hi all,
I am trying to have a linkbutton control with the commandname property set. However, when I invoke it, the CommandArgument property ends up coming to be blank. However, if I click on the buttonfield control i get back the rowindex in CommandArgument. Why is that? How do I get my custom linkbutton to work? public void GridView_Command(object sender, GridViewCommandEventArgs e) { switch (e.CommandName) { case "EditSelect": Response.Write(e.CommandArgument); Response.End(); GridView_Edit(Convert.ToInt32(e.CommandArgument)); break; } } <asp:ButtonField ButtonType="Link" CommandName="EditSelect" HeaderText="ID" Text="Edit" /> <asp:TemplateField HeaderText="ID" HeaderStyle-HorizontalAlign="left" ItemStyle-HorizontalAlign="left"> <ItemTemplate> <asp:LinkButton ID="btnedit" runat="server" Text='<%#Eval("feeid") %>' CommandName="EditSelect" /> </ItemTemplate> </asp:TemplateField> TIA! |
|
|
|
|
#2 |
|
Posts: n/a
|
Thanks for Suresh's input.
Hi Param, As for the ASP.NET GridView control, it will use the CommandArgument property of Command button(Button or LinkButton) to hold the RowIndex parameter. The built-in CommandField/ButtonField will use the following code to assign the rowindex at Button's creation time: ===reflector diassembled code=== control1.Text = this.Text; control1.CommandName = this.CommandName; control1.CommandArgument = rowIndex.ToString(CultureInfo.InvariantCulture); ==================== Therefore, if you have a custom template field that will add a button control to trigger a GridView event, you can manually set the current rowindex into the button's "CommandArgument" field. e.g. ======in aspx template========== <asp:GridView ID="GridView1" .............. AllowPaging="True" PageSize="4"> <Columns> .................. <asp:TemplateField> <ItemTemplate> <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>">My Select</asp:LinkButton> </ItemTemplate> </asp:TemplateField> ........... ============================= or you can also use RowDataBound event to set the commandargument. e.g. ======in RowDataBound event========== protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { LinkButton btn = e.Row.FindControl("btn1") as LinkButton; if (btn != null) { btn.CommandArgument = e.Row.RowIndex.ToString(CultureInfo.InvariantCultu re); } } } ===================== Hope this helps. Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. Steven Cheng[MSFT] |
|
|
|
#3 |
|
Posts: n/a
|
Thanks Suresh. that worked.
"Suresh" <> wrote in message news:15FC4387-239A-4C38-B565-... >I believe using the CommandArgument to determine the row index only works > when you use the command field in a gridview. > > Not sure if row index is what you are after in the CommandArgument?? > If it is try the following: > > (FYI you can bind any data item value to the commandargument property as > well) > > asp:LinkButton ID="btnedit" runat="server" Text='<%#Eval("feeid") %>' > CommandName="EditSelect" CommandArgument="<%# Container.DataItemIndex %>" > /> > > HTH, > Suresh. > > "" wrote: > >> Hi all, >> >> I am trying to have a linkbutton control with the commandname property >> set. >> However, when I invoke it, the CommandArgument property ends up coming to >> be >> blank. However, if I click on the buttonfield control i get back the >> rowindex in CommandArgument. Why is that? How do I get my custom >> linkbutton >> to work? >> >> public void GridView_Command(object sender, GridViewCommandEventArgs e) >> >> { >> >> switch (e.CommandName) >> >> { >> >> case "EditSelect": >> >> Response.Write(e.CommandArgument); >> >> Response.End(); >> >> GridView_Edit(Convert.ToInt32(e.CommandArgument)); >> >> break; >> >> } >> >> } >> >> <asp:ButtonField ButtonType="Link" CommandName="EditSelect" >> HeaderText="ID" >> Text="Edit" /> >> >> <asp:TemplateField HeaderText="ID" HeaderStyle-HorizontalAlign="left" >> ItemStyle-HorizontalAlign="left"> >> >> <ItemTemplate> >> >> <asp:LinkButton ID="btnedit" runat="server" Text='<%#Eval("feeid") %>' >> CommandName="EditSelect" /> >> >> </ItemTemplate> >> >> </asp:TemplateField> >> >> >> >> TIA! >> >> >> |
|
|
|
#4 |
|
Posts: n/a
|
Hi Param,
DataItemIndex refer to the index of current databound item in the datasource. The value may differ from actual GridView item index when you using paging. the DisplayIndex matchs the actual GridView itemindex no matter you use paging or not. <asp:LinkButton ID="LinkButton1" runat="server" CommandName="Select" CommandArgument="<%# Container.DisplayIndex %>">My Select</asp:LinkButton> Sincerely, Steven Cheng Microsoft MSDN Online Support Lead This posting is provided "AS IS" with no warranties, and confers no rights. Steven Cheng[MSFT] |
|
|
|
#5 | |
|
Junior Member
|
Quote:
Thank you very much for this post, it just solved my problem. bioscom |
|
|
|
|
![]() |
| Thread Tools | Search this Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Gridview row information disappearing after selectedIndexChanged | Ry99 | General Help Related Topics | 0 | 07-13-2009 03:35 PM |
| ASP GridView hyperlinks driving me insane! | WillC999 | General Help Related Topics | 0 | 10-28-2008 11:02 AM |
| gridview inside usercontrol to XLS | tejesvi | Software | 0 | 04-19-2008 08:52 AM |
| Checkbox values problem in gridview | thanigaimani.thirumalai | Software | 0 | 11-09-2007 05:12 AM |
| Update and Delete From Gridview | Usman | Software | 0 | 11-01-2006 10:05 AM |