Categories
General (9)
General ASP.Net (6)
Javascript (6)
ASP.Net WebForms (6)
GridView Controls (7)
ASP.Net AJAX (2)
ASP.Net MVC (1)
 
 
Archive
Aug 2010 (1)
Jul 2010 (1)
Jun 2010 (1)
May 2010 (1)
Apr 2010 (1)
Mar 2010 (1)
Feb 2010 (2)
Jan 2010 (1)
Dec 2009 (2)
Nov 2009 (5)
Oct 2009 (10)
Sep 2009 (6)
Aug 2009 (2)
 
 
7607 Visits
 
Sep 2009

Camel-Casing TextBox Text

In a textbox, the text needs to be displayed as Camel-cased

Camel-case is where the first letter of every word is capitalized. Eg, How Are You? This can be done using the text-transform style as follows:

<asp:TextBox runat="server" style="text-transform: Capitalize;"/>

2 Comment(s), Your comments?

Handling OnDblClick Event of GridViewRow

I have a GridView that displays a list of names. On double-clicking a name, the name needs to be set in a textbox. This needs to happen w/o any postback

The RowCreated event of GridView should add a javascript event handler for the OnDblClick event as follows: ASPX Page:

<asp:TextBox ID="txtSearch" runat="server"/> <asp:GridView ID="gvMain" runat="server" AutoGenerateColumns="false" OnRowCreated="gvMain_RowCreated"> <Columns> <asp:BoundField HeaderText="Name" DataField="Name" /> </Columns> </asp:GridView>
RowCreated Event Handler:
protected void gvMain_RowCreated(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { e.Row.Attributes.Add("OnDblClick", "setSearch(" + e.Row.RowIndex + ");"); } }
The Javascript for the SetSearch() is below:
<script language="javascript" type="text/javascript"> function setSearch(row) { var tbl = document.getElementById("gvMain"); var txt = document.getElementById("txtSearch"); txt.value = tbl.rows[row+1].cells[0].innerText; } </script>
Since GridView is rendered as table, the individual cell can be accessed as tbl.rows[rowIndex].cells[cellIndex].innerText

1 Comment(s), Your comments?

SQL for Matrix Reports

I have a list of students in the student table. I have a list of exams in the exam table. There is a score table with the following columns studentID, examID and score. I want to create a matrix report with students in the rows and exams in the columns

The report will look like this:

Student NameExam 1Exam 2
Vijay7383
Rahul7964
If we can get the SQL select statement as SELECT StudentName, Exam 1, Exam 2 FROM TempTable, we can accomplish this matrix report. The whole point is writing a stored procedure to create this matrix table. Below is the SQL stored procedure for creating the matrix. This involves cursors and dynamic SQL:
CREATE PROCEDURE [dbo].[GetScoreMatrix] AS CREATE TABLE #Temp ( StudentName VARCHAR(50) ) DECLARE @sTable VARCHAR(1000) DECLARE @sCol VARCHAR(1000) SET @sTable = 'ALTER TABLE #Temp ADD ' DECLARE cExam CURSOR FOR SELECT ExamName FROM Exam OPEN cExam DECLARE @sExamName VARCHAR(50) FETCH NEXT FROM cExam INTO @sExamName WHILE @@FETCH_STATUS = 0 BEGIN SET @sCol = @sTable + '[' + @sExamName + '] INT NULL' EXEC (@sCol) FETCH NEXT FROM cExam INTO @sExamName END CLOSE cExam DEALLOCATE cExam INSERT INTO #Temp (StudentName) SELECT StudentName FROM Student DECLARE cScore CURSOR FOR SELECT s.StudentName, e.ExamName, x.Score FROM STUDENT s INNER JOIN Score x ON s.StudentID = x.StudentID INNER JOIN Exam e ON x.ExamID = e.ExamID OPEN cScore DECLARE @sStudName VARCHAR(50) DECLARE @iScore INT DECLARE @sUpdate VARCHAR(1000) FETCH NEXT FROM cScore INTO @sStudName, @sExamName, @iScore WHILE @@FETCH_STATUS <> -1 BEGIN SET @sUpdate = 'UPDATE #Temp ' SET @sUpdate = @sUpdate + 'SET [' + @sExamName + '] = ' + CAST(@iScore AS VARCHAR(3)) SET @sUpdate = @sUpdate + ' WHERE StudentName = ''' + @sStudName + '''' EXEC (@sUpdate) FETCH NEXT FROM cScore INTO @sStudName, @sExamName, @iScore END CLOSE cScore DEALLOCATE cScore SELECT * FROM #Temp DROP TABLE #Temp GO

0 Comment(s), Your comments?

Confirm Backdating

I have a calendar control. If the user wants to backdate (ie, SelectedDate < Today), then I want to display a confirm messagebox asking the user: 'Are you sure you want to backdate?'

Calendar Control has a OnDayRender event that you can handle. In the eventhandler, for all date before today's date, add the javascript: confirm() function to the OnClick attribute as follows:

protected void cal_DayRender(object sender, DayRenderEventArgs e) { if(e.Day.Date < DateTime.Today) e.Cell.Attributes.Add("OnClick", "return confirm('Are you sure you want to backdate?');"); }

1 Comment(s), Your comments?

Application_End() and Server.MapPath()

I wrote the following code in Application_End. It appeared as if Application_End was not firing

XmlDocument statDoc = new XmlDocument(); string path = Server.MapPath("~/App_data/Stat.xml"); statDoc.Load(path); statDoc.SelectSingleNode("/Stats/Stat[@id=\"Visits\"]").Attributes["Count"].Value = Application["Visits"].ToString(); statDoc.SelectSingleNode("/Stats/Stat[@id=\"Hits\"]").Attributes["Count"].Value = Application["Hits"].ToString(); statDoc.Save(path);

With the help of this forum post, I was able to solve the mystery. Server.MapPath does not work within Application_End() or Server.End()

2 Comment(s), Your comments?

Inserting Image into the database

How do I insert an image into the database?

In the ASPX page, have a asp:FileUpload control and a Submit button. On clicking the submit button, the following code inserts into the table - Image and column - ImageStream, of type varbinary

<asp:FileUpload ID="fup" runat="server" /> <asp:Button ID="btnSubmit" runat="server" Text="Hello" OnClick="btnSubmit_Click" />
protected void btnSubmit_Click(object sender, EventArgs e) { byte [] bytes = fup.FileBytes; SqlConnection conn = new SqlConnection("Data Source=localhost; Database=Pubs; Integrated Security=true;"); conn.Open(); string sql = "INSERT INTO IMAGE (ImageStream) VALUES (@bytes)"; SqlCommand comm = new SqlCommand(sql, conn); comm.Parameters.AddWithValue("@bytes", bytes); comm.ExecuteNonQuery(); }
2 Comment(s), Your comments?