Click here to Skip to main content
15,909,373 members
Home / Discussions / ASP.NET
   

ASP.NET

 
AnswerRe: Size of the textbox according to the data Pin
CrazyCoder2618-Jun-09 2:05
CrazyCoder2618-Jun-09 2:05 
QuestionEnable options according to button click Pin
<<Tash18>>18-Jun-09 1:11
<<Tash18>>18-Jun-09 1:11 
AnswerRe: Enable options according to button click Pin
himanshu256118-Jun-09 1:13
himanshu256118-Jun-09 1:13 
GeneralRe: Enable options according to button click Pin
<<Tash18>>18-Jun-09 1:14
<<Tash18>>18-Jun-09 1:14 
AnswerRe: Enable options according to button click Pin
CrazyCoder2618-Jun-09 1:39
CrazyCoder2618-Jun-09 1:39 
Questionhow to highlight newly inserted record in a gridview with paging=true? Pin
Pawan Kiran18-Jun-09 1:10
Pawan Kiran18-Jun-09 1:10 
AnswerRe: how to highlight newly inserted record in a gridview with paging=true? Pin
Amol-11118-Jun-09 1:34
Amol-11118-Jun-09 1:34 
AnswerRe: how to highlight newly inserted record in a gridview with paging=true? Pin
Member 387855310-Jul-09 17:45
Member 387855310-Jul-09 17:45 
The Code

In order to run the code below, you must first set up Membership. The simplest way to do this is to simply add to your web.config a very small section enabling RoleManager. This will automatically create the membership database in sqlexpress. You should probably add a couple users just so your gridview is not empty from the start. The code you need to add to an empty asp.net 2.0 web site project is as follows (put it in the <System.Web> section).



<roleManager enabled=“true“></roleManager>

Once you have done that, you can copy the code below to a new web page and simply run it.

Briefly, the way the code works is that when a row is inserted into the database the sqldatasource’s Inserted event is called. In this event, we take a look at the return parameter which comes from the sql:

InsertCommand=”INSERT INTO [Names] ([name]) VALUES (@name);SELECT @NewID = Scope_Identity()”

NewId is actually a return parameter, so we can get that back in the inserted event. Once we get the value back, we store it in viewstate so that on the upcoming page_prerender, we can check and see which row has that id in it, and then highlight that row. The reason we do it in prerender and not load is because the inserted event is processed after load and it would not work if we put it there.

So, here is the code! Good luck.

<%@ Page Language=”C#” %>



<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>



<script runat=”server”>



// simple table named: Names. Two columns: id int, name varchar(64)



protected void ButtonInsert_Click(object sender, EventArgs e)

{

SqlDataSource1.InsertParameters["name"].DefaultValue = DateTime.Now.ToString();

int numInserted = SqlDataSource1.Insert();

GridView1.DataBind();

}



protected void SqlDataSource1_Inserted(object sender, SqlDataSourceStatusEventArgs e)

{

object newId = e.Command.Parameters["@NewId"].Value;

ViewState["NewId"] = Convert.ToInt32(newId);



}



protected void Page_PreRender(object sender, EventArgs e)

{

string newIdLast = string.Empty;

if (ViewState["NewId"] != null)

{

int newId = (int)ViewState["NewId"];

newIdLast = newId.ToString();

int rowCnt = 0;

foreach (GridViewRow row in GridView1.Rows)

{

string newIdText = row.Cells[1].Text;

if (newIdText.Equals(newIdLast))

{

//GridView1.EditIndex = rowCnt;

//GridView1.SelectedIndex = rowCnt;

row.Attributes.Add(“bgcolor”, “Gray”);



break;

}

rowCnt++;

}

}

}

</script>



<html xmlns=”http://www.w3.org/1999/xhtml” >

<head id=”Head1″ runat=”server”>

<title>Untitled Page</title>

</head>

<body>

<form id=”form1″ runat=”server”>

<div>



<h2>Example of GridView that shows highlighted last inserted row</h2>

<br />

<asp:GridView ID=”GridView1″ runat=”server” AutoGenerateColumns=”False” DataKeyNames=”id” DataSourceID=”SqlDataSource1″ >

