Monday, May 16, 2011

Data View Web Part: generating a consecutive number.

Task in hand: A Data View Web Part pulls a list of records. The "ComplaintNumber" field is the primary key. I needed the UI generate a complaint number when a new record is being inserted.

Solution: In my AllItems.aspx I had this automatically generated code:
<td class="ms-vb">
<asp:TextBox runat="server" id="ff5{$Pos}"  __designer:bind="{ddwrt:DataBind('i',concat('ff5',$Pos),'Text','TextChanged','ComplaintNumber',ddwrt:EscapeDelims(string(@ComplaintNumber)),'@ComplaintNumber')}" />
</td>

Which allows replacing the value of the ComplaintNumber field in the database by whatever number the user enters.
Under <xsl:template name="dvt_1.rowinsert"> I added the following line:
<xsl:variable name="mynewcomplaint" select="(/dsQueryResponse/NewDataSet/Row[position()=last()]/@ComplaintNumber)+1"></xsl:variable>
mynewcomplaint stores the value contained in the last node + 1 (e.g. if the last complaint number in the database is 15 then mynewcomplaint=16. Note that the expression position()=last() is evaluated and if the calculation returns true, the item is included in the rowset; if it returns false, the item is not included.  Therefore, only the last item is included in this case.

Finally I added the text attribute to the Text Box and assigned to it the value of mynewcomplaint:
<td class="ms-vb">
<asp:TextBox runat="server" id="ff5{$Pos}" text="{$mynewcomplaint}" __designer:bind="{ddwrt:DataBind('i',concat('ff5',$Pos),'Text','TextChanged','ComplaintNumber',ddwrt:EscapeDelims(string(@ComplaintNumber)),'@ComplaintNumber')}" />
</td>

This way, when the user clicks on the "New" link ( <a href="javascript: {ddwrt:GenFireServerEvent('__cancel;dvt_1_form_insertmode={1}')}">New</a> ) a new complaint number appears in the text box.
Cheers!

No comments:

Post a Comment

Famous quotes

Even if you do learn to speak correct English, whom are you going to speak it to?

Clarence Darrow (1857 -1938)