473,846 Members | 1,983 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

nateraaaa
663 Recognized Expert Contributor
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_SortCo mmand" OnPageIndexChan ged="dgReorgs_P ageIndexChanged "
PagerStyle-HorizontalAlign ="Center" PagerStyle-Mode="NumericPa ges" BorderWidth="0p x" CellSpacing="1" CellPadding="2" AllowSorting="T rue" ShowFooter="Tru e" AutoGenerateCol umns="False" PageSize="20"
AllowPaging="Tr ue" OnItemCommand=" dgReorgs_ItemCo mmand" PagerStyle-CssClass="Pagin gLink">
<Columns>
<asp:TemplateCo lumn>
<HeaderStyle HorizontalAlign ="Center"></HeaderStyle>
<ItemStyle HorizontalAlign ="Center"></ItemStyle>
<HeaderTemplate >
<asp:ImageButto n ID="ibtnAdd" Runat="server" ImageUrl="image s/Add.gif" OnClick="ibtnAd d_Click" Visible="False" ></asp:ImageButton >
</HeaderTemplate>
<ItemTemplate >
<asp:ImageButto n ID="ibtnEdit" Visible="False" Runat="server" ImageUrl="image s/Edit.gif" CommandArgument ='<%# DataBinder.Eval (Container,"Dat aItem.record_id ")%>' CommandName="Ed it">
</asp:ImageButton >&nbsp;
</ItemTemplate>
</asp:TemplateCol umn>
<asp:BoundColum n DataField="offe r_posting_date" SortExpression= "offer_posting_ date" HeaderText="Off er Posting Date"
DataFormatStrin g="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="symb ol" SortExpression= "symbol" HeaderText="Sym bol" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="offe r_expiration_da te" SortExpression= "offer_expirati on_date" HeaderText="Exp iration Date"
DataFormatStrin g="{0:MM/dd/yyyy}" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle HorizontalAlign ="Center" BackColor="Whit e"></ItemStyle>
</asp:BoundColumn >
<asp:BoundColum n DataField="note s" SortExpression= "notes" HeaderText="Not es" HeaderStyle-Font-Size="10">
<HeaderStyle HorizontalAlign ="Center" ForeColor="Whit e" BackColor="Navy "></HeaderStyle>
<ItemStyle Width="180px" BackColor="Whit e" Wrap="True"></ItemStyle>
</asp:BoundColumn >
</Columns>
<PagerStyle HorizontalAlign ="Center" Mode="NumericPa ges"></PagerStyle>
</asp:datagrid>[/HTML]

The following statement is the key CommandArgument ='<%# DataBinder.Eval (Container,"Dat aItem.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.CommandArgume nt.
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 13311
sugunavathy
2 New Member
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
11840
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 completed. here's the code: System.Data.OleDb.OleDbConnection hist_cnn = new System.Data.OleDb.OleDbConnection(); System.Data.OleDb.OleDbCommand hist_command = new System.Data.OleDb.OleDbCommand();
0
1566
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 know the syntax to databind the commandargument to the row through code. I am assumning I use the DataBinder.Eval method but I cannot get it going. Below is the code sample. Thanks Solomon public class KnowlagentDataGridTemplate : ITemplate
2
2672
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 can't see why. can anyone tell me why this chunk of code works: <asp:datagrid id="DataGrid1" style="Z-INDEX: 104; LEFT: 16px; POSITION: absolute; TOP: 136px" runat="server" CellPadding="3" OnItemDataBound="DataGridBindEvent"...
1
6432
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 binding it to a repeater control. This repeater control has multiple text boxes and buttons. Can you please tell me how can i do paging in this case ? I'm posting my code below. The problem is that if i click on "AdjustThisAd" button, it opens...
0
2188
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 edited and then saved. Currently, after saving the edited transaction and returning to the check register, the repeater control table returns to the bottom. I would like the edited transaction to appear in the table instead. I know that a...
0
1897
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 edited and then saved. Currently, after saving the edited transaction and returning to the check register, the repeater control table returns to the bottom. I would like the edited transaction to appear in the table instead. I know that a JavaScript...
1
1518
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, row));
1
2125
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 a datatable and bind that to the datagrid. Where is my code to bind the grid to the datatable. DataTable dt = new DataTable(); dt.Columns.Add("FundDescription");
8
4081
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 take those date ranges to INSERT records to a table in another database, then delete the records, but it will take really long time to finish this action (up to 1 or 2 hours). My question is if there is some other way I should do to speed up...
0
9879
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, well explore What is ONU, What Is Router, ONU & Routers main usage, and What is the difference between ONU and Router. Lets take a closer look ! Part I. Meaning of...
0
9725
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10976
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10640
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10705
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10330
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5714
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4111
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3157
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.