How to get recently inserted row index in Datalist - c#

Is there a way to get the index of the most recently inserted record that was bound to a datalist? My datalist is sorted, so my record is not the last one. I am trying to highlight the record that just got inserted, so I need to know where it is in the datalist.
This is in ASP .NET C#.
edit: I should probably rephrase my question. After I insert a record, I "re-get" my records and rebind the datalist to update it with the inserted record. Depending on what sort mode it is on, the newly added record can be anywhere in the list.

I hope if this will work for you.
<Columns>
<asp:TemplateField>
<ItemTemplate>
<%# Container.DataItemIndex + 1 %>
</ItemTemplate>
</asp:TemplateField>
</Columns>
REF: Autonumbering ASP.NET grid controls

Related

How to not get repeated values with gridview?

i have a gridview to insert values to the database dynamically..
i have one dropdownlist for selecting employee id and one textbox for getting personnel details...
dropdownlist values are getting from database using select query and binding those values into dropdownlist which exists in gridview
personnel details text box values entered by user for each employee
if one employee selects from dropdownlist and inserts details into textbox after submitting his details his empid must hide in that dropdownlist...for not getting multiple rows about the employee.
<asp:TemplateField HeaderText="EMPID>
<ItemTemplate>
<asp:DropDownList ID="empid" runat="server" Width="100px">
</asp:DropDownList>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="EMPID>
<ItemTemplate>
<asp:TextBox id="txtbox" runat="server" width="100px"></asp:TextBox>
</ItemTemplate>
<FooterTemplate>
<asp:Button ID="Add_Details" runat="server" Text="Add Details" OnClick="Addhour_Click"/>
</FooterTemplate>
</asp:TemplateField>
in the above empid is the dropdownlist which binds data from database...
and txtbox is used to get the personneldetails about the empid selected from dropdownlist empid...
after selecting one id from dropdownlist and entering some data into text if we click to add details button the details are storing into database correctly and inserting another row into gridview..
but again already inserted empid also displaying in that dropdownlist...
how to remove that item from dropdownlist after inserting data into database for perticular emp..id.. thanks in advance.. please help me to solve this problem...
If you want to really hide id from dropdown then you can add field (may be bit datatype and name as addedRecord) in the table from where Id is coming. set that field if record is added and bind drop down with only ids that are not set. Otherwise you can check in database before adding personnel info, if that exists then don't insert it.
Use your postback correctly Give this in your pageload()
if(!postback)
{
ddlbindcode();//dropdownlist bind query
}

Cascading dropDownLists in repeater. DropDownLists with SQL source and add/delete option

What is the trick to implement the following interface in ASP.NET?
I think the screenshot should explain everything.
Both dropDownLists work on SQL tables. [Categories],[Items] bound with ID_category PK/FK
Number of rows variable (by default 1 row with "Add" button)
Choosing category changes only the content of the dropDownList next to it and the content stays the same when other rows get added/deleted
"Add" button saves current setup and adds new row with either "Select value" or eventually 1st category/1st item selected
How to store previously selected categories/items and the dropDown filtering by category, while adding, changing, deleting items or some other PostBack on website?
What I have tried so far is to use Repeater with DataSet, however I have encountered several different problems:
Was unable to preserve the relations between previous dropDowns
DropDowns resetting one another
2nd dropDown losing filtering when adding new row
I can post some code but since it doesn't fully work, perhaps a totally different approach would be better. Basically I started with this tutorial. The repeater currently looks like:
<repeater>
<itemtemplate>
<dropDown DataSource="categoriesDS"
value='<%# DataBinder.Eval(Container.DataItem, "Category") %>'.../>
<dropDown DataSource="itemsDS"
value='<%# DataBinder.Eval(Container.DataItem, "Item") %>'.../>
<button CommandName='<%# DataBinder.Eval(Container.DataItem, "Button") %>'
Text='<%# DataBinder.Eval(Container.DataItem, "Button") %>' .../>
</itemtemplate>
</repeater>
Thanks in advance for any suggestions or solutions and I hope it'll be helpful for others.
Ajax Cascading DropDownlist
It is very simple to use.

Pagination for Grid View

