473,395 Members | 1,937 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes and contribute your articles to a community of 473,395 developers and data experts.

Using CommandArgument to find the database table record_id for a datagrid row

nateraaaa
663 Expert 512MB
While working on a recent project I discovered how useful the CommandArgument property can be. I needed to determine the record_id of a row displayed in my datagrid. When the user clicked the Edit ImageButton I needed to redirect the user to the Edit page where the data on the page would be prepopulated with data from the database. I tried several things without success then discovered that the CommandArgument property could be used for this task.

Here is my datagrid
[HTML]<asp:datagrid id="dgReorgs" runat="server" OnSortCommand="dgReorgs_SortCommand" OnPageIndexChanged="dgReorgs_PageIndexChanged"
PagerStyle-HorizontalAlign="Center" PagerStyle-Mode="NumericPages" BorderWidth="0px" CellSpacing="1" CellPadding="2" AllowSorting="True" ShowFooter="True" AutoGenerateColumns="False" PageSize="20"
AllowPaging="True" OnItemCommand="dgReorgs_ItemCommand" PagerStyle-CssClass="PagingLink">
<Columns>
<asp:TemplateColumn>
<HeaderStyle HorizontalAlign="Center"></HeaderStyle>
<ItemStyle HorizontalAlign="Center"></ItemStyle>
<HeaderTemplate>
<asp:ImageButton ID="ibtnAdd" Runat="server" ImageUrl="images/Add.gif" OnClick="ibtnAdd_Click" Visible="False"></asp:ImageButton>
</HeaderTemplate>
<ItemTemplate>
<asp:ImageButton ID="ibtnEdit" Visible="False" Runat="server" ImageUrl="images/Edit.gif" CommandArgument='<%# DataBinder.Eval(Container,"DataItem.record_id")%>' CommandName="Edit">
</asp:ImageButton>&nbsp;
</ItemTemplate>
</asp:TemplateColumn>
<asp:BoundColumn DataField="offer_posting_date" SortExpression="offer_posting_date" HeaderText="Offer Posting Date"
DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="symbol" SortExpression="symbol" HeaderText="Symbol" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="offer_expiration_date" SortExpression="offer_expiration_date" HeaderText="Expiration Date"
DataFormatString="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle HorizontalAlign="Center" BackColor="White"></ItemStyle>
</asp:BoundColumn>
<asp:BoundColumn DataField="notes" SortExpression="notes" HeaderText="Notes" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign="Center" ForeColor="White" BackColor="Navy"></HeaderStyle>
<ItemStyle Width="180px" BackColor="White" Wrap="True"></ItemStyle>
</asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" Mode="NumericPages"></PagerStyle>
</asp:datagrid>[/HTML]

The following statement is the key CommandArgument='<%# DataBinder.Eval(Container,"DataItem.record_id")%>' (see line 12 in the code). NOTE: This statement will not work if the datasource of your datagrid does not contain a record_id column.

Now in the code behind.
I use the ItemCommand event of the datagrid to get the record_id of the row that has been selected for editing. In this event I have to check to see if the Item is part of the datagrid header section or if it is part of the datagrid pager section. If it is not then I can redirect the page to the Edit page and append the record_id as a querystring using e.CommandArgument.
Expand|Select|Wrap|Line Numbers
  1. public void dgReorgs_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
  2. {
  3. if(e.Item.ItemType != ListItemType.Header && e.Item.ItemType != ListItemType.Pager)
  4. {
  5. if(e.CommandName == "Edit")
  6. {
  7. Response.Redirect("EditReorg.aspx?value=" + e.CommandArgument);
  8. }
  9. }
  10. }
Then in the page_load event of the Edit page I will call a query that will get me the data for this row and then prepopulate the controls on the page with data.

I hope this code will help some of you understand how to use the CommandArgument property.

Nathan
Jun 11 '07 #1
1 13279
it's super.but i need gridview coding.u know the coding post me.
Feb 16 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: authorking | last post by:
I use the following code to insert a data record in to a datatable of an access database.But every time I execute the command, there will rise an exception and the insert operation can't be...
0
by: Solomon Shaffer | last post by:
I have a datagrid that I am adding a template column to (containing a couple of linkbuttons) at runtime and am having some difficulty setting the commandargument or the linkbuttons. Does anyone...
2
by: jason | last post by:
i'm trying to set the commandargument property of a buttoncolumn in the DataGrid ItemBind event. it works fine in one instance, but in the latest page i've written it doesn't seem to work, and i...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
0
by: Keith | last post by:
I have a web form that contains a repeater control that is designed to ask like a check book register. Clicking on a certain transaction takes the user to a different .aspx page where it can be...
1
by: Martin Pöpping | last post by:
Hello, I´ve a problem with the return values of a database (MS SQL) query. My code looks like this: foreach (DataRow row in dt.Rows) { this.fingerprints.Add(new DocumentFingerprint(row,...
1
by: Brett Wesoloski | last post by:
I am new to using template columns. I am just trying to create a data grid with a bound column and another column with a imagebutton in it. What I have always done in the past was to then create...
8
by: Lee | last post by:
guys, I have a project need to move more than 100,000 records from one database table to another database table every week. Currently, users input date range from web UI, my store procedure will...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.