<Columns>

<asp:CommandField ShowEditButton=”True” ShowDeleteButton=”True” />

<asp:BoundField DataField=”id” HeaderText=”id” InsertVisible=”False” ReadOnly=”True”

SortExpression=”id” />

<asp:BoundField DataField=”name” HeaderText=”name” SortExpression=”name” />

</Columns>

</asp:GridView>

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server” ConnectionString=”<%$ ConnectionStrings:ConnectionString %>“

DeleteCommand=”DELETE FROM [Names] WHERE [id] = @id”

InsertCommand=”INSERT INTO [Names] ([name]) VALUES (@name);SELECT @NewID = Scope_Identity()”

SelectCommand=”SELECT [id], [name] FROM [Names]“ UpdateCommand=”UPDATE [Names] SET [name] = @name WHERE [id] = @id” OnInserted=”SqlDataSource1_Inserted”>

<DeleteParameters>

<asp:Parameter Name=”id” Type=”Int32″ />

</DeleteParameters>

<UpdateParameters>

<asp:Parameter Name=”name” Type=”String” />

<asp:Parameter Name=”id” Type=”Int32″ />

</UpdateParameters>

<InsertParameters>

<asp:Parameter Name=”name” Type=”String” />

<asp:Parameter Direction=Output Name=”NewId” Size=4 Type=Int16 />

</InsertParameters>

</asp:SqlDataSource>

<br />

<br />

&nbsp;<asp:Button ID=”ButtonInsert” runat=”server” OnClick=”ButtonInsert_Click” Text=”Insert Record” /></div>

</form>

</body>

</html>
Questionhow to set width of textbox based on the given input? Pin
Pawan Kiran18-Jun-09 0:59
Pawan Kiran18-Jun-09 0:59 
AnswerRe: how to set width of textbox based on the given input? Pin
CrazyCoder2618-Jun-09 2:04
CrazyCoder2618-Jun-09 2:04 
Question[Message Deleted] Pin
Inderjeet Kaur18-Jun-09 0:49
Inderjeet Kaur18-Jun-09 0:49 
AnswerRe: Address verification System(AVS) Pin
himanshu256118-Jun-09 0:58
himanshu256118-Jun-09 0:58 
AnswerRe: Address verification System(AVS) Pin
Abhijit Jana18-Jun-09 1:07
professionalAbhijit Jana18-Jun-09 1:07 
QuestionAddress verification System(AVS) Pin
Inderjeet Kaur18-Jun-09 0:49
Inderjeet Kaur18-Jun-09 0:49 
AnswerRe: Address verification System(AVS) Pin
DoctorMick18-Jun-09 1:16
DoctorMick18-Jun-09 1:16 
AnswerRe: Address verification System(AVS) Pin
Abhijit Jana18-Jun-09 1:24
professionalAbhijit Jana18-Jun-09 1:24 
QuestionRe: Address verification System(AVS) [modified] Pin
Inderjeet Kaur18-Jun-09 1:39
Inderjeet Kaur18-Jun-09 1:39 
GeneralPaypal integration Pin
coolsatty18-Jun-09 0:35
coolsatty18-Jun-09 0:35 
GeneralRe: Paypal integration Pin
DoctorMick18-Jun-09 1:20
DoctorMick18-Jun-09 1:20 
QuestionGet IP address Pin
raushan_918-Jun-09 0:34
raushan_918-Jun-09 0:34 
AnswerRe: Get IP address Pin
Vimalsoft(Pty) Ltd18-Jun-09 0:50
professionalVimalsoft(Pty) Ltd18-Jun-09 0:50 
AnswerRe: Get IP address Pin
httplover18-Jun-09 4:44
httplover18-Jun-09 4:44 
QuestionUploading of media files into chunks Pin
Member 391626718-Jun-09 0:18
Member 391626718-Jun-09 0:18 
AnswerRe: Uploading of media files into chunks Pin
Abhishek Sur18-Jun-09 1:38
professionalAbhishek Sur18-Jun-09 1:38 
GeneralRe: Uploading of media files into chunks Pin
Member 391626724-Jun-09 18:42
Member 391626724-Jun-09 18:42 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.