I am using a Grid View of asp.net 4.0. I want to use pagination.
I am Handling Pagination with SQL query. I am getting only the number of records, i want to show in my grid. I want to set the total number of records to my grid, so that my grid displays page numbers (eg. 1 2 3 ....8) according to my total number of records. My grid should tell me what is the current page number which is been viewed.
I am unable to set Page numbers according to my total record count.
Please Help me on this.
Thanks in Advance.
Brining back the whole result set to get paging is probably not the way you want to be going. You should really extend the GridView Control and introduce a virtual item count property. You should then pass the virtual item count the Count of all records in your grid. Also you should then ensure that OnPageIndexChanging event should then set the current page index to the next page.
better approach I ever got for this, using store procedure and returning the data based on PageIndex and PageSize.
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">
<Columns>
//your data
</Columns>
</asp:GridView>
This shows pagination
<asp:Repeater ID="rptPager" runat="server">
<ItemTemplate>
<asp:LinkButton ID="lnkPage" runat="server" Text = '<%#Eval("Text") %>' CommandArgument = '<%# Eval("Value") %>' Enabled = '<%# Eval("Enabled") %>' OnClick = "Page_Changed"></asp:LinkButton>
</ItemTemplate>
</asp:Repeater>
It's all here what you want: Custom Paging in ASP.Net GridView using SQL Server Stored Procedure

How to select a row from grid view and edit the details in a new webform using ASP.NET

I have displayed some fields in grid view, Now i want to select a row and i want to display all field from sql server in to a web form, after displaying the data in respective controls(textbox, dropdown), i want to update it.
How i can redirect the page to new webpage after selecting the respective row from grid view (Using primary key). and how the data will be pass on page load in respective fields so i could able to update it by using update query.
As i new to ASP. Net. Please update me in detail. If possible please with code.
It would go something like this.
You will make a 'list' webform with all records containing the gridview
Make another webform called details
The gridview on list page will contain a hyperlink button field something like this
<asp:HyperLinkField Text="View Details" DataNavigateUrlFields="YourId" DataNavigateUrlFormatString="details.aspx?id={0}" />
(Notice I am just redirecting to the details page passing the field id with querystring.
Now, on details page you would just fetch the querystring value & query your database with a datareader & populate your details page.
Something like on page load
if(Request.QueryString["id"])!=null)
{
// Fetch respective item from database here & populate form fields
// Open connection(); execute datareader;populate form fields
}
Easiest way to do this:
<asp:GridView ID="grd" runat="server" autogeneratedcolumn="false">
<asp:TemplateField HeaderText="ID">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HeaderText="Name">
<ItemTemplate>
<%#Eval("ID")%>
<ItemTemplate>
<asp:TemplateField>
<asp:TemplateField HaderText="Edit">
<ItemTemplate>
<a href='EditData.aspx?ID=<%#Eval("ID")%>'>Edit</a>
</ItemTemplate>
<asp:TemplateField>
<asp:GridView>
EditData.aspx page will be used to edit the data. After receiving the Query-string Value,we can bind the data to the controls and then update the data after making changes.

When does a Gridview have a null DataSource?

I've read multiple sources that say Gridview's do not persist the Gridview.DataSource property on postback. My understanding is that in term's of ASP.NET, a postback is any page load that is not the first pageload (see MSDN).
I've got a situation with 2 very similar gridviews.
GvOne.DataSource is null on postback.
GvTwo.DataSource is NOT null on postback.
The only big difference outside of a few differing columns is GvOne is populated with the Entity Framework and LINQ. GvTwo is populated by a DataTable filled by a SqlDataAdapter.
Further, GvOne and GvTwo have a TemplateField with a TextBox that I use to gather user input. Both use the same code to pull the TextBox.Text on postback:
TextBox tb = (TextBox)GvOne.Rows[i].FindControl("actualTxt");
GvOne properly gathers tb.Text. GvTwo always finds the tb.Text value to be 0.
Basic Gridview code:
<asp:GridView ID="GvOne" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Return">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
<asp:GridView ID="GvTwo" runat="server" AutoGenerateColumns="False">
<Columns>
<asp:TemplateField HeaderText="Order">
<ItemTemplate>
<asp:TextBox id="actualTxt" runat="server" Text='0' Width="40px"></asp:TextBox>
</ItemTemplate>
</asp:TemplateField>
...
</Columns>
</asp:GridView>
Changing GvTwo to use Entity Framework and LINQ is a potential solution, albeit a major undertaking. Does anyone know what is going on here?
UPDATE (See my comment on Joel Etherton's Answer)
Due to popular demand here is the code to populate the gridview within Page_Load event for GvTwo (GvOne is similar):
ordersGV.DataSource = dataSetObject.Tables["activeParts"];
ordersGV.DataBind();
Searching through the code behind I found no other references to ordersGv.Datasource and no other events that are hooked into associated with the page life cycle.
Gridviews do not persist the datasource across postbacks. If you have a gridview that has a non-null datasource then you must be filling that datasource somewhere in your code. It would be instructive to travel through your event cycle to find where exactly the population of the datasource is occuring on postback.
what does your Page_load code look like?
GridView does not keep DataSource property populated over the postbacks for performance issues
Maybe the second gridview is rebinding the datasource on postback?

Categories